TAPs 0.7.7.3
TAPsOpenGLVolPresTriModel.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLVolPresTriModel.cpp
00003 
00004 Class TAPsOpenGLVolPresTriModel is for a deformable triangulated model with 
00005 volume preservation.
00006 
00007 SUKITTI PUNAK   (11/02/2004)
00008 UPDATE          (11/02/2004)
00009 ******************************************************************************/
00010 #include "TAPsOpenGLVolPresTriModel.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 OpenGLVolPresTriModel<T>::OpenGLVolPresTriModel()
00021     : VolPresTriModel<T>(), bRunSimulation( false )
00022 {
00023     #ifdef  TAPs_DEBUG_MODE
00024     std::cout << "OpenGLVolPresTriModel<" << typeid(T).name() << "> constructor\n";
00025     #endif//TAPs_DEBUG_MODE
00026 }
00027 //-----------------------------------------------------------------------------
00028 // destructor
00029 template <typename T>
00030 OpenGLVolPresTriModel<T>::~OpenGLVolPresTriModel()
00031 {
00032     #ifdef  TAPs_DEBUG_MODE
00033     std::cout << "OpenGLVolPresTriModel<" << typeid(T).name() << "> destructor\n";
00034     #endif//TAPs_DEBUG_MODE
00035 }
00036 /*
00037 //-----------------------------------------------------------------------------
00038 //void DisplayGL()
00039 template <typename T>
00040 void OpenGLVolPresTriModel<T>::DisplayGL( OpenGL::Enum::DrawMode DM )
00041 {
00042     switch (DM) {
00043     case OpenGL::Enum::POLYGON:
00044         DrawGL( GL_POLYGON );
00045         break;
00046     case OpenGL::Enum::WIRE_FRAME:
00047         DrawGL( GL_LINE_LOOP );
00048         break;
00049     case OpenGL::Enum::POINT:
00050         DrawGL( GL_POINTS );
00051         break;
00052     case OpenGL::Enum:: POLYGON_WITH_WIRE_FRAME:
00053         // Draw the object with GL_POLYGON_OFFSET enabled
00054         glEnable( GL_POLYGON_OFFSET_FILL );
00055         glPolygonOffset( 1.0, 1.0 );
00056         DrawGL( GL_POLYGON );
00057         glDisable( GL_POLYGON_OFFSET_FILL );
00058         // Draw the wire frame of the object
00059         glDisable( GL_LIGHTING );
00060         glColor3ub( 255, 200, 0 );
00061         DrawGL( GL_LINE_LOOP );
00062         glEnable( GL_LIGHTING );
00063         break;
00064     }
00065 }
00066 //*/
00067 //-----------------------------------------------------------------------------
00068 //void DisplayGLText()
00069 template <typename T>
00070 void OpenGLVolPresTriModel<T>::DisplayGLText( T x, T y, T z )
00071 {
00072     char str[128] = "";
00073     CalVolume(); // for calculating centroid
00074     sprintf( str, "Centroid: (%10.4f, %10.4f, %10.4f)", m_vtCentroid[0], m_vtCentroid[1], m_vtCentroid[2] );
00075     DrawText( str, x, y, z );
00076     sprintf( str, "Volume:  %g", m_tVolume );
00077     DrawText( str, x, y-2, z );
00078     sprintf( str, "weight0: %g", weight[0] );
00079     DrawText( str, x, y-4, z );
00080     sprintf( str, "weight1: %g", weight[1] );
00081     DrawText( str, x, y-6, z );
00082     sprintf( str, "weight2: %g", weight[2] );
00083     DrawText( str, x, y-8, z );
00084     sprintf( str, "weight3: %g", weight[3] );
00085     DrawText( str, x, y-10, z );
00086 }
00087 //-----------------------------------------------------------------------------
00088 // Helper Fn
00089 // void DrawGL()
00090 template <typename T>
00091 void OpenGLVolPresTriModel<T>::DrawGL( GLenum drawMode )
00092 {
00093     // StepSimulation
00094     int count;
00095     if ( bRunSimulation ) {
00097         for ( count = 0; count < 10; count++ ) {
00098             StepSimulation( 0.005 );
00099         }
00100         //*/
00101         
00102         /*
00103         for ( int i = 0; i < 5; ++i ) {
00104             for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) {
00105                 m_prXVertex[ m_prFace[i].GetVertexNo(j) ].SetPosition( 
00106                     m_prXVertex[ m_prFace[i].GetVertexNo(j) ].GetPosition() * 1.001
00107                 );
00108                 //m_prXVertex[ 0].SetPosition( m_prXVertex[ 0].GetPosition() * 1.001 );
00109                 //m_prXVertex[12].SetPosition( m_prXVertex[12].GetPosition() * 1.001 );
00110                 //m_prXVertex[13].SetPosition( m_prXVertex[13].GetPosition() * 1.001 );
00111             }
00112         }
00113         */
00114     }
00115 
00116     if ( isFacetShading ) {
00117         // Draw the object
00118         //glEnable( GL_TEXTURE_2D );
00119         for ( int i = 0; i < m_iNoFaces; i++ )
00120         {
00121             glBegin( drawMode );
00122             for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j )
00123             {
00124                 // Draw texture
00125                 if ( m_prFace[i].GetNoTexCoords() != 0 ) {
00126                     glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 
00127                                 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) );
00128                 }
00129                 // Normal of the face
00130                 glNormal3f ( static_cast<float>( m_prFace[i].GetNormal()[0] ), 
00131                              static_cast<float>( m_prFace[i].GetNormal()[1] ), 
00132                              static_cast<float>( m_prFace[i].GetNormal()[2] ) );
00133                 // Draw the vertex
00134                 glVertex3f ( 
00135                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 
00136                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 
00137                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) );
00138             }
00139             glEnd();
00140         }
00141         //glDisable( GL_TEXTURE_2D );
00142     }
00143     else {
00144         // Draw the object
00145         //glEnable( GL_TEXTURE_2D );
00146         for ( int i = 0; i < m_iNoFaces; i++ )
00147         {
00148             glBegin( drawMode );
00149             for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j )
00150             {
00151                 // Draw texture
00152                 if ( m_prFace[i].GetNoTexCoords() != 0 ) {
00153                     glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 
00154                                   static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) );
00155                 }
00156                 // Normal of the vertex
00157                 glNormal3f ( static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[0] ), 
00158                              static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[1] ), 
00159                              static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[2] ) ); 
00160                 // Draw the vertex
00161                 glVertex3f ( 
00162                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 
00163                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 
00164                     static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) );
00165             }
00166             glEnd();
00167         }
00168         //glDisable( GL_TEXTURE_2D );
00169     }
00170 
00171     // Display vertex normals as red lines
00172     DisplayGL_ParticleNormals();
00173 
00174     // Display Volume and Centroid
00175     glDisable( GL_LIGHTING );
00176     glPushAttrib(GL_ALL_ATTRIB_BITS);
00177     glColor3ub( 200, 185, 0 );
00178 
00179     PreserveVolume( weight );
00180 
00181     glPopAttrib();
00182     glEnable( GL_LIGHTING );
00183 }
00184 
00185 //=============================================================================
00186 // FOR SIMULATION
00187 //-----------------------------------------------------------------------------
00188 // Display Particle Normals Via OpenGL
00189 template <typename T>
00190 void OpenGLVolPresTriModel<T>::DisplayGL_ParticleNormals()
00191 {
00192     T x, y, z;
00193 
00194     // Draw Normal of each particle
00195     glDisable( GL_LIGHTING );
00196     glColor3f( 1.0f, 0.0f, 0.0f );
00197     glBegin( GL_LINES );
00198     for ( int n = 0; n < m_iNoVertices; ++n ){
00199         m_pprParticleVertex[n]->GetPosition().GetXYZ( x, y, z );
00200         glVertex3f( x, y, z );
00201         glVertex3f( x + m_pprParticleVertex[n]->GetNormal()[0], 
00202                     y + m_pprParticleVertex[n]->GetNormal()[1],
00203                     z + m_pprParticleVertex[n]->GetNormal()[2] );
00204     }
00205     glEnd();
00206     glEnable( GL_LIGHTING );
00207 }
00208 //-----------------------------------------------------------------------------
00209 //=============================================================================
00210 END_NAMESPACE_TAPs__OpenGL
00211 //-----------------------------------------------------------------------------
00212 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00213 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines