00001 #include <gra/render/GLEnergyRender.h>
00002 #include <gra/camera/CameraGL.h>
00003 #include <esg/explorer/ObjsExplorer.h>
00004 #include <GL/gl.h>
00005 #include <assert.h>
00006
00007 #ifdef HAVE_FREE_GLUT
00008 #include <GL/freeglut.h>
00009 #else
00010 #include <GL/glut.h>
00011 #endif
00012
00013
00014 using namespace gra;
00015
00016 #define __SET_MAP_FACET(c,e,s) { \
00017 glBegin(GL_POLYGON); { \
00018 glTexCoord2f(0.0, 0.0); \
00019 glVertex3f((c).x + s[0] * (e.x/2), \
00020 (c).y + s[1] * (e.y/2), \
00021 (c).z + s[2] * (e.z/2)); \
00022 glTexCoord2f(0.0, 1.0); \
00023 glVertex3f((c).x + s[3] * (e.x/2), \
00024 (c).y + s[4] * (e.y/2), \
00025 (c).z + s[5] * (e.z/2)); \
00026 glTexCoord2f(1.0, 1.0); \
00027 glVertex3f((c).x + s[6] * (e.x/2), \
00028 (c).y + s[7] * (e.y/2), \
00029 (c).z + s[8] * (e.z/2)); \
00030 glTexCoord2f(1.0, 0.0); \
00031 glVertex3f((c).x + s[9] * (e.x/2), \
00032 (c).y + s[10] * (e.y/2), \
00033 (c).z + s[11] * (e.z/2)); \
00034 } glEnd(); }
00035
00036 void GLEnergyRender::_render_light_map(LightMap& lightMap)
00037 {
00038 Color3f color;
00039 int s1[12] = {-1,-1,+1,-1,+1,+1,+1,+1,+1,+1,-1,+1};
00040 int s2[12] = {+1,-1,-1,+1,+1,-1,-1,+1,-1,-1,-1,-1};
00041 int s3[12] = {-1,-1,-1,-1,+1,-1,-1,+1,+1,-1,-1,+1};
00042 int s4[12] = {+1,-1,+1,+1,+1,+1,+1,+1,-1,+1,-1,-1};
00043 int s5[12] = {-1,+1,+1,-1,+1,-1,+1,+1,-1,+1,+1,+1};
00044 int s6[12] = {-1,-1,-1,-1,-1,+1,+1,-1,+1,+1,-1,-1};
00045
00046 for (int i = 0; i < 6; i++) {
00047 GLfloat *pixels = new GLfloat [lightMap.vRes() * lightMap.uRes() * 3];
00048 unsigned i = 0;
00049 for (unsigned v = 0; v < lightMap.vRes(); v++)
00050 for (unsigned u = 0; u < lightMap.uRes(); u++)
00051 if (lightMap.getValue((LightMap::MapIndex)i, u, v, color)) {
00052 pixels[i++] = (GLfloat) color.x;
00053 pixels[i++] = (GLfloat) color.y;
00054 pixels[i++] = (GLfloat) color.z;
00055 #if 0
00056 pixels[i++] = (GLfloat) 1.0/(v+1.0);
00057 pixels[i++] = (GLfloat) 1.0/(u+1.0);
00058 pixels[i++] = (GLfloat) color.z;
00059 #endif
00060 } else {
00061 pixels[i++] = (GLfloat) 0.0;
00062 pixels[i++] = (GLfloat) 0.0;
00063 pixels[i++] = (GLfloat) 0.0;
00064 }
00065
00066 GLuint textureName;
00067 glGenTextures(1, &textureName);
00068 glBindTexture(GL_TEXTURE_2D, textureName);
00069
00070
00071 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00072 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00073 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00074 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
00075 lightMap.uRes(), lightMap.vRes(), 0,
00076 GL_RGB, GL_FLOAT, pixels);
00077
00078 glEnable(GL_TEXTURE_2D);
00079 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00080 glBindTexture(GL_TEXTURE_2D, textureName);
00081
00082 if ((LightMap::MapIndex) i == LightMap::NEAR_MAP)
00083 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s1);
00084 if ((LightMap::MapIndex) i == LightMap::FAR_MAP)
00085 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s2);
00086 if ((LightMap::MapIndex) i == LightMap::LEFT_MAP)
00087 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s3);
00088 if ((LightMap::MapIndex) i == LightMap::RIGHT_MAP)
00089 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s4);
00090 if ((LightMap::MapIndex) i == LightMap::TOP_MAP)
00091 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s5);
00092 if ((LightMap::MapIndex) i == LightMap::BOTTOM_MAP)
00093 __SET_MAP_FACET(lightMap.centroid(), lightMap.extent(), s6);
00094
00095 glDisable(GL_TEXTURE_2D);
00096 glDeleteTextures(1, &textureName);
00097
00098 delete [] pixels;
00099 }
00100 }
00101
00102 #undef __SET_MAP_FACET
00103
00104 void GLEnergyRender::_render_partial_energy(PolygonalEnergy& e)
00105 {
00106 AutoPtr<Mesh> * pAMesh = e.getMesh();
00107 if (!pAMesh) return;
00108
00109 Mesh * pMesh = pAMesh->origObject();
00110 if (!pMesh) return;
00111
00112 glEnable(GL_COLOR_MATERIAL);
00113 glDisable(GL_LIGHTING);
00114
00115 PolygonalEnergy::Regions regions = e.getRegionsDescr();
00116
00117 if (regions == PolygonalEnergy::FACET_BASED) {
00118
00119 return;
00120 }
00121
00122 if (regions == PolygonalEnergy::VERTEX_BASED) {
00123 Vector3 u,v;
00124
00125
00126 pMesh->resetActSolid();
00127 do {
00128 pMesh->resetActPlane();
00129 do {
00130 glBegin(GL_POLYGON); {
00131 pMesh->resetActEdge();
00132 do {
00133 v.set(pMesh->getActVert1(true));
00134 glVertex3f(v.x,v.y,v.z);
00135 } while (pMesh->goToNextEdge());
00136 } glEnd();
00137 } while (pMesh->goToNextPlane());
00138 } while (pMesh->goToNextSolid());
00139
00140 glColor3f(0.0,0.0,0.0);
00141 glBegin(GL_LINES); {
00142 pMesh->resetActSolid();
00143 do {
00144 pMesh->resetEdgeWalkInSolid();
00145 do {
00146 u.set(pMesh->getActVert1(false));
00147 v.set(pMesh->getActVert2(false));
00148 u.scale(1.002);
00149 v.scale(1.002);
00150 glVertex3f(u.x,u.y,u.z);
00151 glVertex3f(v.x,v.y,v.z);
00152 } while (pMesh->stepInEdgeWalkInSolid());
00153 } while (pMesh->goToNextSolid());
00154 } glEnd();
00155
00156 GLfloat pointSize;
00157 GLboolean antia;
00158 glGetFloatv(GL_POINT_SIZE, &pointSize);
00159 glGetBooleanv(GL_LINE_SMOOTH, &antia);
00160
00161
00162
00163 glMatrixMode(GL_MODELVIEW);
00164
00165
00166 pMesh->resetActSolid();
00167 do {
00168 pMesh->resetVertWalkInSolid();
00169 do {
00170 u.set(pMesh->getActVert());
00171 e.getRegionEnergy(pMesh->getActVertID(), v);
00172 glColor3f(v.x,v.y,v.z);
00173
00174 glPushMatrix();
00175 glTranslatef(u.x,u.y,u.z);
00176 glutSolidSphere(.04, 10, 10);
00177 glPopMatrix();
00178 } while (pMesh->stepInVertWalkInSolid());
00179 } while (pMesh->goToNextSolid());
00180
00181
00182 glPointSize(pointSize);
00183 if (!antia) glDisable(GL_LINE_SMOOTH);
00184
00185 return;
00186 }
00187 }
00188
00189 void GLEnergyRender::_render_energy(Scene& scene)
00190 {
00191 ObjsExplorer explorer;
00192 explorer.explore(*(scene.root()));
00193
00194 AutoPtr<EnergyCoat> * pAEnergy;
00195 EnergyCoat * pEnergy;
00196 Matrix4 trMat;
00197 bool transformed;
00198 SceneGraphObject * pObj = explorer.result(trMat, transformed);
00199
00200 while (pObj) {
00201 pAEnergy = pObj->getEnergyState();
00202 if (pAEnergy) {
00203 pEnergy = pAEnergy->origObject();
00204 assert(pEnergy);
00205 PolygonalEnergy * pPE = dynamic_cast<PolygonalEnergy*>(pEnergy);
00206 if (pPE)
00207 _render_partial_energy(*pPE);
00208 else {
00209 LightMap* pLM = dynamic_cast<LightMap*>(pEnergy);
00210 if (pLM) _render_light_map(*pLM);
00211 }
00212 }
00213 pObj = explorer.result(trMat, transformed);
00214 }
00215 }
00216
00217 void GLEnergyRender::_render(Shader& shader,
00218 FrameBuffer& fb,
00219 Camera& camera,
00220 Scene& scene)
00221 {
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 CameraGL cameraGL(camera);
00233
00234 if (_useDisplayList) {
00235 if (_displayList) glCallList(_displayList);
00236 else {
00237 _displayList = glGenLists(1);
00238 glNewList(_displayList, GL_COMPILE_AND_EXECUTE); {
00239 _render_energy(scene);
00240 } glEndList();
00241 }
00242 } else
00243 _render_energy(scene);
00244 }
00245