00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef __WIN32_
00022 #include <windows.h>
00023 #endif
00024
00025 #include <GL/gl.h>
00026 #include <iostream>
00027 #include "polyhedron_gl.h"
00028 #include "util.h"
00029
00030 using namespace std;
00031 using namespace HE;
00032 using namespace Geometry;
00033
00034 Polyhedron_GL::Polyhedron_GL(const Polyhedron* p)
00035 : _poly(p), _normalLen(0.5)
00036 {
00037 #if 0
00038 cerr << "Ready to draw a Polyhedron with " << p->numFaces() << " faces (" << p->numHoles() << " holes), and "
00039 << p->numVertices() << " vertices:\n";
00040
00041 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00042 {
00043 const Face* f = *it;
00044 cerr << "Face: " << *f << endl;
00045 }
00046 #endif
00047
00048 _poly->GL(this);
00049 recomputeNormals();
00050 recomputeCentroids();
00051 recomputeColors();
00052 recomputeBoundingBox();
00053 }
00054
00055 void Polyhedron_GL::drawVertices() const
00056 {
00057
00058 glBegin(GL_POINTS);
00059 for (Polyhedron::const_vertex_iterator it=_poly->vBegin() ; it!=_poly->vEnd() ; ++it)
00060 {
00061
00062 glVertex3fv( (*it)->position().getData() );
00063 }
00064 glEnd();
00065 }
00066
00067 void Polyhedron_GL::drawCentroids() const
00068 {
00069 glBegin(GL_POINTS);
00070 for (map<const Face*,Vector3Df>::const_iterator it=_centroids.begin() ; it!=_centroids.end() ; ++it)
00071 glVertex3fv( it->second.getData() );
00072 glEnd();
00073 }
00074
00075 void Polyhedron_GL::drawFaces(const std::map<const Face*,Geometry::Vector3Df>& fnormals, bool faceColors) const
00076 {
00077 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00078 {
00079 const Face* f = *it;
00080 if (f->hole())
00081 continue;
00082 Face::const_edge_circulator e = f->begin();
00083 Face::const_edge_circulator sentinel = e;
00084
00085 glBegin(GL_POLYGON);
00086 glNormal3fv( fnormals.find(f)->second.getData() );
00087 if (faceColors)
00088 glColor3fv( _randomColors.find(f)->second.getData() );
00089 do
00090 {
00091 const Vertex* v = (*e)->dst();
00092 glVertex3fv( v->position().getData() );
00093 ++e;
00094 } while (e != sentinel) ;
00095 glEnd();
00096 }
00097 }
00098
00099
00100 void Polyhedron_GL::drawFacesWithColors(const std::map<const Vertex*,Geometry::Vector3Df>& vcolors, bool flat) const
00101 {
00102 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00103 {
00104 const Face* f = *it;
00105 if (f->hole())
00106 continue;
00107 Face::const_edge_circulator e = f->begin();
00108 Face::const_edge_circulator sentinel = e;
00109
00110 glBegin(GL_POLYGON);
00111 if (flat)
00112 glNormal3fv( _fNormals.find(f)->second.getData() );
00113 do
00114 {
00115 const Vertex* v = (*e)->dst();
00116 glColor3fv( vcolors.find(v)->second.getData() );
00117 if (!flat)
00118 glNormal3fv( _vNormals.find(v)->second.getData() );
00119 glVertex3fv( v->position().getData() );
00120 ++e;
00121 } while (e != sentinel) ;
00122 glEnd();
00123 }
00124 }
00125
00126
00127 void Polyhedron_GL::drawFacesWithTexture(const std::map<const Vertex*,Geometry::Vector3Df>& tCoords, bool flat) const
00128 {
00129 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00130 {
00131 const Face* f = *it;
00132 if (f->hole())
00133 continue;
00134 Face::const_edge_circulator e = f->begin();
00135 Face::const_edge_circulator sentinel = e;
00136
00137 glBegin(GL_POLYGON);
00138 if (flat)
00139 glNormal3fv( _fNormals.find(f)->second.getData() );
00140 do
00141 {
00142 const Vertex* v = (*e)->dst();
00143 glTexCoord2fv( tCoords.find(v)->second.getData() );
00144 if (!flat)
00145 glNormal3fv( _vNormals.find(v)->second.getData() );
00146 glVertex3fv( v->position().getData() );
00147 ++e;
00148 } while (e != sentinel) ;
00149 glEnd();
00150 }
00151 }
00152
00153
00154 void Polyhedron_GL::drawFaces(bool flat, bool faceColors) const
00155 {
00156 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00157 {
00158 const Face* f = *it;
00159 if (f->hole())
00160 continue;
00161 Face::const_edge_circulator e = f->begin();
00162 Face::const_edge_circulator sentinel = e;
00163
00164 glBegin(GL_POLYGON);
00165 if (flat)
00166 glNormal3fv( _fNormals.find(f)->second.getData() );
00167 if (faceColors)
00168 glColor3fv( _randomColors.find(f)->second.getData() );
00169 do
00170 {
00171 const Vertex* v = (*e)->dst();
00172 if (!flat)
00173 glNormal3fv( _vNormals.find(v)->second.getData() );
00174 glVertex3fv( v->position().getData() );
00175 ++e;
00176 } while (e != sentinel) ;
00177 glEnd();
00178 }
00179 }
00180
00181 void Polyhedron_GL::drawHoles() const
00182 {
00183 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00184 {
00185 const Face* f = *it;
00186 if (!f->hole())
00187 continue;
00188 Face::const_edge_circulator e = f->begin();
00189 Face::const_edge_circulator sentinel = e;
00190
00191 glBegin(GL_LINE_LOOP);
00192 do
00193 {
00194 const Vertex* v = (*e)->dst();
00195 glVertex3fv( v->position().getData() );
00196 ++e;
00197 } while (e != sentinel) ;
00198 glEnd();
00199 }
00200 }
00201
00202 void Polyhedron_GL::drawEdges() const
00203 {
00204 glBegin(GL_LINES);
00205 for(Polyhedron::const_edge_iterator it=_poly->eBegin() ; it!=_poly->eEnd() ; ++it)
00206 {
00207 const HalfEdge* e = *it;
00208 glVertex3fv( e->dst()->position().getData() );
00209 glVertex3fv( e->src()->position().getData() );
00210 }
00211 glEnd();
00212 }
00213
00214 void Polyhedron_GL::drawBoundingBox() const
00215 {
00216 glBegin(GL_LINE_LOOP);
00217 glVertex3f(_bbox[0][0], _bbox[0][1], _bbox[0][2]);
00218 glVertex3f(_bbox[1][0], _bbox[0][1], _bbox[0][2]);
00219 glVertex3f(_bbox[1][0], _bbox[1][1], _bbox[0][2]);
00220 glVertex3f(_bbox[0][0], _bbox[1][1], _bbox[0][2]);
00221 glEnd();
00222
00223 glBegin(GL_LINE_LOOP);
00224 glVertex3f(_bbox[0][0], _bbox[0][1], _bbox[1][2]);
00225 glVertex3f(_bbox[1][0], _bbox[0][1], _bbox[1][2]);
00226 glVertex3f(_bbox[1][0], _bbox[1][1], _bbox[1][2]);
00227 glVertex3f(_bbox[0][0], _bbox[1][1], _bbox[1][2]);
00228 glEnd();
00229
00230 glBegin(GL_LINES);
00231 glVertex3f(_bbox[0][0], _bbox[0][1], _bbox[0][2]);
00232 glVertex3f(_bbox[0][0], _bbox[0][1], _bbox[1][2]);
00233
00234 glVertex3f(_bbox[1][0], _bbox[0][1], _bbox[0][2]);
00235 glVertex3f(_bbox[1][0], _bbox[0][1], _bbox[1][2]);
00236
00237 glVertex3f(_bbox[1][0], _bbox[1][1], _bbox[0][2]);
00238 glVertex3f(_bbox[1][0], _bbox[1][1], _bbox[1][2]);
00239
00240 glVertex3f(_bbox[0][0], _bbox[1][1], _bbox[0][2]);
00241 glVertex3f(_bbox[0][0], _bbox[1][1], _bbox[1][2]);
00242 glEnd();
00243 }
00244
00245 void Polyhedron_GL::drawNormals() const
00246 {
00247 glBegin(GL_LINES);
00248 for(Polyhedron::const_vertex_iterator it=_poly->vBegin() ; it!=_poly->vEnd() ; ++it)
00249 {
00250 const Vertex* v = *it;
00251 Vector3Df pos(v->position());
00252 Vector3Df away(pos + (_vNormals.find(v)->second)*_normalLen);
00253 glVertex3fv( pos.getData() );
00254 glVertex3fv( away.getData() );
00255 }
00256 glEnd();
00257 }
00258
00259 void Polyhedron_GL::drawFaceNormals() const
00260 {
00261 glBegin(GL_LINES);
00262 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00263 {
00264 const Face* f = *it;
00265 Vector3Df pos(_centroids.find(f)->second);
00266 Vector3Df away(pos + (_fNormals.find(f)->second)*_normalLen);
00267 glVertex3fv( pos.getData() );
00268 glVertex3fv( away.getData() );
00269 }
00270 glEnd();
00271 }
00272
00273 void Polyhedron_GL::drawFaceNormals(const map<const Face*,Vector3Df>& fn) const
00274 {
00275 glBegin(GL_LINES);
00276
00277 for (map<const Face*,Vector3Df>::const_iterator it=fn.begin() ; it!=fn.end() ; ++it)
00278 {
00279 const Face* f = it->first;
00280 Vector3Df pos(_centroids.find(f)->second);
00281 Vector3Df away(pos + (it->second)*_normalLen);
00282 glVertex3fv( pos.getData() );
00283 glVertex3fv( away.getData() );
00284 }
00285 glEnd();
00286 }
00287
00288
00289
00290 void Polyhedron_GL::drawTriangles(bool flat, bool faceColors) const
00291 {
00292 glBegin(GL_TRIANGLES);
00293 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00294 {
00295 const Face* f = *it;
00296 if (f->hole())
00297 continue;
00298 Face::const_edge_circulator e = f->begin();
00299
00300 if (flat)
00301 glNormal3fv( _fNormals.find(f)->second.getData() );
00302 if (faceColors)
00303 glColor3fv( _randomColors.find(f)->second.getData() );
00304 for (int i=0 ; i<3 ; i++, ++e)
00305 {
00306 const Vertex* v = (*e)->dst();
00307 if (flat)
00308 glNormal3fv( _vNormals.find(v)->second.getData() );
00309 glVertex3fv( v->position().getData() );
00310 }
00311 }
00312 glEnd();
00313 }
00314
00315 void Polyhedron_GL::recomputeNormals()
00316 {
00317 _vNormals.clear();
00318 for (Polyhedron::const_vertex_iterator it=_poly->vBegin() ; it!=_poly->vEnd() ; ++it)
00319 {
00320 const Vertex* v = *it;
00321 const Vector3Df n = v->normal();
00322 _vNormals[v] = n;
00323 }
00324
00325 _fNormals.clear();
00326 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00327 {
00328 const Face* f = *it;
00329 const Vector3Df n = f->normal();
00330 _fNormals[f] = n;
00331 }
00332 }
00333
00334
00335 void Polyhedron_GL::recomputeCentroids()
00336 {
00337 _centroids.clear();
00338 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00339 {
00340 const Face* f = *it;
00341 const Vector3Df c = f->centroid();
00342 _centroids[f] = c;
00343 }
00344 }
00345
00346
00347
00348 void Polyhedron_GL::recomputeColors()
00349 {
00350 _randomColors.clear();
00351 for(Polyhedron::const_face_iterator it=_poly->fBegin() ; it!=_poly->fEnd() ; ++it)
00352 {
00353 Vector3Df c(HE::rangerand(0, 1), HE::rangerand(0, 1), HE::rangerand(0, 1));
00354 c.normalize();
00355
00356 const Face* f = *it;
00357 _randomColors[f] = c;
00358 }
00359 }
00360
00361
00362 void Polyhedron_GL::recomputeBoundingBox()
00363 {
00364 _bbox[0] = _bbox[1] = (*_poly->vBegin())->position();
00365 for (Polyhedron::const_vertex_iterator it=_poly->vBegin() ; it!=_poly->vEnd() ; ++it)
00366 {
00367 const Vector3Df& v = (*it)->position();
00368 for (int i=0 ; i<3 ; i++)
00369 {
00370 if (v[i] < _bbox[0][i])
00371 _bbox[0][i] = v[i];
00372 if (v[i] > _bbox[1][i])
00373 _bbox[1][i] = v[i];
00374 }
00375 }
00376 }
00377