![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLPNTriangleVolPresModel.cpp 00003 00004 (X <==> Extra) 00005 Class OpenGLPNTriangleVolPresModel is for creating an OpenGL PN-Triangle 00006 Model with volume preservation. 00007 00008 See class VolPresTriModel for details. 00009 00010 SUKITTI PUNAK (04/15/2005) 00011 UPDATE (05/25/2005) 00012 ******************************************************************************/ 00013 #include "TAPsOpenGLPNTriangleVolPresModel.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 OpenGLPNTriangleVolPresModel<T>::OpenGLPNTriangleVolPresModel() 00024 : VolPresPolygonalModel<T>(), 00025 bRunSimulation( false ), 00026 bFlagShowVertexNormals( false ), 00027 bFlagShowVertexRings( false ) 00028 { 00029 #ifdef TAPs_DEBUG_MODE 00030 std::cout << "OpenGLPNTriangleVolPresModel<" << typeid(T).name() << "> constructor\n"; 00031 #endif//TAPs_DEBUG_MODE 00032 } 00033 //----------------------------------------------------------------------------- 00034 // destructor 00035 template <typename T> 00036 OpenGLPNTriangleVolPresModel<T>::~OpenGLPNTriangleVolPresModel() 00037 { 00038 if ( m_prPNTriCoef != NULL ) delete [] m_prPNTriCoef; 00039 if ( m_prPNTriNormal != NULL ) delete [] m_prPNTriNormal; 00040 00041 #ifdef TAPs_DEBUG_MODE 00042 std::cout << "OpenGLPNTriangleVolPresModel<" << typeid(T).name() << "> destructor\n"; 00043 #endif//TAPs_DEBUG_MODE 00044 } 00045 //----------------------------------------------------------------------------- 00046 // Initialize 00047 template <typename T> 00048 void OpenGLPNTriangleVolPresModel<T>::Initialize () 00049 { 00050 VolPresPolygonalModel<T>::Initialize(); 00051 CreateAndCalPNTriangles(); 00052 } 00053 //----------------------------------------------------------------------------- 00054 // (Helper Fn) CreateAndCalPNTriangles 00055 template <typename T> 00056 void OpenGLPNTriangleVolPresModel<T>::CreateAndCalPNTriangles () { 00057 if ( !AllocatePNTriangleCoefficients() ) exit(1); 00058 if ( !AllocatePNTriangleNormals() ) exit(1); 00059 InitializePNTriangleCoefficients(); 00060 InitializePNTriangleNormals(); 00061 CalculatePNTriangleCoefficients(); 00062 CalculatePNTriangleNormals(); 00063 } 00064 //----------------------------------------------------------------------------- 00065 // (Helper Fn) AllocatePNTriangleCoefficients 00066 template <typename T> 00067 bool OpenGLPNTriangleVolPresModel<T>::AllocatePNTriangleCoefficients () { 00068 m_prPNTriCoef = new float[GetNoFaces()*10*3]; 00069 if ( m_prPNTriCoef ) return true; 00070 return false; 00071 } 00072 //----------------------------------------------------------------------------- 00073 // (Helper Fn) AllocatePNTriangleNormals 00074 template <typename T> 00075 bool OpenGLPNTriangleVolPresModel<T>::AllocatePNTriangleNormals () { 00076 m_prPNTriNormal = new float[GetNoFaces()*6*3]; 00077 if ( m_prPNTriNormal ) return true; 00078 return false; 00079 } 00080 //----------------------------------------------------------------------------- 00081 // (Helper Fn) InitializePNTriangleCoefficients 00082 template <typename T> 00083 void OpenGLPNTriangleVolPresModel<T>::InitializePNTriangleCoefficients () { 00084 // Coefficients or Control Points 00085 // b003 9 00086 // / \ 00087 // / \ 00088 // b102 b012 7 8 00089 // / \ <===> 00090 // / \ 00091 // b201 b111 b021 4 5 6 00092 // / \ 00093 // / \ 00094 // b300 ---- b210 ---- b120 ---- b030 0 1 2 3 00095 int b = 0; 00096 Vector3<T> *P1, *P2, *P3; 00097 for ( int f = 0; f < GetNoFaces(); ++f, b+=30 ) { 00098 // Initialize b300, b030, and b003 00099 P1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetPosition(); 00100 P2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetPosition(); 00101 P3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetPosition(); 00102 for ( int i = 0; i < 3; ++i ) { 00103 *(m_prPNTriCoef+b+i) = (*P1)[i]; 00104 *(m_prPNTriCoef+b+9+i) = (*P2)[i]; 00105 *(m_prPNTriCoef+b+27+i) = (*P3)[i]; 00106 } 00107 } 00108 } 00109 //----------------------------------------------------------------------------- 00110 // (Helper Fn) InitializePNTriangleNormals 00111 template <typename T> 00112 void OpenGLPNTriangleVolPresModel<T>::InitializePNTriangleNormals () { 00113 // Normals 00114 // n002 5 00115 // / \ 00116 // / \ 00117 // n101 n011 <===> 3 4 00118 // / \ 00119 // / \ 00120 // n200 ---- n110 ---- n020 0 1 2 00121 int n = 0; 00122 Vector3<T> *N1, *N2, *N3; 00123 for ( int f = 0; f < GetNoFaces(); ++f, n+=18 ) { 00124 // Initialize n200, n020, and n002 00125 N1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetNormal(); 00126 N2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetNormal(); 00127 N3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetNormal(); 00128 for ( int i = 0; i < 3; ++i ) { 00129 *(m_prPNTriNormal+n+i) = (*N1)[i]; 00130 *(m_prPNTriNormal+n+6+i) = (*N2)[i]; 00131 *(m_prPNTriNormal+n+15+i) = (*N3)[i]; 00132 } 00133 } 00134 } 00135 //----------------------------------------------------------------------------- 00136 // (Helper Fn) CalculatePNTriangleVertices 00137 template <typename T> 00138 void OpenGLPNTriangleVolPresModel<T>::CalculatePNTriangleCoefficients () { 00139 // Coefficients or Control Points 00140 // b003 9 00141 // / \ 00142 // / \ 00143 // b102 b012 7 8 00144 // / \ <===> 00145 // / \ 00146 // b201 b111 b021 4 5 6 00147 // / \ 00148 // / \ 00149 // b300 ---- b210 ---- b120 ---- b030 0 1 2 3 00150 int b = 0; 00151 Vector3<T> *P1, *P2, *P3; 00152 Vector3<T> *N1, *N2, *N3; 00153 Vector3<T> temp, sum; 00154 T w12, w21, w23, w32, w31, w13; 00155 for ( int f = 0; f < GetNoFaces(); ++f ) { 00156 P1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetPosition(); 00157 P2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetPosition(); 00158 P3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetPosition(); 00159 N1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetNormal(); 00160 N2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetNormal(); 00161 N3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetNormal(); 00162 // Calculate w12, w23, and w31 00163 w12 = ( (*P2) - (*P1) ) * (*N1); 00164 w21 = ( (*P1) - (*P2) ) * (*N2); 00165 w23 = ( (*P3) - (*P2) ) * (*N2); 00166 w32 = ( (*P2) - (*P3) ) * (*N3); 00167 w31 = ( (*P1) - (*P3) ) * (*N3); 00168 w13 = ( (*P3) - (*P1) ) * (*N1); 00169 // b210 00170 b += 3; 00171 sum = temp = (2*(*P1) + (*P2) - w12*(*N1)) / 3.0; 00172 *(m_prPNTriCoef+b) = temp[0]; 00173 *(m_prPNTriCoef+b+1) = temp[1]; 00174 *(m_prPNTriCoef+b+2) = temp[2]; 00175 // b120 00176 b += 3; 00177 sum += temp = (2*(*P2) + (*P1) - w21*(*N2)) / 3.0; 00178 *(m_prPNTriCoef+b) = temp[0]; 00179 *(m_prPNTriCoef+b+1) = temp[1]; 00180 *(m_prPNTriCoef+b+2) = temp[2]; 00181 // b021 00182 b += 12; 00183 sum += temp = (2*(*P2) + (*P3) - w23*(*N2)) / 3.0; 00184 *(m_prPNTriCoef+b) = temp[0]; 00185 *(m_prPNTriCoef+b+1) = temp[1]; 00186 *(m_prPNTriCoef+b+2) = temp[2]; 00187 // b012 00188 b += 6; 00189 sum += temp = (2*(*P3) + (*P2) - w32*(*N3)) / 3.0; 00190 *(m_prPNTriCoef+b) = temp[0]; 00191 *(m_prPNTriCoef+b+1) = temp[1]; 00192 *(m_prPNTriCoef+b+2) = temp[2]; 00193 // b102 00194 b -= 3; 00195 sum += temp = (2*(*P3) + (*P1) - w31*(*N3)) / 3.0; 00196 *(m_prPNTriCoef+b) = temp[0]; 00197 *(m_prPNTriCoef+b+1) = temp[1]; 00198 *(m_prPNTriCoef+b+2) = temp[2]; 00199 // b201 00200 b -= 9; 00201 sum += temp = (2*(*P1) + (*P3) - w13*(*N1)) / 3.0; 00202 *(m_prPNTriCoef+b) = temp[0]; 00203 *(m_prPNTriCoef+b+1) = temp[1]; 00204 *(m_prPNTriCoef+b+2) = temp[2]; 00205 // b111 00206 b += 3; 00207 sum /= 6.0; 00208 temp = (*P1 + *P2 + *P3) / 3.0; 00209 temp = sum + (sum - temp) / 2.0; 00210 *(m_prPNTriCoef+b) = temp[0]; 00211 *(m_prPNTriCoef+b+1) = temp[1]; 00212 *(m_prPNTriCoef+b+2) = temp[2]; 00213 00214 b += 15; // update b index 00215 } 00216 } 00217 //----------------------------------------------------------------------------- 00218 // (Helper Fn) CalculatePNTriangleNormals 00219 template <typename T> 00220 void OpenGLPNTriangleVolPresModel<T>::CalculatePNTriangleNormals () { 00221 // Normals 00222 // n002 5 00223 // / \ 00224 // / \ 00225 // n101 n011 <===> 3 4 00226 // / \ 00227 // / \ 00228 // n200 ---- n110 ---- n020 0 1 2 00229 int n = 0; 00230 Vector3<T> *P1, *P2, *P3; 00231 Vector3<T> *N1, *N2, *N3; 00232 Vector3<T> temp; 00233 T v12, v23, v31; 00234 for ( int f = 0; f < GetNoFaces(); ++f ) { 00235 P1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetPosition(); 00236 P2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetPosition(); 00237 P3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetPosition(); 00238 N1 = &m_prXVertex[ m_prFace[f].GetVertexNo(0) ].GetNormal(); 00239 N2 = &m_prXVertex[ m_prFace[f].GetVertexNo(1) ].GetNormal(); 00240 N3 = &m_prXVertex[ m_prFace[f].GetVertexNo(2) ].GetNormal(); 00241 //cout << *N1 << " " << *N2 << " " << *N3 << endl; 00242 Vector3<T> p12(*P2 - *P1); 00243 Vector3<T> p23(*P3 - *P2); 00244 Vector3<T> p31(*P1 - *P3); 00245 Vector3<T> n12(*N2 + *N1); 00246 Vector3<T> n23(*N3 + *N2); 00247 Vector3<T> n31(*N1 + *N3); 00248 v12 = 2.0 * (p12 * n12) / (p12 * p12); 00249 v23 = 2.0 * (p23 * n23) / (p23 * p23); 00250 v31 = 2.0 * (p31 * n31) / (p31 * p31); 00251 // n110 00252 temp = (n12 - v12*p12).Normalized(); 00253 n += 3; 00254 *(m_prPNTriNormal+n) = temp[0]; 00255 *(m_prPNTriNormal+n+1) = temp[1]; 00256 *(m_prPNTriNormal+n+2) = temp[2]; 00257 // n101 00258 temp = (n23 - v23*p23).Normalized(); 00259 n += 6; 00260 *(m_prPNTriNormal+n) = temp[0]; 00261 *(m_prPNTriNormal+n+1) = temp[1]; 00262 *(m_prPNTriNormal+n+2) = temp[2]; 00263 // n011 00264 temp = (n31 - v31*p31).Normalized(); 00265 n += 3; 00266 *(m_prPNTriNormal+n) = temp[0]; 00267 *(m_prPNTriNormal+n+1) = temp[1]; 00268 *(m_prPNTriNormal+n+2) = temp[2]; 00269 n += 6; // update n index 00270 } 00271 } 00272 //----------------------------------------------------------------------------- 00273 // (Helper Fn) DrawTriBezierPatchNo 00274 template <typename T> 00275 void OpenGLPNTriangleVolPresModel<T>::DrawTriBezierPatchNo(int faceNo, int smoothness) { 00276 int noOfVertices = smoothness*(smoothness+1)/2; 00277 float *vertex = new float[noOfVertices*3]; 00278 float *normal = new float[noOfVertices*3]; 00279 int index = 0; 00280 float uInc = 1.0 / (smoothness-1); 00281 float vInc = 1.0 / (smoothness-1); 00282 float u = 0.0f, v = 0.0f, w; 00283 float uu, vv, ww; 00284 int bCount = faceNo * 30; 00285 int nCount = faceNo * 18; 00286 float *b = m_prPNTriCoef; 00287 float *n = m_prPNTriNormal; 00288 for ( int i = 0; i < smoothness; ++i, u += uInc ) { 00289 v = 0.0f; 00290 for ( int j = 0; j < smoothness; ++j, v += vInc ) { 00291 w = 1.0f - u - v; 00292 if ( w < -0.0000001f ) break; 00293 ww = w*w; 00294 uu = u*u; 00295 vv = v*v; 00296 T magnitude = 0.0; 00297 for ( int d = 0; d < 3; ++d, ++index ) { 00298 vertex[index] = b[bCount+d]*ww*w + b[bCount+9+d]*uu*u + b[bCount+27+d]*vv*v 00299 + 3.0f*( b[bCount+3+d]*ww*u + b[bCount+6+d]*w*uu 00300 + b[bCount+12+d]*ww*v + b[bCount+18+d]*uu*v 00301 + b[bCount+21+d]*w*vv + b[bCount+24+d]*u*vv ) 00302 + b[bCount+15+d]*6.0f*w*u*v; 00303 normal[index] = n[nCount+d]*ww + n[nCount+6+d]*uu + n[nCount+15+d]*vv 00304 + n[nCount+3+d]*w*u + n[nCount+9+d]*u*v + n[nCount+12+d]*w*v; 00305 magnitude += normal[index]*normal[index]; 00306 } 00307 magnitude = sqrt(magnitude); 00308 normal[index-3] /= magnitude; 00309 normal[index-2] /= magnitude; 00310 normal[index-1] /= magnitude; 00311 } 00312 } 00313 /* 00314 glPointSize(11); 00315 int pos = 0; 00316 glBegin( GL_POINTS ); 00317 for ( int i = 0; i < noOfVertices; ++i, pos+=3 ) { 00318 glVertex3f( vertex[pos], vertex[pos+1], vertex[pos+2] ); 00319 } 00320 glEnd(); 00321 */ 00322 00323 int row1 = 0, row2 = smoothness*3; 00324 for ( int r = 1; r < smoothness; ++r, row1+=3 ) { 00325 glBegin( GL_TRIANGLE_STRIP ); 00326 for ( int i = 0; i < smoothness-r; ++i, row1+=3, row2+=3 ) { 00327 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00328 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00329 glNormal3f( normal[row2], normal[row2+1], normal[row2+2] ); 00330 glVertex3f( vertex[row2], vertex[row2+1], vertex[row2+2] ); 00331 } 00332 glNormal3f( normal[row1], normal[row1+1], normal[row1+2] ); 00333 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00334 glEnd(); 00335 } 00336 /* 00337 row1 = 0; 00338 for ( int r = 0; r < noOfVertices; ++r, row1+=3 ) { 00339 glBegin( GL_LINES ); 00340 GLfloat ppp[3]; 00341 ppp[0]= normal[row1]+vertex[row1]; 00342 ppp[1]= normal[row1+1]+vertex[row1+1]; 00343 ppp[2]= normal[row1+2]+vertex[row1+2]; 00344 glVertex3f( vertex[row1], vertex[row1+1], vertex[row1+2] ); 00345 glVertex3fv( ppp ); 00346 glEnd(); 00347 }*/ 00348 /* 00349 int idx = 0; 00350 DrawText( "b300", 00351 static_cast<float>( vertex[idx] ), 00352 static_cast<float>( vertex[idx+1] ), 00353 static_cast<float>( vertex[idx+2] ) ); 00354 idx = (smoothness-1)*3; 00355 DrawText( "b030", 00356 static_cast<float>( vertex[idx] ), 00357 static_cast<float>( vertex[idx+1] ), 00358 static_cast<float>( vertex[idx+2] ) ); 00359 idx = (noOfVertices-1)*3; 00360 DrawText( "b003", 00361 static_cast<float>( vertex[idx] ), 00362 static_cast<float>( vertex[idx+1] ), 00363 static_cast<float>( vertex[idx+2] ) ); 00364 */ 00365 delete [] vertex; 00366 delete [] normal; 00367 } 00368 /* 00369 //----------------------------------------------------------------------------- 00370 //void DisplayGL() 00371 template <typename T> 00372 void OpenGLPNTriangleVolPresModel<T>::DisplayGL( OpenGL::Enum::DrawMode DM ) 00373 { 00374 switch (DM) { 00375 case OpenGL::Enum::POLYGON: 00376 DrawGL( GL_POLYGON ); 00377 break; 00378 case OpenGL::Enum::WIRE_FRAME: 00379 DrawGL( GL_LINE_LOOP ); 00380 break; 00381 case OpenGL::Enum::POINT: 00382 DrawGL( GL_POINTS ); 00383 break; 00384 case OpenGL::Enum:: POLYGON_WITH_WIRE_FRAME: 00385 // Draw the object with GL_POLYGON_OFFSET enabled 00386 glEnable( GL_POLYGON_OFFSET_FILL ); 00387 glPolygonOffset( 1.0, 1.0 ); 00388 DrawGL( GL_POLYGON ); 00389 glDisable( GL_POLYGON_OFFSET_FILL ); 00390 // Draw the wire frame of the object 00391 glDisable( GL_LIGHTING ); 00392 //glDisable( GL_DEPTH_TEST ); 00393 glColor3ub( 255, 200, 0 ); 00394 DrawGL( GL_LINE_LOOP ); 00395 glEnable( GL_LIGHTING ); 00396 //glEnable( GL_DEPTH_TEST ); 00397 break; 00398 } 00399 } 00400 //*/ 00401 //----------------------------------------------------------------------------- 00402 //void DisplayGLText() 00403 template <typename T> 00404 void OpenGLPNTriangleVolPresModel<T>::DisplayGLText( T x, T y, T z ) 00405 { 00406 char str[128] = ""; 00407 int count = 0; 00408 T volume = CalVolume(); // for calculating centroid 00409 sprintf( str, "CalVolume: %g", volume ); 00410 Fn::DrawGLText( str, x, y-2*count++, z ); 00411 sprintf( str, "Centroid: (%10.4f, %10.4f, %10.4f)", m_vtCentroid[0], m_vtCentroid[1], m_vtCentroid[2] ); 00412 Fn::DrawGLText( str, x, y-2*count++, z ); 00413 CalVolume2(); 00414 sprintf( str, "CalVolume2: %g", m_tVolume ); 00415 Fn::DrawGLText( str, x, y-2*count++, z ); 00416 sprintf( str, "weight0: %g", m_weight[0] ); 00417 Fn::DrawGLText( str, x, y-2*count++, z ); 00418 // sprintf( str, "weight1: %g", weight[1] ); 00419 // Fn::DrawGLText( str, x, y-2*count++, z ); 00420 // sprintf( str, "weight2: %g", weight[2] ); 00421 // Fn::DrawGLText( str, x, y-2*count++, z ); 00422 // sprintf( str, "weight3: %g", weight[3] ); 00423 // Fn::DrawGLText( str, x, y-2*count++, z ); 00424 if ( m_bFlagSpringForceMode ) { 00425 sprintf( str, "Spring Force: ON" ); 00426 } 00427 else { 00428 sprintf( str, "Spring Force: OFF" ); 00429 } 00430 Fn::DrawGLText( str, x, y-2*count++, z ); 00431 if ( m_bFlagVolPresMode ) { 00432 sprintf( str, "VolPres: ON" ); 00433 } 00434 else { 00435 sprintf( str, "VolPres: OFF" ); 00436 } 00437 Fn::DrawGLText( str, x, y-2*count++, z ); 00438 } 00439 //----------------------------------------------------------------------------- 00440 // Helper Fn 00441 // void DrawGL() 00442 template <typename T> 00443 void OpenGLPNTriangleVolPresModel<T>::DrawGL( GLenum drawMode ) 00444 { 00445 00446 InitializePNTriangleCoefficients(); 00447 InitializePNTriangleNormals(); 00448 00449 // StepSimulation 00450 int count; 00451 if ( bRunSimulation ) { 00453 for ( count = 0; count < 5; count++ ) { 00454 StepSimulation( 0.01 ); 00455 } 00456 //*/ 00457 00458 /* 00459 for ( int i = 0; i < 5; ++i ) { 00460 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) { 00461 m_prXVertex[ m_prFace[i].GetVertexNo(j) ].SetPosition( 00462 m_prXVertex[ m_prFace[i].GetVertexNo(j) ].GetPosition() * 1.001 00463 ); 00464 //m_prXVertex[ 0].SetPosition( m_prXVertex[ 0].GetPosition() * 1.001 ); 00465 //m_prXVertex[12].SetPosition( m_prXVertex[12].GetPosition() * 1.001 ); 00466 //m_prXVertex[13].SetPosition( m_prXVertex[13].GetPosition() * 1.001 ); 00467 } 00468 } 00469 */ 00470 } 00471 00472 //if ( isFacetShading ) { 00473 if ( false ) { 00474 // Draw the object 00475 //glEnable( GL_TEXTURE_2D ); 00476 for ( int i = 0; i < m_iNoFaces; i++ ) 00477 { 00478 glBegin( drawMode ); 00479 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00480 { 00481 // Draw texture 00482 if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00483 glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00484 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00485 } 00486 // Normal of the face 00487 glNormal3f ( static_cast<float>( m_prFace[i].GetNormal()[0] ), 00488 static_cast<float>( m_prFace[i].GetNormal()[1] ), 00489 static_cast<float>( m_prFace[i].GetNormal()[2] ) ); 00490 // Draw the vertex 00491 glVertex3f ( 00492 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00493 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00494 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00495 } 00496 glEnd(); 00497 } 00498 //glDisable( GL_TEXTURE_2D ); 00499 } 00500 else if ( isFacetShading ) { 00501 // Draw the object 00502 //glEnable( GL_TEXTURE_2D ); 00503 for ( int i = 0; i < m_iNoFaces; i++ ) 00504 { 00505 glBegin( drawMode ); 00506 for ( int j = 0; j < m_prFace[i].GetNoVertices(); ++j ) 00507 { 00508 // Draw texture 00509 if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00510 glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00511 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00512 } 00513 // Normal of the vertex 00514 glNormal3f ( static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[0] ), 00515 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[1] ), 00516 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ].GetNormal()[2] ) ); 00517 // Draw the vertex 00518 glVertex3f ( 00519 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][0] ), 00520 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][1] ), 00521 static_cast<float>( m_prXVertex[ m_prFace[i].GetVertexNo( j ) ][2] ) ); 00522 } 00523 glEnd(); 00524 } 00525 //glDisable( GL_TEXTURE_2D ); 00526 } 00527 else { 00528 // Draw the object 00529 //glEnable( GL_TEXTURE_2D ); 00530 00531 int b[3] = { 0, 9, 27 }, n[3] = { 0, 6, 15 }; 00532 00533 /* 00534 // For FPS 00535 clock_t start, finish; 00536 start = clock(); 00537 */ 00538 //for ( int i = 0; i < m_iNoFaces; ++i ) 00539 { 00540 #ifdef TAPs_USE_GLSL 00541 if ( gbUseGPU ) { 00542 //setUniformVariables(); 00543 //DrawPNTrianglePatch(i, 10, m_prPNTriCoef, m_prPNTriNormal); 00544 #ifdef TAPs_SHADER_FNS_HPP 00545 DrawTriangleUsingHardware(m_iNoFaces, 10, m_prPNTriCoef, m_prPNTriNormal); 00546 #endif 00547 } 00548 else { 00549 // Draw Bezier Patch 00550 for ( int i = 0; i < m_iNoFaces; ++i ) 00551 DrawTriBezierPatchNo(i, 10); 00552 //DrawTriBezierPatchNo(m_iNoFaces, 10); 00553 } 00554 #else 00555 for ( int i = 0; i < m_iNoFaces; ++i ) 00556 DrawTriBezierPatchNo(i, 10); 00557 //DrawTriBezierPatchNo(m_iNoFaces, 10); 00558 #endif 00559 00560 /* 00561 glBegin( GL_LINE_LOOP ); 00562 for ( int j = 0; j < 3; ++j ) 00563 { 00564 // Draw texture 00565 if ( m_prFace[i].GetNoTexCoords() != 0 ) { 00566 glTexCoord2f( static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2 ) ), 00567 static_cast<float>( m_prFace[i].GetTexCoordHalfNo( j*2+1 ) ) ); 00568 } 00569 // Normal of the vertex 00570 glNormal3f ( static_cast<float>( m_prPNTriNormal[n[j] ] ), 00571 static_cast<float>( m_prPNTriNormal[n[j]+1] ), 00572 static_cast<float>( m_prPNTriNormal[n[j]+2] ) ); 00573 // Draw the vertex 00574 glVertex3f ( 00575 static_cast<float>( m_prPNTriCoef[ b[j] ] ), 00576 static_cast<float>( m_prPNTriCoef[ b[j]+1] ), 00577 static_cast<float>( m_prPNTriCoef[ b[j]+2] ) ); 00578 } 00579 glEnd(); 00580 */ 00581 00582 /* 00583 DrawText( "b300", 00584 static_cast<float>( m_prPNTriCoef[ b[0] ] ), 00585 static_cast<float>( m_prPNTriCoef[ b[0]+1] ), 00586 static_cast<float>( m_prPNTriCoef[ b[0]+2] ) ); 00587 DrawText( "b030", 00588 static_cast<float>( m_prPNTriCoef[ b[1] ] ), 00589 static_cast<float>( m_prPNTriCoef[ b[1]+1] ), 00590 static_cast<float>( m_prPNTriCoef[ b[1]+2] ) ); 00591 DrawText( "b003", 00592 static_cast<float>( m_prPNTriCoef[ b[2] ] ), 00593 static_cast<float>( m_prPNTriCoef[ b[2]+1] ), 00594 static_cast<float>( m_prPNTriCoef[ b[2]+2] ) ); 00595 */ 00596 00597 /* 00598 // Draw Bezier control points 00599 glPointSize(5); 00600 glDisable(GL_LIGHTING); 00601 int s; 00602 glBegin( GL_POINTS ); 00603 for ( s = 0; s < 30; s+=3 ) { 00604 // Draw the vertex 00605 glColor3ub( s*25, 0, 0 ); 00606 glVertex3f ( 00607 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00608 static_cast<float>( m_prPNTriCoef[ b[0]+s+1 ] ), 00609 static_cast<float>( m_prPNTriCoef[ b[0]+s+2 ] ) ); 00610 } 00611 glEnd(); 00612 */ 00613 00614 /* 00615 DrawText( "b300", 00616 static_cast<float>( m_prPNTriCoef[ b[0] ] ), 00617 static_cast<float>( m_prPNTriCoef[ b[0]+1] ), 00618 static_cast<float>( m_prPNTriCoef[ b[0]+2] ) ); 00619 DrawText( "b210", 00620 static_cast<float>( m_prPNTriCoef[ b[0]+3] ), 00621 static_cast<float>( m_prPNTriCoef[ b[0]+4] ), 00622 static_cast<float>( m_prPNTriCoef[ b[0]+5] ) ); 00623 DrawText( "b120", 00624 static_cast<float>( m_prPNTriCoef[ b[0]+6] ), 00625 static_cast<float>( m_prPNTriCoef[ b[0]+7] ), 00626 static_cast<float>( m_prPNTriCoef[ b[0]+8] ) ); 00627 DrawText( "b030", 00628 static_cast<float>( m_prPNTriCoef[ b[0]+9 ] ), 00629 static_cast<float>( m_prPNTriCoef[ b[0]+10] ), 00630 static_cast<float>( m_prPNTriCoef[ b[0]+11] ) ); 00631 DrawText( "b201", 00632 static_cast<float>( m_prPNTriCoef[ b[0]+12] ), 00633 static_cast<float>( m_prPNTriCoef[ b[0]+13] ), 00634 static_cast<float>( m_prPNTriCoef[ b[0]+14] ) ); 00635 DrawText( "b111", 00636 static_cast<float>( m_prPNTriCoef[ b[0]+15] ), 00637 static_cast<float>( m_prPNTriCoef[ b[0]+16] ), 00638 static_cast<float>( m_prPNTriCoef[ b[0]+17] ) ); 00639 DrawText( "b021", 00640 static_cast<float>( m_prPNTriCoef[ b[0]+18] ), 00641 static_cast<float>( m_prPNTriCoef[ b[0]+19] ), 00642 static_cast<float>( m_prPNTriCoef[ b[0]+20] ) ); 00643 DrawText( "b102", 00644 static_cast<float>( m_prPNTriCoef[ b[0]+21] ), 00645 static_cast<float>( m_prPNTriCoef[ b[0]+22] ), 00646 static_cast<float>( m_prPNTriCoef[ b[0]+23] ) ); 00647 DrawText( "b012", 00648 static_cast<float>( m_prPNTriCoef[ b[0]+24] ), 00649 static_cast<float>( m_prPNTriCoef[ b[0]+25] ), 00650 static_cast<float>( m_prPNTriCoef[ b[0]+26] ) ); 00651 DrawText( "b003", 00652 static_cast<float>( m_prPNTriCoef[ b[0]+27] ), 00653 static_cast<float>( m_prPNTriCoef[ b[0]+28] ), 00654 static_cast<float>( m_prPNTriCoef[ b[0]+29] ) ); 00655 */ 00656 00657 /* 00658 // Draw lines connecting Bezier control points 00659 glColor3ub( 0, 220, 0 ); 00660 glBegin( GL_LINE_LOOP ); 00661 s = 0; 00662 glVertex3f ( 00663 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00664 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00665 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00666 s = 3; 00667 glVertex3f ( 00668 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00669 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00670 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00671 s = 6; 00672 glVertex3f ( 00673 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00674 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00675 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00676 s = 9; 00677 glVertex3f ( 00678 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00679 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00680 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00681 s = 18; 00682 glVertex3f ( 00683 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00684 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00685 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00686 s = 24; 00687 glVertex3f ( 00688 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00689 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00690 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00691 s = 27; 00692 glVertex3f ( 00693 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00694 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00695 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00696 s = 21; 00697 glVertex3f ( 00698 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00699 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00700 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00701 s = 12; 00702 glVertex3f ( 00703 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00704 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00705 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00706 glEnd(); 00707 glBegin( GL_LINE_LOOP ); 00708 s = 3; 00709 glVertex3f ( 00710 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00711 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00712 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00713 s = 12; 00714 glVertex3f ( 00715 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00716 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00717 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00718 s = 15; 00719 glVertex3f ( 00720 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00721 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00722 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00723 glEnd(); 00724 glBegin( GL_LINE_LOOP ); 00725 s = 6; 00726 glVertex3f ( 00727 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00728 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00729 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00730 s = 15; 00731 glVertex3f ( 00732 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00733 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00734 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00735 s = 18; 00736 glVertex3f ( 00737 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00738 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00739 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00740 glEnd(); 00741 glBegin( GL_LINE_LOOP ); 00742 s = 15; 00743 glVertex3f ( 00744 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00745 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00746 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00747 s = 21; 00748 glVertex3f ( 00749 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00750 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00751 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00752 s = 24; 00753 glVertex3f ( 00754 static_cast<float>( m_prPNTriCoef[ b[0]+s ] ), 00755 static_cast<float>( m_prPNTriCoef[ b[0]+s+1] ), 00756 static_cast<float>( m_prPNTriCoef[ b[0]+s+2] ) ); 00757 glEnd(); 00758 */ 00759 00760 //glEnable(GL_LIGHTING); 00761 b[0] += 30; 00762 b[1] += 30; 00763 b[2] += 30; 00764 n[0] += 18; 00765 n[1] += 18; 00766 n[2] += 18; 00767 } 00768 /* 00769 // For FPS 00770 finish = clock(); 00771 double duration = static_cast<double>(finish - start); 00772 int dFPS; 00773 if ( duration > 0.0 ) { 00774 dFPS = CLOCKS_PER_SEC / duration; 00775 } 00776 else { 00777 dFPS = 1000; 00778 } 00779 std::cout << "FPs: " << dFPS << "\n"; 00780 */ 00781 00782 //glDisable( GL_TEXTURE_2D ); 00783 } 00784 // Display vertex normals 00785 if ( bFlagShowVertexNormals ) { 00786 DisplayGL_ParticleNormals(); 00787 } 00788 // Display vertex rings 00789 if ( bFlagShowVertexRings ) { 00790 DisplayGL_VertexRings(); 00791 } 00792 // Display the deformed face 00793 if ( bFlagShowDeformedFace ) { 00794 //DisplayGL_DeformedFaceNo(); 00795 // comment out because gpu drawing will cause it 00796 // be drawn incorrectly 00797 } 00798 00799 // Display Volume and Centroid 00800 glDisable( GL_LIGHTING ); 00801 glPushAttrib(GL_ALL_ATTRIB_BITS); 00802 glColor3ub( 200, 185, 0 ); 00803 00804 glPopAttrib(); 00805 glEnable( GL_LIGHTING ); 00806 } 00807 //----------------------------------------------------------------------------- 00808 //============================================================================= 00809 // FOR SIMULATION 00810 //----------------------------------------------------------------------------- 00811 // Display Particle Normals Via OpenGL 00812 template <typename T> 00813 void OpenGLPNTriangleVolPresModel<T>::DisplayGL_ParticleNormals() 00814 { 00815 T x, y, z; 00816 00817 // Draw Normal of each particle 00818 glDisable( GL_LIGHTING ); 00819 glColor3f( 1.0f, 0.0f, 0.0f ); 00820 glBegin( GL_LINES ); 00821 for ( int n = 0; n < m_iNoVertices; ++n ){ 00822 m_pprParticleVertex[n]->GetPosition().GetXYZ( x, y, z ); 00823 glVertex3f( static_cast<float>( x ), static_cast<float>( y ), static_cast<float>( z ) ); 00824 glVertex3f ( 00825 static_cast<float>( x + m_pprParticleVertex[n]->GetNormal()[0] ), 00826 static_cast<float>( y + m_pprParticleVertex[n]->GetNormal()[1] ), 00827 static_cast<float>( z + m_pprParticleVertex[n]->GetNormal()[2] ) 00828 ); 00829 } 00830 glEnd(); 00831 glEnable( GL_LIGHTING ); 00832 } 00833 //----------------------------------------------------------------------------- 00834 // Display Vertex Rings Via OpenGL 00835 template <typename T> 00836 void OpenGLPNTriangleVolPresModel<T>::DisplayGL_VertexRings() 00837 { 00838 glDisable( GL_LIGHTING ); 00839 //glLineWidth( 3.0f ); 00840 glPointSize( 7.0f ); 00841 for ( int i = 0; i <= m_pcVertexRings->GetNoRings(); ++i ) { 00842 glColor3ub( 255-(i*25), 0+(i*25), 15 ); 00843 //glBegin( GL_LINE_LOOP ); 00844 glBegin( GL_POINTS ); 00845 for ( int j = m_pcVertexRings->GetRingLayers()[i]; j < m_pcVertexRings->GetRingLayers()[i+1]; ++j ) { 00846 glVertex3f ( 00847 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][0] ), 00848 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][1] ), 00849 static_cast<float>( m_prXVertex[ m_pcVertexRings->GetRingVertices()[j] ][2] ) 00850 ); 00851 } 00852 glEnd(); 00853 } 00854 glPointSize( 1.0f ); 00855 //glLineWidth( 1.0f ); 00856 glEnable( GL_LIGHTING ); 00857 } 00858 //----------------------------------------------------------------------------- 00859 // Display The Deformed Face Via OpenGL 00860 template <typename T> 00861 void OpenGLPNTriangleVolPresModel<T>::DisplayGL_DeformedFaceNo () 00862 { 00863 // Draw Normal of each particle 00864 glDisable( GL_LIGHTING ); 00865 glColor3f( 0.0f, 1.0f, 0.0f ); 00866 glBegin( GL_POLYGON ); 00867 for ( int n = 0; n < m_prFace[m_iDeformedFaceNo].GetNoVertices(); ++n ){ 00868 glVertex3f ( 00869 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][0] ), 00870 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][1] ), 00871 static_cast<float>( m_prXVertex[m_prFace[m_iDeformedFaceNo].GetVertexNo( n )][2] ) 00872 ); 00873 } 00874 glEnd(); 00875 glEnable( GL_LIGHTING ); 00876 } 00877 //----------------------------------------------------------------------------- 00878 // Preserve Volume using Vertex Normals 00879 // If not a triangle face find the center (average) point, then virtually crete 00880 // triangle fan from the center point for volume calculations 00881 // Using d3mom0 ( v, dv, wgt ) subroutine to calculate a volume contribution by 00882 // the triangle(s), dv zeroes mean the point is fixed, else the point will have 00883 // to be moved in order to preserve the volume of the model. 00884 template <typename T> 00885 void OpenGLPNTriangleVolPresModel<T>::PreserveVolumeByVertexNormals ( T wgt[4] ) 00886 { 00887 VolPresPolygonalModel<T>::PreserveVolumeByVertexNormals( wgt ); 00888 // Recalculate PN-Triangles 00889 CalculatePNTriangleCoefficients(); 00890 CalculatePNTriangleNormals(); 00891 } 00892 //============================================================================= 00893 END_NAMESPACE_TAPs__OpenGL 00894 //----------------------------------------------------------------------------- 00895 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00896 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8