![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 OpenGLModel.cpp 00003 00004 A Base (Abstract) OpenGL Model 00005 00006 SUKITTI PUNAK (11/02/2004) 00007 UPDATE (03/27/2006) 00008 ******************************************************************************/ 00009 #include "TAPsOpenGLModel.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 //----------------------------------------------------------------------------- 00017 // default constructor 00018 template <typename T> 00019 OpenGLModel<T>::OpenGLModel () : 00020 Model<T>(), 00021 OpenGLSupport() 00022 // ColDetSupport<T>() 00023 { 00024 #ifdef TAPs_DEBUG_MODE 00025 std::cout << "OpenGLModel<" << typeid(T).name() << "> constructor\n"; 00026 #endif//TAPs_DEBUG_MODE 00027 } 00028 //----------------------------------------------------------------------------- 00029 // destructor 00030 template <typename T> 00031 OpenGLModel<T>::~OpenGLModel () 00032 { 00033 #ifdef TAPs_DEBUG_MODE 00034 std::cout << "OpenGLModel<" << typeid(T).name() << "> destructor\n"; 00035 #endif//TAPs_DEBUG_MODE 00036 } 00037 //----------------------------------------------------------------------------- 00038 // Return this object info as a string 00039 template <typename T> 00040 std::string OpenGLModel<T>::StrInfo () const 00041 { 00042 std::ostringstream ss; 00043 ss << Model<T>::StrInfo(); 00044 //ss << "OpenGLModel<" << typeid(T).name() << ">"; 00045 //ss << " ID: " << GetID() << " Name: " << GetName(); 00046 return ss.str(); 00047 } 00048 //----------------------------------------------------------------------------- 00049 // Draw AABB Bounding Box 00050 template <typename T> 00051 void OpenGLModel<T>::DrawBoundingAABB () 00052 { 00053 //glColor3f( 0, 0, 0 ); 00054 //glDisable( GL_LIGHTING ); 00055 glBegin( GL_LINE_STRIP ); 00056 glVertex3f( 00057 static_cast<float>( m_paBoundingAABB[0][0] ), 00058 static_cast<float>( m_paBoundingAABB[0][1] ), 00059 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00060 glVertex3f( 00061 static_cast<float>( m_paBoundingAABB[1][0] ), 00062 static_cast<float>( m_paBoundingAABB[0][1] ), 00063 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00064 glVertex3f( 00065 static_cast<float>( m_paBoundingAABB[1][0] ), 00066 static_cast<float>( m_paBoundingAABB[1][1] ), 00067 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00068 glVertex3f( 00069 static_cast<float>( m_paBoundingAABB[0][0] ), 00070 static_cast<float>( m_paBoundingAABB[1][1] ), 00071 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00072 glVertex3f( 00073 static_cast<float>( m_paBoundingAABB[0][0] ), 00074 static_cast<float>( m_paBoundingAABB[0][1] ), 00075 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00076 glVertex3f( 00077 static_cast<float>( m_paBoundingAABB[0][0] ), 00078 static_cast<float>( m_paBoundingAABB[0][1] ), 00079 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00080 glVertex3f( 00081 static_cast<float>( m_paBoundingAABB[0][0] ), 00082 static_cast<float>( m_paBoundingAABB[1][1] ), 00083 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00084 glVertex3f( 00085 static_cast<float>( m_paBoundingAABB[1][0] ), 00086 static_cast<float>( m_paBoundingAABB[1][1] ), 00087 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00088 glVertex3f( 00089 static_cast<float>( m_paBoundingAABB[1][0] ), 00090 static_cast<float>( m_paBoundingAABB[0][1] ), 00091 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00092 glVertex3f( 00093 static_cast<float>( m_paBoundingAABB[0][0] ), 00094 static_cast<float>( m_paBoundingAABB[0][1] ), 00095 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00096 glEnd(); 00097 glBegin( GL_LINES ); 00098 glVertex3f( 00099 static_cast<float>( m_paBoundingAABB[0][0] ), 00100 static_cast<float>( m_paBoundingAABB[1][1] ), 00101 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00102 glVertex3f( 00103 static_cast<float>( m_paBoundingAABB[0][0] ), 00104 static_cast<float>( m_paBoundingAABB[1][1] ), 00105 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00106 glVertex3f( 00107 static_cast<float>( m_paBoundingAABB[1][0] ), 00108 static_cast<float>( m_paBoundingAABB[1][1] ), 00109 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00110 glVertex3f( 00111 static_cast<float>( m_paBoundingAABB[1][0] ), 00112 static_cast<float>( m_paBoundingAABB[1][1] ), 00113 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00114 glVertex3f( 00115 static_cast<float>( m_paBoundingAABB[1][0] ), 00116 static_cast<float>( m_paBoundingAABB[0][1] ), 00117 static_cast<float>( m_paBoundingAABB[1][2] ) ); 00118 glVertex3f( 00119 static_cast<float>( m_paBoundingAABB[1][0] ), 00120 static_cast<float>( m_paBoundingAABB[0][1] ), 00121 static_cast<float>( m_paBoundingAABB[0][2] ) ); 00122 glEnd(); 00123 //glEnable( GL_LIGHTING ); 00124 } 00125 //----------------------------------------------------------------------------- 00126 // Draw Ellipsoid Bounding Box 00127 template <typename T> 00128 void OpenGLModel<T>::DrawBoundingEllipsoid () 00129 { 00130 double x = m_pBoundingEllipsoid.GetX(); 00131 double y = m_pBoundingEllipsoid.GetY(); 00132 double z = m_pBoundingEllipsoid.GetZ(); 00133 double max = x > y ? x : y; 00134 max = z > max ? z : max; 00135 x /= max; 00136 y /= max; 00137 z /= max; 00138 glPushMatrix(); 00139 glTranslatef( m_pBoundingCenter[0], m_pBoundingCenter[1], m_pBoundingCenter[2] ); 00140 glScalef( x, y, z ); 00141 glutWireSphere( max, 20, 16 ); 00142 glPopMatrix(); 00143 } 00144 //----------------------------------------------------------------------------- 00145 // Draw Sphere Bounding Box 00146 template <typename T> 00147 void OpenGLModel<T>::DrawBoundingSphere () 00148 { 00149 glPushMatrix(); 00150 glTranslatef( m_pBoundingCenter[0], m_pBoundingCenter[1], m_pBoundingCenter[2] ); 00151 glutWireSphere( static_cast<double>(m_pBoundingSphere)/25.0, 20, 16 ); 00152 glutWireSphere( static_cast<double>(m_pBoundingSphere), 20, 16 ); 00153 //std::cout << "Sphere Center: " << m_pBoundingCenter << std::endl; 00154 glPopMatrix(); 00155 } 00156 //----------------------------------------------------------------------------- 00157 // Display By OpenGL 00158 template <typename T> 00159 void OpenGLModel<T>::DisplayGL ( OpenGL::Enum::DrawMode DM ) 00160 { 00161 //glPushAttrib( GL_ALL_ATTRIB_BITS ); 00162 glPushAttrib( GL_LIGHTING_BIT ); // Since ApplyMaterial() uses these bits 00163 ApplyMaterial(); 00164 //-------------------------------------------------------------------- 00165 // Do Transformation Here 00166 glPushMatrix(); 00167 //-------------------------------------------------------------------- 00168 GetTransform().TransformByOpenGLForDrawing(); 00169 00170 // This fn is called by any model that is inherited from OpenGLModel. 00171 // So modify it will affect all of the drawing of the inherited models. 00172 // Draw world coordinate axes 00173 //OpenGLUsefulObj<T>::DrawWorldCoordinateAxes(); 00174 00175 switch (DM) { 00176 00177 // POLYGON DRAWING MODE 00178 case OpenGL::Enum::POLYGON: 00179 00180 #ifdef TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00181 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00182 if ( GetDimensionOfVertexColors() == 3 ) DrawGLSLWithDSFaceVertex_RGB( GL_POLYGON ); 00183 else DrawGLSLWithDSFaceVertex_RGBA( GL_POLYGON ); 00184 break; 00185 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00186 #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00187 00188 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00189 if ( IsVertexColorsEnabled() ) { 00190 if ( GetDimensionOfVertexColors() == 3 ) DrawGLWithFaceVertexColor_RGB( GL_POLYGON ); 00191 else DrawGLWithFaceVertexColor_RGBA( GL_POLYGON ); 00192 } 00193 else DrawGL( GL_POLYGON ); 00194 #else //TAPs_ENABLE_FACE_VERTEX_COLOR 00195 DrawGL( GL_POLYGON ); 00196 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00197 break; 00198 00199 // WIRE FRAME DRAWING MODE 00200 case OpenGL::Enum::WIRE_FRAME: 00201 00202 #ifdef TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00203 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00204 if ( GetDimensionOfVertexColors() == 3 ) DrawGLSLWithDSFaceVertex_RGB( GL_LINE_LOOP ); 00205 else DrawGLSLWithDSFaceVertex_RGBA( GL_LINE_LOOP ); 00206 break; 00207 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00208 #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00209 00210 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00211 if ( IsVertexColorsEnabled() ) { 00212 if ( GetDimensionOfVertexColors() == 3 ) DrawGLWithFaceVertexColor_RGB( GL_LINE_LOOP ); 00213 else DrawGLWithFaceVertexColor_RGBA( GL_LINE_LOOP ); 00214 } 00215 else DrawGL( GL_LINE_LOOP ); 00216 #else //TAPs_ENABLE_FACE_VERTEX_COLOR 00217 DrawGL( GL_LINE_LOOP ); 00218 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00219 break; 00220 00221 // POINT DRAWING MODE 00222 case OpenGL::Enum::POINT: 00223 00224 #ifdef TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00225 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00226 if ( GetDimensionOfVertexColors() == 3 ) DrawGLSLWithDSFaceVertex_RGB( GL_POINTS ); 00227 else DrawGLSLWithDSFaceVertex_RGBA( GL_POINTS ); 00228 break; 00229 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00230 #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00231 00232 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00233 if ( IsVertexColorsEnabled() ) { 00234 if ( GetDimensionOfVertexColors() == 3 ) DrawGLWithFaceVertexColor_RGB( GL_POINTS ); 00235 else DrawGLWithFaceVertexColor_RGBA( GL_POINTS ); 00236 } 00237 else DrawGL( GL_POINTS ); 00238 #else //TAPs_ENABLE_FACE_VERTEX_COLOR 00239 DrawGL( GL_POINTS ); 00240 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00241 break; 00242 00243 // POLYGON WITH WIRE FRAME DRAWING MODE 00244 case OpenGL::Enum:: POLYGON_WITH_WIRE_FRAME: 00245 00246 #ifdef TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00247 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00248 if ( GetDimensionOfVertexColors() == 3 ) { 00249 DrawGLSLWithDSFaceVertex_RGB( GL_POLYGON ); 00250 DrawGLSLWithDSFaceVertex_RGB( GL_LINE_LOOP ); 00251 } 00252 else { 00253 DrawGLSLWithDSFaceVertex_RGBA( GL_POLYGON ); 00254 DrawGLSLWithDSFaceVertex_RGBA( GL_LINE_LOOP ); 00255 } 00256 break; 00257 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00258 #endif//TAPs_RENDER_BY_GLSL_DS_FACE_VERTEX 00259 00260 // Draw the object with GL_POLYGON_OFFSET enabled 00261 glEnable( GL_POLYGON_OFFSET_FILL ); 00262 glPolygonOffset( 1.0, 1.0 ); 00263 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00264 if ( IsVertexColorsEnabled() ) { 00265 if ( GetDimensionOfVertexColors() == 3 ) DrawGLWithFaceVertexColor_RGB( GL_POLYGON ); 00266 else DrawGLWithFaceVertexColor_RGBA( GL_POLYGON ); 00267 } 00268 else DrawGL( GL_POLYGON ); 00269 #else //TAPs_ENABLE_FACE_VERTEX_COLOR 00270 DrawGL( GL_POLYGON ); 00271 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00272 glDisable( GL_POLYGON_OFFSET_FILL ); 00273 // Draw the wire frame of the object 00274 glDisable( GL_LIGHTING ); 00275 //glDisable( GL_DEPTH_TEST ); 00276 glColor3ub( 255, 200, 0 ); 00277 #ifdef TAPs_ENABLE_FACE_VERTEX_COLOR 00278 if ( IsVertexColorsEnabled() ) { 00279 if ( GetDimensionOfVertexColors() == 3 ) DrawGLWithFaceVertexColor_RGB( GL_LINE_LOOP ); 00280 else DrawGLWithFaceVertexColor_RGBA( GL_LINE_LOOP ); 00281 } 00282 else DrawGL( GL_LINE_LOOP ); 00283 #else //TAPs_ENABLE_FACE_VERTEX_COLOR 00284 DrawGL( GL_LINE_LOOP ); 00285 #endif//TAPs_ENABLE_FACE_VERTEX_COLOR 00286 glEnable( GL_LIGHTING ); 00287 //glEnable( GL_DEPTH_TEST ); 00288 break; 00289 } 00290 //-------------------------------------------------------------------- 00291 glPopMatrix(); 00292 //-------------------------------------------------------------------- 00293 glPopAttrib(); 00294 } 00295 //----------------------------------------------------------------------------- 00296 //============================================================================= 00297 END_NAMESPACE_TAPs__OpenGL 00298 //----------------------------------------------------------------------------- 00299 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00300 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8