![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsGLFns.cpp 00003 00004 Global OpenGL Functions in TAPs::OpenGL::Fn namespace 00005 00006 SUKITTI PUNAK (09/10/2010) 00007 UPDATE (09/10/2010) 00008 ******************************************************************************/ 00009 #include "STAPsGLFns.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__Fn 00015 //============================================================================= 00016 // START: GLOBAL VARIABLE(S) 00017 //----------------------------------------------------------------------------- 00018 // GLenum 00019 //GLenum g_eCullFace = GL_BACK; 00020 //GLenum g_eDrawFace = GL_FRONT_AND_BACK; 00021 //GLenum g_eDrawMode = GL_FILL; 00022 //----------------------------------------------------------------------------- 00023 //----------------------------------------------------------------------------- 00024 // END: GLOBAL VARIABLE(S) 00025 //============================================================================= 00026 00027 00028 00029 00030 //============================================================================= 00031 // START: OPENGL DEBUG/ERROR 00032 //----------------------------------------------------------------------------- 00033 // Check OpenGL Error 00042 #ifdef TAPs_DEBUG_MODE 00043 void GFnCheckGLError ( char * file, int line, int * glErrCode ) 00044 { 00045 GLenum glErr = glGetError(); 00046 int errCode = 0; 00047 while ( glErr != GL_NO_ERROR ) { 00048 #ifdef TAPs_USE_WXWIDGETS 00049 wxLogError( wxT( "GL Error #%d (%s) in file %s at line#%d" ), 00050 glErr, gluErrorString(glErr), file, line ); 00051 #else 00052 std::cout 00053 << "GL Error #" << glErr << " (" << gluErrorString(glErr) << ") " 00054 << "in file " << file << " at line#" << line << std::endl; 00055 #endif 00056 errCode = 1; 00057 glErr = glGetError(); 00058 } 00059 if ( glErrCode ) *glErrCode = errCode; 00060 } 00061 #else 00062 void GFnCheckGLError ( char *file, int line, int * glErrCode ) {} 00063 #endif 00064 #define CHECK_GL_ERROR() GFnCheckGLError( __FILE__, __LINE__ ) 00065 //----------------------------------------------------------------------------- 00066 // Check OpenGL Error with Custom Message 00078 #ifdef TAPs_DEBUG_MODE 00079 void GFnCheckGLError ( char * customMessage, int * glErrCode ) 00080 { 00081 GLenum glErr = glGetError(); 00082 int errCode = 0; 00083 while ( glErr != GL_NO_ERROR ) { 00084 #ifdef TAPs_USE_WXWIDGETS 00085 wxLogError( wxT( "%s --> GL Error #%d (%s)!" ), 00086 customMessage, glErr, gluErrorString(glErr) ); 00087 #else 00088 std::cerr 00089 << customMessage << " --> GL Error #" << glErr 00090 << " (" << gluErrorString(glErr) << ")!" << std::endl; 00091 #endif 00092 errCode = 1; 00093 glErr = glGetError(); 00094 } 00095 if ( glErrCode ) *glErrCode = errCode; 00096 } 00097 #else 00098 void GFnCheckGLError ( char * customMessage, int * glErrCode ) {} 00099 #endif 00100 #define CHECK_GL_ERROR_MESSAGE( s ) GFnCheckGLError( s ) 00101 //----------------------------------------------------------------------------- 00102 // Check OpenGL Frame Buffer Status 00111 #ifdef TAPs_DEBUG_MODE 00112 #ifdef TAPs_USE_GLSL 00113 void GFnCheckFrameBufferStatus () 00114 { 00115 //--------------------------------------------------------------- 00116 switch ( glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT ) ) { 00117 case GL_FRAMEBUFFER_COMPLETE_EXT: 00118 break; 00119 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: 00120 #ifdef TAPs_USE_WXWIDGETS 00121 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT" ) ); 00122 #else 00123 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT" << std::endl; 00124 #endif 00125 break; 00126 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: 00127 #ifdef TAPs_USE_WXWIDGETS 00128 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT" ) ); 00129 #else 00130 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT" << std::endl; 00131 #endif 00132 break; 00133 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: 00134 #ifdef TAPs_USE_WXWIDGETS 00135 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT" ) ); 00136 #else 00137 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT" << std::endl; 00138 #endif 00139 break; 00140 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: 00141 #ifdef TAPs_USE_WXWIDGETS 00142 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT" ) ); 00143 #else 00144 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT" << std::endl; 00145 #endif 00146 break; 00147 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: 00148 #ifdef TAPs_USE_WXWIDGETS 00149 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT" ) ); 00150 #else 00151 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT" << std::endl; 00152 #endif 00153 break; 00154 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: 00155 #ifdef TAPs_USE_WXWIDGETS 00156 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT" ) ); 00157 #else 00158 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT" << std::endl; 00159 #endif 00160 break; 00161 case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 00162 #ifdef TAPs_USE_WXWIDGETS 00163 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_UNSUPPORTED_EXT" ) ); 00164 #else 00165 std::cout << "ERROR (GLEXTFrameBufferObject): GL_FRAMEBUFFER_UNSUPPORTED_EXT" << std::endl; 00166 #endif 00167 break; 00168 default: 00169 #ifdef TAPs_USE_WXWIDGETS 00170 wxLogError( wxT( "ERROR (GLEXTFrameBufferObject): UNKNOWN ERROR!" ) ); 00171 #else 00172 std::cout << "ERROR (GLEXTFrameBufferObject): UNKNOWN ERROR!" << std::endl; 00173 #endif 00174 break; 00175 } 00176 } 00177 #else 00178 void GFnCheckFrameBufferStatus () {} 00179 #endif 00180 #else 00181 void GFnCheckFrameBufferStatus () {} 00182 #endif 00183 #define CHECK_FRAMEBUFFER_STATUS() GFnCheckFrameBufferStatus() 00184 //----------------------------------------------------------------------------- 00185 // END: OPENGL DEBUG/ERROR 00186 //============================================================================= 00187 00188 00189 00190 00191 //============================================================================= 00192 // START: OPENGL DRAWING 00193 //----------------------------------------------------------------------------- 00194 // Set Polygon Face Oriantation 00195 inline void SetFrontFaceCCW( bool b ) 00196 { 00197 if ( b ) glFrontFace( GL_CCW ); 00198 else glFrontFace( GL_CW ); 00199 } 00200 //----------------------------------------------------------------------------- 00201 // Set Polygon Face Oriantation 00202 inline void SetFrontFaceCW( bool b ) 00203 { 00204 SetFrontFaceCCW( !b ); 00205 } 00206 //----------------------------------------------------------------------------- 00207 // Toggle Polygon Face Oriantation: CCW <--> CW 00208 inline void ToggleFrontFaceOriantation() 00209 { 00210 GLint value; 00211 glGetIntegerv( GL_FRONT_FACE, &value ); 00212 if ( GL_CCW == value ) 00213 glEnable( GL_CW ); 00214 else 00215 glEnable( GL_CCW ); 00216 } 00217 //----------------------------------------------------------------------------- 00218 // Enable OpenGL Cull Face 00219 inline void EnableCullFace() { glEnable( GL_CULL_FACE ); } 00220 //----------------------------------------------------------------------------- 00221 // Disable OpenGL Cull Face 00222 inline void DisableCullFace() { glDisable( GL_CULL_FACE ); } 00223 //----------------------------------------------------------------------------- 00224 // Toggle OpenGL Cull Face 00225 inline void ToggleCullFace() 00226 { 00227 if ( glIsEnabled( GL_CULL_FACE ) ) glDisable( GL_CULL_FACE ); 00228 else glEnable ( GL_CULL_FACE ); 00229 } 00230 //----------------------------------------------------------------------------- 00231 // Set Drawing Mode 00232 // face (GLenum) can be 00233 // GL_FRONT_AND_BACK = 1032 00234 // GL_FRONT = 1028 00235 // GL_BACK = 1029 00236 // mode (GLenum) can be 00237 // GL_POINT = 6912 00238 // GL_LINE = 6913 00239 // GL_FILL = 6914 00240 inline void SetDrawingMode( GLenum face, GLenum mode ) 00241 { 00242 glPolygonMode( face, mode ); 00243 glCullFace( face ); 00244 glEnable( GL_CULL_FACE ); 00245 } 00246 //----------------------------------------------------------------------------- 00247 // Switch Drawing Mode 00248 // the drawing mode is switched circlely from 00249 // GL_POINT, GL_LINE, and GL_FILL 00250 void SwitchDrawingMode() 00251 { 00252 GLint DrawFaceAndMode[2]; 00253 glGetIntegerv( GL_POLYGON_MODE, DrawFaceAndMode ); 00254 switch ( DrawFaceAndMode[1] ) { 00255 case GL_POINT: 00256 DrawFaceAndMode[1] = GL_LINE; 00257 break; 00258 case GL_LINE: 00259 DrawFaceAndMode[1] = GL_FILL; 00260 break; 00261 case GL_FILL: 00262 DrawFaceAndMode[1] = GL_POINT; 00263 break; 00264 } 00265 glPolygonMode( DrawFaceAndMode[0], DrawFaceAndMode[1] ); 00266 00267 //++g_eDrawMode; 00268 //if ( g_eDrawMode > GL_FILL ) g_eDrawMode = GL_POINT; 00269 //glPolygonMode( g_eDrawFace, g_eDrawMode ); 00270 } 00271 //----------------------------------------------------------------------------- 00272 // Switch Draw Face Mode 00273 // the draw face mode is switched circlely from 00274 // GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK 00275 void SwitchDrawFaceMode() 00276 { 00277 GLint DrawFaceAndMode[2]; 00278 glGetIntegerv( GL_POLYGON_MODE, DrawFaceAndMode ); 00279 switch ( DrawFaceAndMode[0] ) { 00280 case GL_FRONT: 00281 DrawFaceAndMode[0] = GL_BACK; 00282 break; 00283 case GL_BACK: 00284 DrawFaceAndMode[0] = GL_FRONT_AND_BACK; 00285 break; 00286 case GL_FRONT_AND_BACK: 00287 DrawFaceAndMode[0] = GL_FRONT; 00288 break; 00289 } 00290 glPolygonMode( DrawFaceAndMode[0], DrawFaceAndMode[1] ); 00291 00292 // if ( GL_FRONT == g_eCullFace ) g_eCullFace = GL_BACK; 00293 // else if ( GL_BACK == g_eCullFace ) g_eCullFace = GL_FRONT_AND_BACK; 00294 // else g_eCullFace = GL_FRONT; 00295 // glPolygonMode( g_eDrawFace, g_eDrawMode ); 00296 } 00297 //----------------------------------------------------------------------------- 00298 // Switch Cull Face Mode 00299 // the cull face mode is switched circlely from 00300 // GL_BACK, GL_FRONT, and GL_FRONT_AND_BACK 00301 void SwitchCullFaceMode() 00302 { 00303 GLint cullFace; 00304 glGetIntegerv( GL_CULL_FACE_MODE, &cullFace ); 00305 switch ( cullFace ) { 00306 case GL_FRONT: 00307 cullFace = GL_BACK; 00308 break; 00309 case GL_BACK: 00310 cullFace = GL_FRONT_AND_BACK; 00311 break; 00312 case GL_FRONT_AND_BACK: 00313 cullFace = GL_FRONT; 00314 break; 00315 } 00316 glCullFace( cullFace ); 00317 00318 // if ( GL_BACK == g_eCullFace ) g_eCullFace = GL_FRONT; 00319 // else if ( GL_FRONT == g_eCullFace ) g_eCullFace = GL_FRONT_AND_BACK; 00320 // else g_eCullFace = GL_BACK; 00321 // glCullFace( g_eCullFace ); 00322 } 00323 //----------------------------------------------------------------------------- 00324 // END: OPENGL DRAWING 00325 //============================================================================= 00326 00327 00328 00329 00330 //============================================================================= 00331 // START: SHADOW FUNCTIONS 00332 //----------------------------------------------------------------------------- 00333 // Create a shadow projection matrix out of the plane equation 00334 // lightPos ==> { xPos, yPos, zPos, 1 } 00335 // planeEqn ==> a*x + b*y + c*z + d = 0 00336 template <typename T> 00337 Matrix4x4<T> CalShadowProjectionMatrix ( Vector4<T> const &lightPos, Vector4<T> const &planeEqn ) 00338 { 00339 T dot = planeEqn*lightPos; 00340 return Matrix4x4<T> ( 00341 dot - lightPos[0]*planeEqn[0], 00342 dot - lightPos[0]*planeEqn[1], 00343 dot - lightPos[0]*planeEqn[2], 00344 dot - lightPos[0]*planeEqn[3], 00345 dot - lightPos[1]*planeEqn[0], 00346 dot - lightPos[1]*planeEqn[1], 00347 dot - lightPos[1]*planeEqn[2], 00348 dot - lightPos[1]*planeEqn[3], 00349 dot - lightPos[2]*planeEqn[0], 00350 dot - lightPos[2]*planeEqn[1], 00351 dot - lightPos[2]*planeEqn[2], 00352 dot - lightPos[2]*planeEqn[3], 00353 dot - lightPos[3]*planeEqn[0], 00354 dot - lightPos[3]*planeEqn[1], 00355 dot - lightPos[3]*planeEqn[2], 00356 dot - lightPos[3]*planeEqn[3] 00357 ); 00358 } 00359 //----------------------------------------------------------------------------- 00360 // END: SHADOW FUNCTIONS 00361 //============================================================================= 00362 00363 00364 00365 00366 //============================================================================= 00367 END_NAMESPACE_TAPs__OpenGL__Fn 00368 //----------------------------------------------------------------------------- 00369 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00370 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8