| 1 |  | 
|---|
| 2 | /* | 
|---|
| 3 |  | 
|---|
| 4 |     orxonox - the future of 3D-vertical scrollers | 
|---|
| 5 |      | 
|---|
| 6 |     Copyright (C) 2004 orx | 
|---|
| 7 |      | 
|---|
| 8 |     This program is free software; you can redistribute it and/or modify it  | 
|---|
| 9 |     under the terms of the GNU General Public License as published by the Free  | 
|---|
| 10 |     Software Foundation; either version 2, or (at your option) any later version. | 
|---|
| 11 |      | 
|---|
| 12 |     ### File Specific: | 
|---|
| 13 |     main-programmer: David Gruetter | 
|---|
| 14 |     co-programmer: ... | 
|---|
| 15 |      | 
|---|
| 16 |      | 
|---|
| 17 |     Created by Dave, in this file shadow will be implemented in a quite sexy and | 
|---|
| 18 |     fast way, with a lot of opengl-code, to keep it fast! The origin of the code | 
|---|
| 19 |     comes form an example at www.frustum.org, slitly different although, so that  | 
|---|
| 20 |     it works with Orxonox:) | 
|---|
| 21 |      | 
|---|
| 22 |     */ | 
|---|
| 23 |      | 
|---|
| 24 | #include "importer/material.h" | 
|---|
| 25 | #include "stdincl.h" | 
|---|
| 26 | #include <stdio.h> | 
|---|
| 27 | #include <stdarg.h> | 
|---|
| 28 | #include <malloc.h> | 
|---|
| 29 | #include <math.h> | 
|---|
| 30 | #include "shadow.h" | 
|---|
| 31 |  | 
|---|
| 32 | #define SIZE    128 | 
|---|
| 33 | #define GL_CLAMP_TO_EDGE_EXT 0x812F | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 |    \brief default Constructor | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 | Shadow::Shadow(Model* player,float* groundVertexes) | 
|---|
| 42 | { | 
|---|
| 43 |     this->player=player; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 |     \brief default destructor | 
|---|
| 49 | */ | 
|---|
| 50 |  | 
|---|
| 51 | Shadow::~Shadow() | 
|---|
| 52 | { | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void Shadow::init() | 
|---|
| 56 | { | 
|---|
| 57 |      | 
|---|
| 58 |     float plane_s[] ={1.0f,0.0f,0.0f,0.0f}; | 
|---|
| 59 |     float plane_t[] ={0.0f,1.0f,0.0f,0.0f}; | 
|---|
| 60 |     float plane_r[] ={0.0f,0.0f,1.0f,0.0f}; | 
|---|
| 61 |     float plane_q[] ={0.0f,0.0f,0.0f,1.0f}; | 
|---|
| 62 |      | 
|---|
| 63 |     glClearDepth(1); | 
|---|
| 64 |     glDepthFunc(GL_LEQUAL); | 
|---|
| 65 |     glTexGenfv(GL_S,GL_EYE_PLANE,plane_s); | 
|---|
| 66 |     glTexGenfv(GL_T,GL_EYE_PLANE,plane_t); | 
|---|
| 67 |     glTexGenfv(GL_R,GL_EYE_PLANE,plane_r); | 
|---|
| 68 |     glTexGenfv(GL_Q,GL_EYE_PLANE,plane_q); | 
|---|
| 69 |     glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); | 
|---|
| 70 |     glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); | 
|---|
| 71 |     glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); | 
|---|
| 72 |     glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); | 
|---|
| 73 |      | 
|---|
| 74 |     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); | 
|---|
| 75 |     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); | 
|---|
| 76 |     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE_EXT); | 
|---|
| 77 |     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE_EXT); | 
|---|
| 78 |     glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,SIZE,SIZE,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); | 
|---|
| 79 |  | 
|---|
| 80 |     this->ground_id=glGenLists(1); | 
|---|
| 81 |     glNewList(this->ground_id,GL_COMPILE); | 
|---|
| 82 |      | 
|---|
| 83 |     //blabla | 
|---|
| 84 |      | 
|---|
| 85 |     glEndList(); | 
|---|
| 86 |      | 
|---|
| 87 |     this->player_id=glGenLists(1); | 
|---|
| 88 |     glNewList(this->player_id,GL_COMPILE); | 
|---|
| 89 |      | 
|---|
| 90 |     //blabla | 
|---|
| 91 |      | 
|---|
| 92 |     glEndList();     | 
|---|
| 93 |      | 
|---|
| 94 |     this->image=(unsigned char*)malloc(SIZE*SIZE*4); | 
|---|
| 95 |      | 
|---|
| 96 |     //this->player_id=newList {glNormalefv;glVertex3fv} | 
|---|
| 97 |     //this->ground_id=newList {glTexCoord2fv;glNormal3fv;glVertex3fv} | 
|---|
| 98 |      | 
|---|
| 99 |      | 
|---|
| 100 |     //lightPos[] blabla | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | void Shadow::createShadow() | 
|---|
| 105 | { | 
|---|
| 106 |     glViewport(0,0,SIZE,SIZE); | 
|---|
| 107 |     glScissor(0,0,SIZE,SIZE); | 
|---|
| 108 |     glEnable(GL_SCISSOR_TEST); | 
|---|
| 109 |     glBindTexture(GL_TEXTURE_2D,this->shadow_id); | 
|---|
| 110 |      | 
|---|
| 111 |     glClearColor(1,1,1,1); | 
|---|
| 112 |     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
|---|
| 113 |     glMatrixMode(GL_PROJECTION); | 
|---|
| 114 |     glLoadIdentity(); | 
|---|
| 115 |     glOrtho(-1,1,-1,1,-100,100); | 
|---|
| 116 |     glMatrixMode(GL_MODELVIEW); | 
|---|
| 117 |     glLoadIdentity(); | 
|---|
| 118 |      | 
|---|
| 119 |     //gluLookAt(light0,light1,light2,player0,player1,player2,0,0,1); | 
|---|
| 120 |      | 
|---|
| 121 |     glColor3f(.4,.4,.4); | 
|---|
| 122 |     //glTranslatef(player0,player1,player2); | 
|---|
| 123 |     glCallList(this->player_id); | 
|---|
| 124 |     glColor3f(1,1,1); | 
|---|
| 125 |     glReadPixels(0,0,SIZE,SIZE,GL_RGB,GL_UNSIGNED_BYTE,this->image); | 
|---|
| 126 |     blur(this->image,SIZE); | 
|---|
| 127 |     glTexSubImage2D(GL_TEXTURE_2D,0,0,0,SIZE,SIZE,GL_RGB,GL_UNSIGNED_BYTE,this->image); | 
|---|
| 128 |      | 
|---|
| 129 |     glDisable(GL_SCISSOR_TEST); | 
|---|
| 130 |     glViewport(0,0,1024,768); //Achtung: hier Aufloesung von Orxonox einstellen! | 
|---|
| 131 |       | 
|---|
| 132 |      | 
|---|
| 133 |      | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void Shadow::m_inverse(const float *m,float *out) | 
|---|
| 137 | { | 
|---|
| 138 |     float det; | 
|---|
| 139 |     det=m[0]*m[5]*m[10]; | 
|---|
| 140 |     det+= m[4]*m[9]*m[2]; | 
|---|
| 141 |     det+= m[8]*m[1]*m[6]; | 
|---|
| 142 |     det-= m[8]*m[5]*m[2]; | 
|---|
| 143 |     det-= m[4]*m[1]*m[10]; | 
|---|
| 144 |     det-= m[0]*m[9]*m[6]; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|
| 148 | void Shadow::draw() | 
|---|
| 149 | { | 
|---|
| 150 |     float m[16],im[16]; | 
|---|
| 151 |     createShadow(); | 
|---|
| 152 |      | 
|---|
| 153 |     glClearColor(0,0,0,0); | 
|---|
| 154 |     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
|---|
| 155 |     glMatrixMode(GL_PROJECTION); | 
|---|
| 156 |     glLoadIdentity(); | 
|---|
| 157 |     gluPerspective(45,4.0/3.0,.5,100); | 
|---|
| 158 |     glMatrixMode(GL_MODELVIEW); | 
|---|
| 159 |     glLoadIdentity(); | 
|---|
| 160 |      | 
|---|
| 161 |     //gluLookAt(camera0,camera1,camera2,player0,player1,player2,0,0,1); | 
|---|
| 162 |      | 
|---|
| 163 |     glEnable(GL_TEXTURE_GEN_S); | 
|---|
| 164 |     glEnable(GL_TEXTURE_GEN_T); | 
|---|
| 165 |     glEnable(GL_TEXTURE_GEN_R); | 
|---|
| 166 |     glEnable(GL_TEXTURE_GEN_Q); | 
|---|
| 167 |     glGetFloatv(GL_MODELVIEW_MATRIX,m); | 
|---|
| 168 |      | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | /** | 
|---|
| 174 |     \don't ask me how this works, but it adds a blur effect to the shadow | 
|---|
| 175 |     \for it doesn't look that edgy | 
|---|
| 176 |      | 
|---|
| 177 | */ | 
|---|
| 178 | void Shadow::blur(unsigned char *in,int size) | 
|---|
| 179 | { | 
|---|
| 180 |     int x,y,sum,size3=size*3; | 
|---|
| 181 |     unsigned char *out,*inp,*outp; | 
|---|
| 182 |     out = (unsigned char *)malloc(size * size * 3); | 
|---|
| 183 |     memset(out,255,size *size *3); | 
|---|
| 184 |      | 
|---|
| 185 |     inp=in+size3; | 
|---|
| 186 |     outp=out+size3; | 
|---|
| 187 |     for(y=1;y<size-1;y++){ | 
|---|
| 188 |         inp+=3; | 
|---|
| 189 |         outp+=3; | 
|---|
| 190 |         for(x=1;x<size-1;x++){ | 
|---|
| 191 |             sum=inp[-size3-3]+ inp[-size3] + inp[-size3+3]+ | 
|---|
| 192 |             inp[-3]+inp[0]+inp[3]+ | 
|---|
| 193 |             inp[size3-3]+inp[size3]+inp[size3+3]; | 
|---|
| 194 |             sum/=9; | 
|---|
| 195 |             inp+=3; | 
|---|
| 196 |             *outp++ =sum; | 
|---|
| 197 |             *outp++ =sum; | 
|---|
| 198 |             *outp++ =sum; | 
|---|
| 199 |         } | 
|---|
| 200 |         inp+=3; | 
|---|
| 201 |         outp+=3; | 
|---|
| 202 |     } | 
|---|
| 203 |      | 
|---|
| 204 |     memcpy(in,out,size*size*3); | 
|---|
| 205 |     free(out); | 
|---|
| 206 |          | 
|---|
| 207 |              | 
|---|
| 208 |      | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|