![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLVolPresPolygonalModel.cpp 00003 00004 Class TAPsOpenGLVolPresPolygonalModel is for a deformable mesh model with 00005 volume preservation. 00006 00007 SUKITTI PUNAK (11/11/2004) 00008 UPDATE (11/15/2004) 00009 ******************************************************************************/ 00010 #include "TAPsOpenGLVolPresPolygonalModel.hpp" 00011 // Using Inclusion Model (i.e. definitions are included in declarations) 00012 // (this name.cpp is included in name.hpp) 00013 // Each friend is defined directly inside its declaration. 00014 00015 BEGIN_NAMESPACE_TAPs__OpenGL 00016 //============================================================================= 00017 //----------------------------------------------------------------------------- 00018 // default constructor 00019 template <typename T> 00020 OpenGLVolPresPolygonalModel<T>::OpenGLVolPresPolygonalModel() 00021 : VolPresPolygonalModel<T>(), 00022 bRunSimulation( false ), 00023 bFlagShowVertexNormals( false ), 00024 bFlagShowVertexRings( false ) 00025 { 00026 #ifdef TAPs_DEBUG_MODE 00027 std::cout << "OpenGLVolPresPolygonalModel<" << typeid(T).name() << "> constructor\n"; 00028 #endif//TAPs_DEBUG_MODE 00029 } 00030 //----------------------------------------------------------------------------- 00031 // destructor 00032 template <typename T> 00033 OpenGLVolPresPolygonalModel<T>::~OpenGLVolPresPolygonalModel() 00034 { 00035 #ifdef TAPs_DEBUG_MODE 00036 std::cout << "OpenGLVolPresPolygonalModel<" << typeid(T).name() << "> destructor\n"; 00037 #endif//TAPs_DEBUG_MODE 00038 } 00039 /* 00040 //----------------------------------------------------------------------------- 00041 //void DisplayGL() 00042 template <typename T> 00043 void OpenGLVolPresPolygonalModel<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 glColor3ub( 255, 200, 0 ); 00064 DrawGL( GL_LINE_LOOP ); 00065 glEnable( GL_LIGHTING ); 00066 break; 00067 } 00068 } 00069 //*/ 00070 //----------------------------------------------------------------------------- 00071 //void DisplayGLText() 00072 template <typename T> 00073 void OpenGLVolPresPolygonalModel<T>::DisplayGLText( T x, T y, T z ) 00074 { 00075 char str[128] = ""; 00076 int count = 0; 00077 T volume = CalVolume(); // for calculating centroid 00078 sprintf( str, "Centroid: (%10.4f, %10.4f, %10.4f)", m_vtCentroid[0], m_vtCentroid[1], m_vtCentroid[2] ); 00079 Fn::DrawGLText( str, x, y-count++, z ); 00080 sprintf( str, "New Volume: %g", volume ); 00081 Fn::DrawGLText( str, x, y-count++, z ); 00082 sprintf( str, "Orig Volume: %g", m_tVolume ); 00083 Fn::DrawGLText( str, x, y-count++, z ); 00084 sprintf( str, "weight0: %g", m_weight[0] ); 00085 Fn::DrawGLText( str, x, y-count++, z ); 00086 // sprintf( str, "weight1: %g", weight[1] ); 00087 // Fn::DrawGLText( str, x, y-2*count++, z ); 00088 // sprintf( str, "weight2: %g", weight[2] ); 00089 // DrawGLText( str, x, y-2*count++, z ); 00090 // sprintf( str, "weight3: %g", weight[3] ); 00091 // DrawGLText( str, x, y-2*count++, z ); 00092 if ( m_bFlagSpringForceMode ) { 00093 sprintf( str, "Spring Force: ON" ); 00094 } 00095 else { 00096 sprintf( str, "Spring Force: OFF" ); 00097 } 00098 Fn::DrawGLText( str, x, y-count++, z ); 00099 if ( m_bFlagVolPresMode ) { 00100 sprintf( str, "VolPres: ON" ); 00101 } 00102 else { 00103 sprintf( str, "VolPres: OFF" ); 00104 } 00105 Fn::DrawGLText( str, x, y-count++, z ); 00106 } 00107 //----------------------------------------------------------------------------- 00108 // Helper Fn 00109 // void DrawGL() 00110 template <typename T> 00111 void OpenGLVolPresPolygonalModel<T>::DrawGL ( GLenum drawMode ) 00112 { 00113 glPushMatrix(); 00114 00115 00116 glTranslatef( static_cast<float>( GetTransform().GetTranslation()[0] ), 00117 static_cast<float>( GetTransform().GetTranslation()[1] ), 00118 static_cast<float>( GetTransform().GetTranslation()[2] ) ); 00119 /*glRotatef( static_cast<float>( Rotation[0] ), 00120 static_cast<float>( 1 ), 00121 static_cast<float>( 0 ), 00122 static_cast<float>( 0 ) ); 00123 glRotatef( static_cast<float>( Rotation[1] ), 00124 static_cast<float>( 0 ), 00125 static_cast<float>( 1 ), 00126 static_cast<float>( 0 ) ); 00127 glRotatef( static_cast<float>( Rotation[2] ), 00128 static_cast<float>( 0 ), 00129 static_cast<float>( 0 ), 00130 static_cast<float>( 1 ) ); 00131 glScalef( static_cast<float>( Scale[0]/2 ), 00132 static_cast<float>( Scale[1]/2 ), 00133 static_cast<float>( Scale[2] ) ); 00134 */ 00135 //ModelTransformationByTranslationRatationAndScale(); 00136 00137 //------------------------------------------------------------------------- 00138 // StepSimulation 00139 int count; 00140 if ( bRunSimulation ) { 00142 for ( count = 0; count < 5; count++ ) { 00143 StepSimulation( 0.01 ); 00144 } 00145 //*/ 00146 00147 /* 00148 for ( int i = 0; i < 5; ++i ) { 00149 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) { 00150 m_prXVertex[ m_prFace[i].GetVertexNo(j) ].SetPosition( 00151 m_prXVertex[ m_prFace[i].GetVertexNo(j) ].GetPosition() * 1.001 00152 ); 00153 //m_prXVertex[ 0].SetPosition( m_prXVertex[ 0].GetPosition() * 1.001 ); 00154 //m_prXVertex[12].SetPosition( m_prXVertex[12].GetPosition() * 1.001 ); 00155 //m_prXVertex[13].SetPosition( m_prXVertex[13].GetPosition() * 1.001 ); 00156 } 00157 } 00158 */ 00159 } 00160 00161 if ( isFacetShading ) { 00162 // Draw the object 00163 //glEnable( GL_TEXTURE_2D ); 00164 for ( int i = 0; i < m_iNoFaces; i++ ) 00165 { 00166 glBegin( drawMode ); 00167 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00168 { 00169 // Draw texture 00170 if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00171 glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00172 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00173 } 00174 // Normal of the face 00175 glNormal3f ( static_cast<float>( m_prFace[i].GetNormal()[0] ), 00176 static_cast<float>( m_prFace[i].GetNormal()[1] ), 00177 static_cast<float>( m_prFace[i].GetNormal()[2] ) ); 00178 // Draw the vertex 00179 glVertex3f ( 00180 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00181 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00182 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00183 } 00184 glEnd(); 00185 } 00186 //glDisable( GL_TEXTURE_2D ); 00187 } 00188 else { 00189 // Draw the object 00190 //glEnable( GL_TEXTURE_2D ); 00191 for ( int i = 0; i < m_iNoFaces; i++ ) 00192 { 00193 glBegin( drawMode ); 00194 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00195 { 00196 // Draw texture 00197 if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00198 glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00199 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00200 } 00201 // Normal of the vertex 00202 glNormal3f ( static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[0] ), 00203 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[1] ), 00204 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[2] ) ); 00205 // Draw the vertex 00206 glVertex3f ( 00207 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00208 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00209 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00210 } 00211 glEnd(); 00212 } 00213 //glDisable( GL_TEXTURE_2D ); 00214 } 00215 00216 // Display vertex normals 00217 if ( bFlagShowVertexNormals ) { 00218 DisplayGL_ParticleNormals(); 00219 } 00220 // Display vertex rings 00221 if ( bFlagShowVertexRings ) { 00222 DisplayGL_VertexRings(); 00223 } 00224 // Display the deformed face 00225 if ( bFlagShowDeformedFace ) { 00226 DisplayGL_DeformedFaceNo(); 00227 } 00228 00229 // Display Volume and Centroid 00230 glDisable( GL_LIGHTING ); 00231 glPushAttrib(GL_ALL_ATTRIB_BITS); 00232 glColor3ub( 200, 185, 0 ); 00233 00234 // Draw Bounding Volume(s) 00235 //DrawBoundingAABB(); 00236 //DrawBoundingSphere(); 00237 00238 glPopAttrib(); 00239 glEnable( GL_LIGHTING ); 00240 00241 glPopMatrix(); 00242 } 00243 00244 //============================================================================= 00245 // FOR SIMULATION 00246 //----------------------------------------------------------------------------- 00247 // Display Particle Normals Via OpenGL 00248 template <typename T> 00249 void OpenGLVolPresPolygonalModel<T>::DisplayGL_ParticleNormals() 00250 { 00251 T x, y, z; 00252 00253 // Draw Normal of each particle 00254 glDisable( GL_LIGHTING ); 00255 glColor3f( 1.0f, 0.0f, 0.0f ); 00256 glBegin( GL_LINES ); 00257 for ( int n = 0; n < m_iNoVertices; ++n ){ 00258 m_pprParticleVertex[n]->GetPosition().GetXYZ( x, y, z ); 00259 glVertex3f( static_cast<float>( x ), static_cast<float>( y ), static_cast<float>( z ) ); 00260 glVertex3f ( 00261 static_cast<float>( x + m_pprParticleVertex[n]->GetNormal()[0] ), 00262 static_cast<float>( y + m_pprParticleVertex[n]->GetNormal()[1] ), 00263 static_cast<float>( z + m_pprParticleVertex[n]->GetNormal()[2] ) 00264 ); 00265 } 00266 glEnd(); 00267 glEnable( GL_LIGHTING ); 00268 } 00269 //----------------------------------------------------------------------------- 00270 // Display Vertex Rings Via OpenGL 00271 template <typename T> 00272 void OpenGLVolPresPolygonalModel<T>::DisplayGL_VertexRings() 00273 { 00274 glDisable( GL_LIGHTING ); 00275 //glLineWidth( 3.0f ); 00276 glPointSize( 7.0f ); 00277 for ( int i = 0; i <= m_pcVertexRings->GetNoRings(); ++i ) { 00278 glColor3ub( 255-(i*25), 0+(i*25), 15 ); 00279 //glBegin( GL_LINE_LOOP ); 00280 glBegin( GL_POINTS ); 00281 for ( int j = m_pcVertexRings->GetRingLayers()[i]; j < m_pcVertexRings->GetRingLayers()[i+1]; ++j ) { 00282 glVertex3f ( 00283 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][0] ), 00284 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][1] ), 00285 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][2] ) 00286 ); 00287 } 00288 glEnd(); 00289 } 00290 glPointSize( 1.0f ); 00291 //glLineWidth( 1.0f ); 00292 glEnable( GL_LIGHTING ); 00293 } 00294 //----------------------------------------------------------------------------- 00295 // Display The Deformed Face Via OpenGL 00296 template <typename T> 00297 void OpenGLVolPresPolygonalModel<T>::DisplayGL_DeformedFaceNo () 00298 { 00299 // Draw Normal of each particle 00300 glDisable( GL_LIGHTING ); 00301 glColor3f( 0.0f, 1.0f, 0.0f ); 00302 glBegin( GL_POLYGON ); 00303 for ( int n = 0; n < m_prFace[m_iDeformedFaceNo].GetNoVertices(); ++n ){ 00304 glVertex3f ( 00305 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][0] ), 00306 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][1] ), 00307 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][2] ) 00308 ); 00309 } 00310 glEnd(); 00311 glEnable( GL_LIGHTING ); 00312 } 00313 //----------------------------------------------------------------------------- 00314 //============================================================================= 00315 END_NAMESPACE_TAPs__OpenGL 00316 //----------------------------------------------------------------------------- 00317 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00318 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8