TAPs 0.7.7.3
TAPsOpenGLUsefulObjs.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLUsefulObjs.cpp
00003 
00004 Useful Objects drawed by OpenGL
00005 
00006 SUKITTI PUNAK   (07/10/2005)
00007 UPDATE          (11/28/2006)
00008 ******************************************************************************/
00009 #include "TAPsOpenGLUsefulObjs.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs__OpenGL
00015 //=============================================================================
00016 template <typename T> GLUquadricObj * OpenGLUsefulObj<T>::g_pQuadricObj = Fn::NewGLUQuardic();
00017 //template <typename T> GLuint          OpenGLUsefulObj<T>::m_uiDisplayList = 0;
00018 //template <typename T> GLsizei         OpenGLUsefulObj<T>::m_iDisplayListSize = 3;
00019 //=============================================================================
00020 // Constructor
00021 //-----------------------------------------------------------------------------
00022 //template <typename T>
00023 //OpenGLUsefulObj<T>::OpenGLUsefulObj ()
00024 //{
00025 //  GenDisplayList( GLU_FILL, 10, 10 );
00026 //}
00027 //-----------------------------------------------------------------------------
00028 template <typename T>
00029 OpenGLUsefulObj<T>::OpenGLUsefulObj ( 
00030     GLenum drawStyle, GLint iNumSlices, GLint iNumStacks,
00031     Vector4<T> & vMaterialColor )
00032     : m_uiDisplayList( 0 ), 
00033       m_iDisplayListSize( 4 )
00034 {
00035     GenDisplayList( drawStyle, iNumSlices, iNumStacks, vMaterialColor );
00036 }
00037 //-----------------------------------------------------------------------------
00038 template <typename T>
00039 OpenGLUsefulObj<T>::OpenGLUsefulObj ( 
00040     GLenum drawStyle, GLint iNumSlices, GLint iNumStacks,
00041     GLfloat r, GLfloat g, GLfloat b, GLfloat a )
00042     : m_uiDisplayList( 0 ), 
00043       m_iDisplayListSize( 4 )
00044 {
00045     GenDisplayList( drawStyle, iNumSlices, iNumStacks, r, g, b, a );
00046 }
00047 //-----------------------------------------------------------------------------
00048 //=============================================================================
00049 // Generate Display List
00050 //-----------------------------------------------------------------------------
00051 template <typename T>
00052 void OpenGLUsefulObj<T>::GenDisplayList ( 
00053     GLenum drawStyle, GLint iNumSlices, GLint iNumStacks, 
00054     Vector4<T> & vMaterialColor )
00055 {
00056     GenDisplayList( drawStyle, iNumSlices, iNumStacks, 
00057         vMaterialColor[0], 
00058         vMaterialColor[1], 
00059         vMaterialColor[2], 
00060         vMaterialColor[3] 
00061     );
00062 }
00063 //-----------------------------------------------------------------------------
00064 template <typename T>
00065 void OpenGLUsefulObj<T>::GenDisplayList ( 
00066     GLenum drawStyle, GLint iNumSlices, GLint iNumStacks, 
00067     GLfloat r, GLfloat g, GLfloat b, GLfloat a )
00068 {
00069     // GLenum drawStyle
00070     //  GLU_FILL
00071     //  GLU_LINE
00072     //  GLU_SILHOUETTE
00073     //  GLU_POINT
00074     if ( m_uiDisplayList != 0 ) {
00075         //-------------------------------------------------
00076         // Delete the current list
00077         glDeleteLists( m_uiDisplayList, m_iDisplayListSize );
00078     }
00079     //-------------------------------------------------
00080     //glPushAttrib( GL_ENABLE_BIT );
00081     //glEnable( GL_COLOR_MATERIAL );
00082     //glColor4fv( vMaterialColor.GetDataFloat() );
00083     m_afMaterialColor[0] = r;
00084     m_afMaterialColor[1] = g;
00085     m_afMaterialColor[2] = b;
00086     m_afMaterialColor[3] = a;
00087     //-------------------------------------------------
00088     m_uiDisplayList = glGenLists( m_iDisplayListSize );
00089     GLUquadricObj *qObj = gluNewQuadric();
00090     //-------------------------------------------------
00091     // m_uiDisplayList + 00 --> Sphere
00092     glNewList( m_uiDisplayList, GL_COMPILE );
00093         gluQuadricDrawStyle( qObj, drawStyle );
00094         //gluSphere( qObj, 1.0, 9, 9 );
00095         //gluSphere( qObj, 1.0, 12, 12 );
00096         gluSphere( qObj, 1.0, iNumSlices, iNumStacks );
00097     glEndList();
00098     //-------------------------------------------------
00099     // m_uiDisplayList + 01 --> Cylinder
00100     glNewList( m_uiDisplayList + 1, GL_COMPILE );
00101         gluQuadricDrawStyle( qObj, drawStyle );
00102         // Draw a cylinder oriented along the z-axis, 
00103         // with the base of the cylinder at z = 0
00104         // and the top at z = height
00105         gluCylinder( qObj, 
00106                         1.0,            // Base Radius
00107                         1.0,            // Top  Radius
00108                         1.0,            // Height
00109                         iNumSlices,     // #slices
00110                         iNumStacks      // #stacks
00111         );
00112     glEndList();
00113     //-------------------------------------------------
00114     // m_uiDisplayList + 02 --> Cone
00115     glNewList( m_uiDisplayList + 2, GL_COMPILE );
00116         gluQuadricDrawStyle( qObj, drawStyle );
00117         // Draw a cylinder oriented along the z-axis, 
00118         // with the base of the cylinder at z = 0
00119         // and the top at z = height
00120         gluCylinder( qObj, 
00121                         1.0,            // Base Radius
00122                         0.0,            // Top  Radius
00123                         1.0,            // Height
00124                         iNumSlices,     // #slices
00125                         iNumStacks      // #stacks
00126         );
00127     glEndList();
00128     //-------------------------------------------------
00129     // m_uiDisplayList + 03 --> Cube
00130     float half = 0.5f;
00131     glNewList( m_uiDisplayList + 3, GL_COMPILE );
00132         /*
00133         glBegin( GL_QUAD_STRIP );
00134             // Left
00135             glNormal3f( -1,  0,  0 );
00136             glVertex3f( -half,  half,  half );
00137             glVertex3f( -half, -half,  half );
00138             glVertex3f( -half,  half, -half );
00139             glVertex3f( -half, -half, -half );
00140             // Front
00141             glNormal3f(  0,  0,  1 );
00142             glVertex3f(  half,  half, -half );
00143             glVertex3f(  half, -half, -half );
00144             // Right
00145             glNormal3f(  1,  0,  0 );
00146             glVertex3f(  half,  half,  half );
00147             glVertex3f(  half, -half,  half );
00148             // Back
00149             glNormal3f(  0,  0, -1 );
00150             glVertex3f( -half,  half,  half );
00151             glVertex3f( -half, -half,  half );
00152         glEnd();
00153         //*/
00154 
00155         //*
00156         glBegin( GL_QUADS );
00157             // Left square
00158             glNormal3f( -1,  0,  0 );
00159             glVertex3f( -0.5f, -0.5f,  0.5f );
00160             glVertex3f( -0.5f,  0.5f,  0.5f );
00161             glVertex3f( -0.5f,  0.5f, -0.5f );
00162             glVertex3f( -0.5f, -0.5f, -0.5f );
00163         glEnd();
00164         glBegin( GL_QUADS );
00165             // Right square
00166             glNormal3f(  1,  0,  0 );
00167             glVertex3f(  0.5f, -0.5f,  0.5f );
00168             glVertex3f(  0.5f,  0.5f,  0.5f );
00169             glVertex3f(  0.5f,  0.5f, -0.5f );
00170             glVertex3f(  0.5f, -0.5f, -0.5f );
00171         glEnd();
00172         glBegin( GL_QUADS );
00173             // Front square
00174             glNormal3f(  0,  0,  1 );
00175             glVertex3f( -0.5f, -0.5f,  0.5f );
00176             glVertex3f(  0.5f, -0.5f,  0.5f );
00177             glVertex3f(  0.5f,  0.5f,  0.5f );
00178             glVertex3f( -0.5f,  0.5f,  0.5f );
00179         glEnd();
00180         glBegin( GL_QUADS );
00181             // Back square
00182             glNormal3f(  0,  0, -1 );
00183             glVertex3f( -0.5f, -0.5f, -0.5f );
00184             glVertex3f(  0.5f, -0.5f, -0.5f );
00185             glVertex3f(  0.5f,  0.5f, -0.5f );
00186             glVertex3f( -0.5f,  0.5f, -0.5f );
00187         glEnd();
00188         glBegin( GL_QUADS );
00189             // Top square
00190             glNormal3f(  0,  1,  0 );
00191             glVertex3f( -0.5f,  0.5f, -0.5f );
00192             glVertex3f(  0.5f,  0.5f, -0.5f );
00193             glVertex3f(  0.5f,  0.5f,  0.5f );
00194             glVertex3f( -0.5f,  0.5f,  0.5f );
00195         glEnd();
00196         glBegin( GL_QUADS );
00197             // Bottom square
00198             glNormal3f(  0, -1,  0 );
00199             glVertex3f( -0.5f, -0.5f, -0.5f );
00200             glVertex3f(  0.5f, -0.5f, -0.5f );
00201             glVertex3f(  0.5f, -0.5f,  0.5f );
00202             glVertex3f( -0.5f, -0.5f,  0.5f );
00203         glEnd();
00204         //*/
00205     glEndList();
00206     //-------------------------------------------------
00207     //glPopAttrib();
00208     //-------------------------------------------------
00209     gluDeleteQuadric( qObj );
00210 }
00211 //-----------------------------------------------------------------------------
00212 //=============================================================================
00213 // Sphere
00214 //-----------------------------------------------------------------------------
00215 template <typename T>
00216 void OpenGLUsefulObj<T>::DrawSphere ()
00217 {
00218     DrawCallListNo( 0 );
00219 }
00220 //-----------------------------------------------------------------------------
00221 template <typename T>
00222 void OpenGLUsefulObj<T>::DrawSphere ( T scale )
00223 {
00224     assert( m_uiDisplayList != 0 );
00225     //---------------------------------------------------------------
00226     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00227     glEnable( GL_COLOR_MATERIAL );
00228     glColor4fv( m_afMaterialColor );
00229     glPushMatrix();
00230         glScalef( scale, scale, scale );
00231         glCallList( m_uiDisplayList );
00232     glPopMatrix();
00233     glPopAttrib();
00234 }
00235 //-----------------------------------------------------------------------------
00236 template <typename T>
00237 void OpenGLUsefulObj<T>::DrawSphere ( T sx, T sy, T sz )
00238 {
00239     assert( m_uiDisplayList != 0 );
00240     //---------------------------------------------------------------
00241     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00242     glEnable( GL_COLOR_MATERIAL );
00243     glColor4fv( m_afMaterialColor );
00244     glPushMatrix();
00245         glScalef( sx, sy, sz );
00246         glCallList( m_uiDisplayList );
00247     glPopMatrix();
00248     glPopAttrib();
00249 }
00250 //-----------------------------------------------------------------------------
00251 template <typename T>
00252 void OpenGLUsefulObj<T>::DrawSphere ( Vector3<T> & vScale )
00253 {
00254     assert( m_uiDisplayList != 0 );
00255     //---------------------------------------------------------------
00256     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00257     glEnable( GL_COLOR_MATERIAL );
00258     glColor4fv( m_afMaterialColor );
00259     glPushMatrix();
00260         glScalef( vScale[0], vScale[1], vScale[2] );
00261         glCallList( m_uiDisplayList );
00262     glPopMatrix();
00263     glPopAttrib();
00264 }
00265 //-----------------------------------------------------------------------------
00266 //=============================================================================
00267 // Cylinder
00268 //-----------------------------------------------------------------------------
00269 template <typename T>
00270 void OpenGLUsefulObj<T>::DrawCylinder ()
00271 {
00272     DrawCallListNo( 1 );
00273 }
00274 //-----------------------------------------------------------------------------
00275 template <typename T>
00276 void OpenGLUsefulObj<T>::DrawCylinder ( Vector3<T> & vDirection )
00277 {
00278     DrawCallListNo( 1, vDirection );
00279 }
00280 //-----------------------------------------------------------------------------
00281 template <typename T>
00282 void OpenGLUsefulObj<T>::DrawCylinder ( 
00283     Vector3<T> & vDirection, Vector3<T> & vScale )
00284 {
00285     DrawCallListNo( 1, vDirection, vScale );
00286 }
00287 //-----------------------------------------------------------------------------
00288 //=============================================================================
00289 // Cone
00290 //-----------------------------------------------------------------------------
00291 template <typename T>
00292 void OpenGLUsefulObj<T>::DrawCone ()
00293 {
00294     DrawCallListNo( 2 );
00295 }
00296 //-----------------------------------------------------------------------------
00297 template <typename T>
00298 void OpenGLUsefulObj<T>::DrawCone ( Vector3<T> & vDirection )
00299 {
00300     DrawCallListNo( 2, vDirection );
00301 }
00302 //-----------------------------------------------------------------------------
00303 template <typename T>
00304 void OpenGLUsefulObj<T>::DrawCone ( 
00305     Vector3<T> & vDirection, Vector3<T> & vScale )
00306 {
00307     DrawCallListNo( 2, vDirection, vScale );
00308 }
00309 //-----------------------------------------------------------------------------
00310 //=============================================================================
00311 // Cube
00312 //-----------------------------------------------------------------------------
00313 template <typename T>
00314 void OpenGLUsefulObj<T>::DrawCube ()
00315 {
00316     DrawCallListNo( 3 );
00317 }
00318 //-----------------------------------------------------------------------------
00319 template <typename T>
00320 void OpenGLUsefulObj<T>::DrawCube ( Vector3<T> & vScale )
00321 {
00322     assert( m_uiDisplayList != 0 );
00323     //---------------------------------------------------------------
00324     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00325     glEnable( GL_COLOR_MATERIAL );
00326     glColor4fv( m_afMaterialColor );
00327     glPushMatrix();
00328         glScalef( vScale[0], vScale[1], vScale[2] );
00329         glCallList( m_uiDisplayList + 3 );
00330     glPopMatrix();
00331     glPopAttrib();
00332 }
00333 //-----------------------------------------------------------------------------
00334 template <typename T>
00335 void OpenGLUsefulObj<T>::DrawCube ( 
00336     Vector3<T> & vDirection, Vector3<T> & vScale )
00337 {
00338     DrawCallListNo( 3, vDirection, vScale );
00339 }
00340 //-----------------------------------------------------------------------------
00341 //=============================================================================
00342 // DrawCallListNo Functions
00343 //-----------------------------------------------------------------------------
00344 template <typename T>
00345 void OpenGLUsefulObj<T>::DrawCallListNo ( int i )
00346 {
00347     assert( m_uiDisplayList != 0 );
00348     //---------------------------------------------------------------
00349     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00350     glEnable( GL_COLOR_MATERIAL );
00351     glColor4fv( m_afMaterialColor );
00352         glCallList( m_uiDisplayList + i );
00353     glPopAttrib();
00354 }
00355 //-----------------------------------------------------------------------------
00356 template <typename T>
00357 void OpenGLUsefulObj<T>::DrawCallListNo ( int i, Vector3<T> & vDirection )
00358 {
00359     assert( m_uiDisplayList != 0 );
00360     //---------------------------------------------------------------
00361     Vector3<T>  directionUnit = vDirection.GetUnit();
00362     Vector3<T>  rotationAxis( CGMath<T>::VectorZ ^ directionUnit );
00363     T           rotationAngle =   acos( CGMath<T>::VectorZ * directionUnit ) 
00364                                 * Math<T>::RAD_TO_DEG;
00365     //---------------------------------------------------------------
00366     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00367     glEnable( GL_COLOR_MATERIAL );
00368     glColor4fv( m_afMaterialColor );
00369     glPushMatrix();
00370         glRotatef( rotationAngle, 
00371             rotationAxis[0], rotationAxis[1], rotationAxis[2] );
00372         glCallList( m_uiDisplayList + i );
00373     glPopMatrix();
00374     glPopAttrib();
00375 }
00376 //-----------------------------------------------------------------------------
00377 template <typename T>
00378 void OpenGLUsefulObj<T>::DrawCallListNo ( int i, 
00379             Vector3<T> & vDirection, Vector3<T> & vScale )
00380 {
00381     assert( m_uiDisplayList != 0 );
00382     //---------------------------------------------------------------
00383     Vector3<T>  directionUnit = vDirection.GetUnit();
00384     Vector3<T>  rotationAxis( CGMath<T>::VectorZ ^ directionUnit );
00385     T           rotationAngle =   acos( CGMath<T>::VectorZ * directionUnit ) 
00386                                 * Math<T>::RAD_TO_DEG;
00387     //---------------------------------------------------------------
00388     glPushAttrib( GL_LIGHTING_BIT | GL_CURRENT_BIT );
00389     glEnable( GL_COLOR_MATERIAL );
00390     glColor4fv( m_afMaterialColor );
00391     glPushMatrix();
00392         glRotatef( rotationAngle, 
00393             rotationAxis[0], rotationAxis[1], rotationAxis[2] );
00394         glScalef( vScale[0], vScale[1], vScale[2] );
00395         glCallList( m_uiDisplayList + i );
00396     glPopMatrix();
00397     glPopAttrib();
00398 }
00399 //-----------------------------------------------------------------------------
00400 //=============================================================================
00401 // One-Head-Arrow, i.e. vector (a cylinder + a cone) along z-axis
00402 //-----------------------------------------------------------------------------
00403 template <typename T>
00404 void OpenGLUsefulObj<T>::DrawOneHeadArrow ()
00405 {
00406     //if ( !g_pQuadricObj ) {
00407     //  g_pQuadricObj = Fn::NewGLUQuardic();
00408     //}
00409     glPushMatrix();
00410         gluQuadricDrawStyle( g_pQuadricObj, GLU_FILL );  // smooth shaded
00411         gluQuadricNormals( g_pQuadricObj, GLU_SMOOTH );
00412 
00413         // Not Working!
00414         //gluCylinder( g_pQuadricObj, 0.03, 0.03, 0.8, 1.0, 0.8 );
00415         //glTranslatef( 0, 0, 0.8 );
00416         //gluCylinder( g_pQuadricObj, 0.06, 0, 0.2, 1.0, 0.4 );
00417 
00418         glScalef( 0.1, 0.1, 0.1 );
00419         gluCylinder( g_pQuadricObj, 0.15, 0.3, 8, 10, 8 );
00420         glTranslatef( 0, 0, 8 );
00421         gluCylinder( g_pQuadricObj, 0.6, 0, 2, 10, 4 );
00422     glPopMatrix();
00423 }
00424 //-----------------------------------------------------------------------------
00425 template <typename T>
00426 void OpenGLUsefulObj<T>::DrawOneHeadArrow ( const Vector3<T> & direction, T length )
00427 {
00428     Vector3<T> directionUnit = direction.GetUnit();
00429     Vector3<T>  rotationAxis( CGMath<T>::VectorZ ^ directionUnit );
00430     T           rotationAngle =   acos( CGMath<T>::VectorZ * directionUnit ) 
00431                                 * Math<T>::RAD_TO_DEG;
00432     glPushMatrix();
00433         glScalef( length, length, length );
00434         glRotatef( rotationAngle, 
00435             rotationAxis[0], rotationAxis[1], rotationAxis[2] );
00436         DrawOneHeadArrow();
00437     glPopMatrix();
00438 }
00439 //-----------------------------------------------------------------------------
00440 template <typename T>
00441 void OpenGLUsefulObj<T>::DrawOneHeadArrow ( const Vector3<T> & startPos, 
00442                                             const Vector3<T> & endPos )
00443 {
00444     Vector3<T>  directionUnit = (endPos - startPos);
00445     T           length        = directionUnit.Length();
00446     directionUnit.Normalized();
00447     Vector3<T>  rotationAxis( CGMath<T>::VectorZ ^ directionUnit );
00448     T           rotationAngle =   acos( CGMath<T>::VectorZ * directionUnit ) 
00449                                 * Math<T>::RAD_TO_DEG;
00450     glPushMatrix();
00451         glTranslatef( startPos[0], startPos[1], startPos[2] );
00452         glScalef( length, length, length );
00453         glRotatef( rotationAngle, 
00454             rotationAxis[0], rotationAxis[1], rotationAxis[2] );
00455         DrawOneHeadArrow();
00456     glPopMatrix();
00457 }
00458 //-----------------------------------------------------------------------------
00459 //=============================================================================
00460 // Right-Handed World Coordinate Axes
00461 //-----------------------------------------------------------------------------
00462 template <typename T>
00463 void OpenGLUsefulObj<T>::DrawWorldCoordinateAxes ()
00464 {
00465     /*
00466     glPushMatrix();
00467     glPushAttrib( GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT );
00468     glEnable( GL_LIGHTING );
00469     glEnable( GL_COLOR_MATERIAL );
00470     //glColor3f( 0, 0, 1 );
00471     glColor3f( 0.0f, 0.0f, 0.4f );
00472     DrawOneHeadArrow();     // z-axis
00473     glRotatef( -90, 1, 0, 0 );
00474     //glColor3f( 0, 1, 0 );
00475     glColor3f( 0.0f, 0.4f, 0.0f );
00476     DrawOneHeadArrow();     // y-axis
00477     glRotatef(  90, 0, 1, 0 );
00478     //glColor3f( 1, 0, 0 );
00479     glColor3f( 0.4f, 0.0f, 0.0f );
00480     DrawOneHeadArrow();     // x-axis
00481     glPopAttrib();
00482     glPopMatrix();
00483     //*/
00484 }
00485 //-----------------------------------------------------------------------------
00486 //=============================================================================
00487 END_NAMESPACE_TAPs__OpenGL
00488 //-----------------------------------------------------------------------------
00489 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00490 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines