![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLHalfEdgeTrigonalModel.cpp 00003 00004 Class OpenGLHalfEdgeTrigonalModel is for creating an OpenGL Trigonal Model 00005 using half-edge data structure. 00006 00007 See class HalfEdgeModel (in "../OpenGLModel/TAPsHalfEdgeTrigonalModel.hpp") and 00008 class OpenGLSupport ("../OpenGL/TAPsOpenGLSupport.hpp") for details. 00009 00010 SUKITTI PUNAK (06/15/2006) 00011 UPDATE (06/15/2006) 00012 ******************************************************************************/ 00013 #include "TAPsOpenGLHalfEdgeTrigonalModel.hpp" 00014 // Using Inclusion Model (i.e. definitions are included in declarations) 00015 // (this name.cpp is included in name.hpp) 00016 // Each friend is defined directly inside its declaration. 00017 00018 BEGIN_NAMESPACE_TAPs__OpenGL 00019 //============================================================================= 00020 //----------------------------------------------------------------------------- 00021 // default constructor 00022 template <typename T> 00023 OpenGLHalfEdgeTrigonalModel<T>::OpenGLHalfEdgeTrigonalModel() 00024 : HalfEdgeTrigonalModel<T>(), bIsDisplayVertexNormals( false ) 00025 { 00026 #ifdef TAPs_DEBUG_MODE 00027 std::cout << "OpenGLHalfEdgeTrigonalModel<" << typeid(T).name() << "> constructor\n"; 00028 #endif//TAPs_DEBUG_MODE 00029 } 00030 //----------------------------------------------------------------------------- 00031 // destructor 00032 template <typename T> 00033 OpenGLHalfEdgeTrigonalModel<T>::~OpenGLHalfEdgeTrigonalModel() 00034 { 00035 #ifdef TAPs_DEBUG_MODE 00036 std::cout << "OpenGLHalfEdgeTrigonalModel<" << typeid(T).name() << "> destructor\n"; 00037 #endif//TAPs_DEBUG_MODE 00038 } 00039 /* 00040 //----------------------------------------------------------------------------- 00041 //void DisplayGL() 00042 template <typename T> 00043 void OpenGLHalfEdgeTrigonalModel<T>::DisplayGL( OpenGL::Enum::DrawMode DM ) 00044 { 00045 switch (DM) { 00046 case OpenGL::Enum::POLYGON: 00047 DrawGL( GL_POLYGON ); 00048 break; 00049 case OpenGL::Enum::WIRE_FRAME: 00050 DrawGL( GL_LINE_LOOP ); 00051 break; 00052 case OpenGL::Enum::POINT: 00053 DrawGL( GL_POINTS ); 00054 break; 00055 case OpenGL::Enum:: POLYGON_WITH_WIRE_FRAME: 00056 // Draw the object with GL_POLYGON_OFFSET enabled 00057 glEnable( GL_POLYGON_OFFSET_FILL ); 00058 glPolygonOffset( 1.0, 1.0 ); 00059 DrawGL( GL_POLYGON ); 00060 glDisable( GL_POLYGON_OFFSET_FILL ); 00061 // Draw the wire frame of the object 00062 glDisable( GL_LIGHTING ); 00063 //glDisable( GL_DEPTH_TEST ); 00064 glColor3ub( 255, 200, 0 ); 00065 DrawGL( GL_LINE_LOOP ); 00066 glEnable( GL_LIGHTING ); 00067 //glEnable( GL_DEPTH_TEST ); 00068 break; 00069 } 00070 } 00071 //*/ 00072 //----------------------------------------------------------------------------- 00073 // Helper Fn 00074 // void DrawGL() 00075 template <typename T> 00076 void OpenGLHalfEdgeTrigonalModel<T>::DrawGL( GLenum drawMode ) 00077 { 00078 //---------------------------------------------------------------- 00079 if ( drawMode == OpenGL::Enum::POINT ) { 00080 HEVertex<T> *ptr = m_listVertex->Head(); 00081 glBegin( drawMode ); 00082 while ( ptr != NULL ) { 00083 // Draw the vertex 00084 glVertex3f ( 00085 static_cast<float>( (*ptr)[0] ), 00086 static_cast<float>( (*ptr)[1] ), 00087 static_cast<float>( (*ptr)[2] ) ); 00088 ptr = ptr->Next(); 00089 } 00090 glEnd(); 00091 } 00092 else if ( isFacetShading ) { 00093 // Draw the object 00094 //glEnable( GL_TEXTURE_2D ); 00095 HEFace<T> *facePtr = m_listFace->Head(); 00096 HEHalfEdge<T> *halfEdgePtr; 00097 //for ( int f = 0; f < GetNoFaces(); ++f ) { 00098 while ( facePtr != NULL ) { 00099 halfEdgePtr = facePtr->IncidentHalfEdge(); 00100 glBegin( drawMode ); 00101 //for ( int i = 0; i < 3; ++i ) { 00102 do { 00104 //if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00105 // glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00106 // static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00107 //} 00108 // Normal of the face 00109 glNormal3f ( 00110 static_cast<float>( facePtr->GetNormal()[0] ), 00111 static_cast<float>( facePtr->GetNormal()[1] ), 00112 static_cast<float>( facePtr->GetNormal()[2] ) 00113 ); 00114 // Normal of the vertex 00115 // Draw the vertex 00116 glVertex3f ( 00117 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[0] ), 00118 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[1] ), 00119 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[2] ) 00120 ); 00121 // DEBUG: 00122 //std::cout << halfEdgePtr->Vertex()->GetPosition()[0]; 00123 //std::cout << halfEdgePtr->Vertex()->GetPosition()[1]; 00124 //std::cout << halfEdgePtr->Vertex()->GetPosition()[2]; 00125 //std::cout << "\n"; 00126 halfEdgePtr = halfEdgePtr->Next(); 00127 } while ( halfEdgePtr != facePtr->IncidentHalfEdge() ); 00128 //std::cout << "\n"; 00129 //} 00130 glEnd(); 00131 facePtr = facePtr->Next(); 00132 } 00133 00134 // Draw the object 00135 //for ( int i = 0; i < m_iNoFaces; i++ ) 00136 //{ 00137 // glBegin( drawMode ); 00138 // for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00139 // { 00140 // // Draw texture 00141 // if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00142 // glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00143 // static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00144 // } 00145 // // Normal of the face 00146 // //glNormal3f ( static_cast<float>( m_prFace[i].GetNormal()[0] ), 00147 // // static_cast<float>( m_prFace[i].GetNormal()[1] ), 00148 // // static_cast<float>( m_prFace[i].GetNormal()[2] ) ); 00149 // // Draw the vertex 00150 // glVertex3f ( 00151 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00152 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00153 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00154 // } 00155 // glEnd(); 00156 //} 00158 } 00159 else { 00160 // Draw the object 00161 //glEnable( GL_TEXTURE_2D ); 00162 HEFace<T> *facePtr = m_listFace->Head(); 00163 HEHalfEdge<T> *halfEdgePtr; 00164 //for ( int f = 0; f < GetNoFaces(); ++f ) { 00165 while ( facePtr != NULL ) { 00166 halfEdgePtr = facePtr->IncidentHalfEdge(); 00167 glBegin( drawMode ); 00168 //for ( int i = 0; i < 3; ++i ) { 00169 do { 00171 //if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00172 // glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00173 // static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00174 //} 00176 glNormal3f ( 00177 static_cast<float>( halfEdgePtr->Vertex()->GetNormal()[0] ), 00178 static_cast<float>( halfEdgePtr->Vertex()->GetNormal()[1] ), 00179 static_cast<float>( halfEdgePtr->Vertex()->GetNormal()[2] ) 00180 ); 00181 // Normal of the vertex 00182 // Draw the vertex 00183 glVertex3f ( 00184 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[0] ), 00185 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[1] ), 00186 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[2] ) 00187 ); 00188 // DEBUG: 00189 //std::cout << halfEdgePtr->Vertex()->GetPosition()[0]; 00190 //std::cout << halfEdgePtr->Vertex()->GetPosition()[1]; 00191 //std::cout << halfEdgePtr->Vertex()->GetPosition()[2]; 00192 //std::cout << "\n"; 00193 halfEdgePtr = halfEdgePtr->Next(); 00194 } while ( halfEdgePtr != facePtr->IncidentHalfEdge() ); 00195 //std::cout << "\n"; 00196 //} 00197 glEnd(); 00198 facePtr = facePtr->Next(); 00199 } 00200 00203 //for ( int i = 0; i < m_iNoFaces; i++ ) 00204 //{ 00205 // glBegin( drawMode ); 00206 // for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00207 // { 00208 // // Draw texture 00209 // if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00210 // glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00211 // static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00212 // } 00213 // // Normal of the vertex 00214 // glNormal3f ( static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[0] ), 00215 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[1] ), 00216 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[2] ) ); 00217 // // Draw the vertex 00218 // glVertex3f ( 00219 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00220 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00221 // static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00222 // } 00223 // glEnd(); 00224 //} 00226 } 00227 00228 // Draw Boundary 00229 //if ( drawMode == OpenGL::Enum::POLYGON_WITH_WIRE_FRAME ) { 00230 //if ( true ) { 00231 // glColor3f( 1.0f, 0.0f, 0.0f ); 00232 // glDisable( GL_LIGHTING ); 00233 // glBegin( GL_LINES ); 00234 // HEFace<T> *facePtr = m_listFace->Head(); 00235 // HEHalfEdge<T> *halfEdgePtr; 00236 // while ( facePtr != NULL ) { 00237 // halfEdgePtr = facePtr->IncidentHalfEdge(); 00238 // do { 00239 // if ( !halfEdgePtr->Pair() ) { 00240 // // Draw the vertex 00241 // glVertex3f ( 00242 // static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[0] ), 00243 // static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[1] ), 00244 // static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[2] ) 00245 // ); 00246 // glVertex3f ( 00247 // static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[0] ), 00248 // static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[1] ), 00249 // static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[2] ) 00250 // ); 00251 // } 00252 // halfEdgePtr = halfEdgePtr->Next(); 00253 // } while ( halfEdgePtr != facePtr->IncidentHalfEdge() ); 00254 // facePtr = facePtr->Next(); 00255 // } 00256 // glEnd(); 00257 // glEnable( GL_LIGHTING ); 00258 //} 00259 00260 // Draw Boundary 00261 if ( true ) { 00262 glColor3f( 1.0f, 0.0f, 0.0f ); 00263 glPushAttrib( GL_LIGHTING_BIT ); 00264 glDisable( GL_LIGHTING ); 00265 glBegin( GL_LINES ); 00266 if ( m_listHoleFace != NULL ) { 00267 HEFace<T> *facePtr = m_listHoleFace->Head(); 00268 HEHalfEdge<T> *halfEdgePtr; 00269 while ( facePtr != NULL ) { 00270 halfEdgePtr = facePtr->IncidentHalfEdge(); 00271 do { 00272 //if ( halfEdgePtr && halfEdgePtr->Next() ) { 00273 // Draw the vertex 00274 glVertex3f ( 00275 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[0] ), 00276 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[1] ), 00277 static_cast<float>( halfEdgePtr->Vertex()->GetPosition()[2] ) 00278 ); 00279 glVertex3f ( 00280 static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[0] ), 00281 static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[1] ), 00282 static_cast<float>( halfEdgePtr->Next()->Vertex()->GetPosition()[2] ) 00283 ); 00284 //} 00285 halfEdgePtr = halfEdgePtr->Next(); 00286 } while ( halfEdgePtr && halfEdgePtr != facePtr->IncidentHalfEdge() ); 00287 facePtr = facePtr->Next(); 00288 } 00289 } 00290 glEnd(); 00291 //glEnable( GL_LIGHTING ); 00292 glPopAttrib(); 00293 } 00294 00295 static HEFace<T> * g_pFace = m_listFace->Head(); 00296 static int gCount = 0; 00297 //static int gCount2 = 0; 00298 00299 if ( bIsDisplayVertexNormals ) { 00300 ++gCount; 00301 glPushAttrib( GL_LIGHTING_BIT | GL_ENABLE_BIT ); 00302 glDisable( GL_LIGHTING ); 00303 glEnable( GL_COLOR_MATERIAL ); 00304 00305 //glColor3ub( 175, 50, 70 ); 00306 //DrawVertexNormals(); 00307 00308 /* 00309 glColor3ub( 75, 50, 175 ); 00310 DrawHalfEdgeByPrevPtrs(); 00311 00312 glColor3ub( 175, 150, 70 ); 00313 DrawHalfEdgeByNextPtrs(); 00314 //*/ 00315 00316 // while ( g_pFace ) { 00317 if ( g_pFace ) { 00318 HEHalfEdge<T> * pHalfEdge = g_pFace->IncidentHalfEdge(); 00319 HEHalfEdge<T> * pStartHalfEdge = pHalfEdge; 00320 Vector3<T> color[6]; 00321 00322 color[0] = Vector3<T>( 0.5, 0.0, 0.0 ); 00323 color[1] = Vector3<T>( 1.0, 0.5, 0.5 ); 00324 00325 color[2] = Vector3<T>( 0.0, 0.5, 0.0 ); 00326 color[3] = Vector3<T>( 0.5, 1.0, 0.5 ); 00327 00328 color[4] = Vector3<T>( 0.0, 0.0, 0.5 ); 00329 color[5] = Vector3<T>( 0.5, 0.5, 1.0 ); 00330 00331 int count = 0; 00332 do { 00333 //std::cout << ++count << "\n"; 00334 DrawHalfEdgePairs( pHalfEdge, color[count], color[count+1] ); 00335 count += 2; 00336 pHalfEdge = pHalfEdge->Next(); 00337 } while ( pHalfEdge != pStartHalfEdge ); 00338 if ( gCount % 25 == 0 ) { 00339 g_pFace = g_pFace->Next(); 00340 gCount = 0; 00341 } 00342 } 00343 else { 00344 g_pFace = m_listFace->Head(); 00345 } 00346 00347 glPopAttrib(); 00348 } 00349 00350 // Draw AABB Bounding Box 00351 //DrawBoundingAABB(); 00352 //DrawBoundingEllipsoid(); 00353 //DrawBoundingSphere(); 00354 //---------------------------------------------------------------- 00355 } 00356 //============================================================================= 00357 // Draw by OpenGL 00358 //----------------------------------------------------------------------------- 00359 template <typename T> 00360 void OpenGLHalfEdgeTrigonalModel<T>::DrawVertexNormals () 00361 { 00362 HEVertex<T> * pVertex = m_listVertex->Head(); 00363 while ( pVertex ) { 00364 OpenGLUsefulObj<T>::DrawOneHeadArrow( pVertex->GetPosition(), pVertex->GetPosition() + pVertex->GetNormal() ); 00365 pVertex = pVertex->Next(); 00366 } 00367 } 00368 //----------------------------------------------------------------------------- 00369 template <typename T> 00370 void OpenGLHalfEdgeTrigonalModel<T>::DrawHalfEdgeByNextPtrs () 00371 { 00372 HEFace<T> * pFace = m_listFace->Head(); 00373 while ( pFace ) { 00374 HEHalfEdge<T> * pHalfEdge = pFace->IncidentHalfEdge(); 00375 HEHalfEdge<T> * pNextHalfEdge = pHalfEdge->Next(); 00376 HEHalfEdge<T> * pStartHalfEdge = pHalfEdge; 00377 do { 00378 OpenGLUsefulObj<T>::DrawOneHeadArrow( 00379 pHalfEdge->Vertex()->GetPosition() + pFace->GetNormal()/5, 00380 pNextHalfEdge->Vertex()->GetPosition() + pFace->GetNormal()/5 ); 00381 pHalfEdge = pHalfEdge->Next(); 00382 pNextHalfEdge = pNextHalfEdge->Next(); 00383 } while ( pHalfEdge != pStartHalfEdge ); 00384 pFace = pFace->Next(); 00385 } 00386 } 00387 //----------------------------------------------------------------------------- 00388 template <typename T> 00389 void OpenGLHalfEdgeTrigonalModel<T>::DrawHalfEdgeByPrevPtrs () 00390 { 00391 HEFace<T> * pFace = m_listFace->Head(); 00392 while ( pFace ) { 00393 HEHalfEdge<T> * pHalfEdge = pFace->IncidentHalfEdge(); 00394 HEHalfEdge<T> * pPrevHalfEdge = pHalfEdge->Prev(); 00395 HEHalfEdge<T> * pStartHalfEdge = pHalfEdge; 00396 do { 00397 OpenGLUsefulObj<T>::DrawOneHeadArrow( 00398 pHalfEdge->Vertex()->GetPosition() + pFace->GetNormal()/10, 00399 pPrevHalfEdge->Vertex()->GetPosition() + pFace->GetNormal()/10 ); 00400 pHalfEdge = pHalfEdge->Next(); 00401 pPrevHalfEdge = pPrevHalfEdge->Next(); 00402 } while ( pHalfEdge != pStartHalfEdge ); 00403 pFace = pFace->Next(); 00404 } 00405 } 00406 //----------------------------------------------------------------------------- 00407 template <typename T> 00408 void OpenGLHalfEdgeTrigonalModel<T>::DrawHalfEdgePairs ( const HEHalfEdge<T> * pHalfEdge, Vector3<T> & color1, Vector3<T> & color2 ) 00409 { 00410 glPushAttrib( GL_LIGHTING_BIT | GL_ENABLE_BIT ); 00411 glDisable( GL_LIGHTING ); 00412 glEnable( GL_COLOR_MATERIAL ); 00413 00414 glColor3f( color1[0], color1[1], color1[2] ); 00415 OpenGLUsefulObj<T>::DrawOneHeadArrow( 00416 pHalfEdge->Vertex()->GetPosition() + pHalfEdge->Face()->GetNormal()/2, 00417 pHalfEdge->Next()->Vertex()->GetPosition() + pHalfEdge->Face()->GetNormal()/2 ); 00418 00419 HEHalfEdge<T> * pPair = pHalfEdge->Pair(); 00420 glColor3f( color2[0], color2[1], color2[2] ); 00421 OpenGLUsefulObj<T>::DrawOneHeadArrow( 00422 pPair->Vertex()->GetPosition() + pHalfEdge->Face()->GetNormal()/4, 00423 pPair->Next()->Vertex()->GetPosition() + pHalfEdge->Face()->GetNormal()/4 ); 00424 00425 glPopAttrib(); 00426 } 00427 //----------------------------------------------------------------------------- 00428 //============================================================================= 00429 END_NAMESPACE_TAPs__OpenGL 00430 //----------------------------------------------------------------------------- 00431 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00432 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8