![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsGLSLFns.cpp 00003 00004 Based on 3DLabs Inc. Ltd. and aGLSL.h by Martin Christen 00005 00006 SUKITTI PUNAK (06/08/2005) 00007 UPDATE (09/10/2006) 00008 ******************************************************************************/ 00009 #include "TAPsGLSLFns.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 // Global Variables 00017 //----------------------------------------------------------------------------- 00018 char * gbGLSLErrStr[] = { 00019 "GLSL is not supported/enabled!", // ERR00 00020 "Not a valid program object!", // ERR01 00021 "Not a valid object!", // ERR02 00022 "Out of memory!", // ERR03 00023 "Compiler log is not available!", // ERR04 00024 "Unknown compiler error!" // ERR05 00025 "Linker log is not available!", // ERR06 00026 "Unknown linker error!", // ERR07 00027 "" // ERR08 00028 }; 00029 bool gbIsGLSLSupported = false; 00030 bool gbIsInitialized = false; 00031 //----------------------------------------------------------------------------- 00032 //============================================================================= 00033 // Global Functions 00034 //----------------------------------------------------------------------------- 00035 // Check GL Error 00036 int GFnCheckGLError ( char *file, int line ) 00037 { 00038 GLenum glErr = glGetError(); 00039 int errCode = 0; 00040 while ( glErr != GL_NO_ERROR ) { 00041 #ifdef TAPs_USE_WXWIDGETS 00042 wxLogError( wxT( "GL Error #%d (%s) in file %s at line#%d" ), 00043 glErr, gluErrorString(glErr), file, line ); 00044 #else 00045 std::cerr << "GL Error #" << glErr << " (" << gluErrorString(glErr) << ") " 00046 << "in file " << file << " at line#" << line << std::endl; 00047 #endif 00048 errCode = 1; 00049 glErr = glGetError(); 00050 } 00051 return errCode; 00052 } 00053 #define CHECK_GL_ERROR() GFnCheckGLError( __FILE__, __LINE__ ) 00054 //----------------------------------------------------------------------------- 00055 // Get file length 00056 unsigned long GFnGetFileLength ( std::ifstream & file ) 00057 { 00058 if ( !file.good() ) return 0; 00059 //unsigned long pos = file.tellg(); 00060 file.seekg( 0, std::ios::end ); 00061 unsigned long len = file.tellg(); 00062 file.seekg( std::ios::beg ); 00063 #ifdef TAPs_DEBUG_MODE 00064 #ifdef TAPs_USE_WXWIDGETS 00065 wxLogError( wxT( "Length of File: %d" ), len ); 00066 #else 00067 std::cout << "Length of File: " << len << std::endl; 00068 #endif 00069 #endif 00070 return len; 00071 } 00072 //----------------------------------------------------------------------------- 00073 // Get file length 00074 unsigned long GFnGetFileLength ( char * fileName ) 00075 { 00076 // Returns the size in bytes of the shader fileName. 00077 // If an error occurred, it returns -1. 00078 int fd; 00079 int count = -1; 00080 // 00081 // Open the file, seek to the end to find its length 00082 // 00083 #ifdef WIN32 /*[*/ 00084 fd = _open(fileName, _O_RDONLY); 00085 if (fd != -1) 00086 { 00087 count = _lseek(fd, 0, SEEK_END) + 1; 00088 _close(fd); 00089 } 00090 #else /*][*/ 00091 fd = open(fileName, O_RDONLY); 00092 if (fd != -1) 00093 { 00094 count = lseek(fd, 0, SEEK_END) + 1; 00095 close(fd); 00096 } 00097 #endif /*]*/ 00098 00099 return count; 00100 } 00101 //----------------------------------------------------------------------------- 00102 bool GFnInitGLSL () 00103 { 00104 if ( gbIsGLSLSupported ) return true; // GLSL has already been initialized 00105 //--------------------------------------------------------------- 00106 // Initialize the "OpenGL Extension Wrangler" library 00107 if ( gbIsInitialized ) return true; 00108 gbIsInitialized = true; 00109 GLenum err = glewInit(); 00110 if ( GLEW_OK != err ) { 00111 #ifdef TAPs_USE_WXWIDGETS 00112 wxLogError( wxT( "Error: %s" ), glewGetErrorString( err ) ); 00113 #else 00114 std::cerr << "Error: " << glewGetErrorString( err ) << std::endl; 00115 #endif 00116 gbIsGLSLSupported = false; 00117 return gbIsGLSLSupported; 00118 } 00119 //--------------------------------------------------------------- 00120 // OpenGL Info 00121 #ifdef TAPs_DEBUG_MODE 00122 #ifdef TAPs_USE_WXWIDGETS 00123 wxLogError( wxT( "OpenGL Vendor: %s" ), glGetString( GL_VENDOR ) ); 00124 wxLogError( wxT( "OpenGL Renderer: %s" ), glGetString( GL_RENDERER ) ); 00125 wxLogError( wxT( "OpenGL Version: %s" ), glGetString( GL_VERSION ) ); 00126 #else 00127 std::cout << "OpenGL Vendor: " << glGetString( GL_VENDOR ) << "\n"; 00128 std::cout << "OpenGL Renderer: " << glGetString( GL_RENDERER ) << "\n"; 00129 std::cout << "OpenGL Version: " << glGetString( GL_VERSION ) << "\n"; 00130 #endif 00131 #endif 00132 //--------------------------------------------------------------- 00133 GFnCheckGLSL(); 00134 //--------------------------------------------------------------- 00135 return gbIsGLSLSupported; 00136 } 00137 //----------------------------------------------------------------------------- 00138 bool GFnCheckGLSL () 00139 { 00140 if ( gbIsGLSLSupported ) return true; // GLSL is available and initailized 00141 //--------------------------------------------------------------- 00142 if ( !gbIsInitialized ) GFnInitGLSL(); 00143 //--------------------------------------------------------------- 00144 #ifdef TAPs_DEBUG_MODE 00145 #ifdef TAPs_USE_WXWIDGETS 00146 if ( GLEW_VERSION_2_0 ) 00147 wxLogError( wxT( "OpenGL 2.0 is available!" ) ); 00148 else if ( GLEW_VERSION_1_5 ) 00149 wxLogError( wxT( "OpenGL 1.5 is available!" ) ); 00150 else if ( GLEW_VERSION_1_4 ) 00151 wxLogError( wxT( "OpenGL 1.4 is available!" ) ); 00152 else if ( GLEW_VERSION_1_3 ) 00153 wxLogError( wxT( "OpenGL 1.3 is available!" ) ); 00154 else if ( GLEW_VERSION_1_2 ) 00155 wxLogError( wxT( "OpenGL 1.2 is available!" ) ); 00156 #else 00157 if ( GLEW_VERSION_2_0 ) 00158 std::cout << "OpenGL 2.0 is available!\n"; 00159 else if ( GLEW_VERSION_1_5 ) 00160 std::cout << "OpenGL 1.5 is available!\n"; 00161 else if ( GLEW_VERSION_1_4 ) 00162 std::cout << "OpenGL 1.4 is available!\n"; 00163 else if ( GLEW_VERSION_1_3 ) 00164 std::cout << "OpenGL 1.3 is available!\n"; 00165 else if ( GLEW_VERSION_1_2 ) 00166 std::cout << "OpenGL 1.2 is available!\n"; 00167 #endif 00168 #endif 00169 //--------------------------------------------------------------- 00170 // Ensure we have the necessary OpenGL Shading Language extensions. 00171 if (glewGetExtension("GL_ARB_fragment_shader") != GL_TRUE || 00172 glewGetExtension("GL_ARB_vertex_shader") != GL_TRUE || 00173 glewGetExtension("GL_ARB_shader_objects") != GL_TRUE ) //|| 00174 //glewGetExtension("GL_ARB_shading_language_100") != GL_TRUE) 00175 { 00176 #ifdef TAPs_DEBUG_MODE 00177 #ifdef TAPs_USE_WXWIDGETS 00178 wxLogError( wxT( "[FAILIED] OpenGL Shading Language is NOT available!" ) ); 00179 #else 00180 std::cout << "[FAILIED] OpenGL Shading Language is NOT available!\n"; 00181 #endif 00182 #endif 00183 gbIsGLSLSupported = false; 00184 } 00185 else { 00186 #ifdef TAPs_DEBUG_MODE 00187 #ifdef TAPs_USE_WXWIDGETS 00188 wxLogError( wxT( "[OK] OpenGL Shading Language is available!" ) ); 00189 #else 00190 std::cout << "[OK] OpenGL Shading Language is available!\n"; 00191 #endif 00192 #endif 00193 gbIsGLSLSupported = true; 00194 } 00195 //--------------------------------------------------------------- 00196 return gbIsGLSLSupported; 00197 } 00198 //----------------------------------------------------------------------------- 00199 bool GFnCheckGL2 () 00200 { 00201 if ( !gbIsInitialized ) GFnInitGLSL(); 00202 return GLEW_VERSION_2_0 == GL_TRUE; 00203 } 00204 //----------------------------------------------------------------------------- 00205 00206 00207 //****************************************************************************** 00208 //============================================================================= 00209 // class GLSLProgramObject 00210 //----------------------------------------------------------------------------- 00211 // Static Variable(s) 00212 bool GLSLProgramObject::m_stcbShaderActive = true; 00213 //----------------------------------------------------------------------------- 00214 // Constructor 00215 GLSLProgramObject::GLSLProgramObject () : 00216 m_uiProgramObject( NULL ), 00217 m_bLinkStatus( false ), 00218 m_cpLinkerLog( NULL ), 00219 m_bManageMemory( false ) 00220 { 00221 if ( GFnInitGLSL() ) { 00222 m_uiProgramObject = glCreateProgram(); 00223 //CHECK_GL_ERROR(); 00224 } 00225 else { 00226 #ifdef TAPs_USE_WXWIDGETS 00227 wxLogError( wxT( "ERROR: OpenGL Shading Language is NOT available!" ) ); 00228 #else 00229 std::cerr << "ERROR: OpenGL Shading Language is NOT available!\n"; 00230 #endif 00231 } 00232 } 00233 //----------------------------------------------------------------------------- 00234 // Destructor 00235 GLSLProgramObject::~GLSLProgramObject () 00236 { 00237 //--------------------------------------------------------------- 00238 if ( m_cpLinkerLog ) { 00239 delete [] m_cpLinkerLog; 00240 m_cpLinkerLog = NULL; 00241 } 00242 if ( gbIsGLSLSupported ) 00243 { 00244 for (unsigned int i = 0; i < m_vShaderList.size(); ++i ) { 00245 glDetachShader( m_uiProgramObject, m_vShaderList[i]->m_uiShaderObject ); 00246 CHECK_GL_ERROR(); // if get an error here, the program object is deleted first 00247 // the m_uiProgramObject must be deleted last 00248 if ( m_bManageMemory ) delete m_vShaderList[i]; 00249 } 00250 glDeleteProgram( m_uiProgramObject ); 00251 CHECK_GL_ERROR(); 00252 } 00253 } 00254 //----------------------------------------------------------------------------- 00255 // IsGLSLEnabled 00256 bool GLSLProgramObject::IsGLSLEnabled () 00257 { 00258 return gbIsGLSLSupported; 00259 } 00260 //----------------------------------------------------------------------------- 00261 // Add a vertex or fragment shader 00262 void GLSLProgramObject::AddShader ( GLSLShader * glslShader ) 00263 { 00264 if ( !gbIsGLSLSupported ) return; 00265 if ( glslShader == NULL ) return; 00266 if ( !glslShader->m_bCompileStatus ) { 00267 #ifdef TAPs_DEBUG_MODE 00268 #ifdef TAPs_USE_WXWIDGETS 00269 wxLogError( wxT( "Trying to compile the shader program ... " ) ); 00270 if ( !glslShader->Compile() ) { 00271 wxLogError( wxT( " ... compile ERROR!" ) ); 00272 return; 00273 } 00274 else { 00275 wxLogError( wxT( " ... Compilation Passed!" ) ); 00276 } 00277 #else 00278 std::cout << "Trying to compile the shader program ... "; 00279 if ( !glslShader->Compile() ) { 00280 std::cout << " ... compile ERROR!\n"; 00281 return; 00282 } 00283 else { 00284 std::cout << " ... Compilation Passed!\n"; 00285 } 00286 #endif 00287 #endif 00288 } 00289 m_vShaderList.push_back( glslShader ); 00290 } 00291 //----------------------------------------------------------------------------- 00292 // Link 00293 bool GLSLProgramObject::Link () 00294 { 00295 if ( !gbIsGLSLSupported ) return false; 00296 unsigned int i; 00297 //---------------------------------------------------------------- 00298 if ( m_bLinkStatus ) { // already linked, detach everything 00299 #ifdef TAPs_DEBUG_MODE 00300 #ifdef TAPs_USE_WXWIDGETS 00301 wxLogError( wxT( "Object is already linked, trying to link it again ..." ) ); 00302 #else 00303 std::cout << "Object is already linked, trying to link it again ..."; 00304 #endif 00305 #endif 00306 for ( i = 0; i < m_vShaderList.size(); ++i ) { 00307 glDetachShader( m_uiProgramObject, m_vShaderList[i]->m_uiShaderObject ); 00308 CHECK_GL_ERROR(); 00309 } 00310 } 00311 //---------------------------------------------------------------- 00312 for ( i = 0; i < m_vShaderList.size(); ++i ) { 00313 glAttachShader( m_uiProgramObject, m_vShaderList[i]->m_uiShaderObject ); 00314 CHECK_GL_ERROR(); 00315 } 00316 //---------------------------------------------------------------- 00317 int linked; 00318 glLinkProgram( m_uiProgramObject ); 00319 CHECK_GL_ERROR(); 00320 glGetProgramiv( m_uiProgramObject, GL_LINK_STATUS, &linked ); 00321 CHECK_GL_ERROR(); 00322 //---------------------------------------------------------------- 00323 if ( linked ) { 00324 m_bLinkStatus = true; 00325 } 00326 else { 00327 m_bLinkStatus = false; 00328 #ifdef TAPs_USE_WXWIDGETS 00329 wxLogError( wxT( "ERROR: GLSL Linker Error" ) ); 00330 #else 00331 std::cerr << "ERROR: GLSL Linker Error" << std::endl; 00332 #endif 00333 } 00334 //---------------------------------------------------------------- 00335 return m_bLinkStatus; 00336 } 00337 //----------------------------------------------------------------------------- 00338 // Get linker messages 00339 char * GLSLProgramObject::GetLinkerLog () 00340 { 00341 //---------------------------------------------------------------- 00342 if ( !gbIsGLSLSupported ) return gbGLSLErrStr[0]; 00343 int bLen = 0, sLen = 0; 00344 //---------------------------------------------------------------- 00345 if ( m_uiProgramObject == NULL ) return gbGLSLErrStr[2]; 00346 glGetProgramiv( m_uiProgramObject, GL_INFO_LOG_LENGTH, &bLen ); 00347 CHECK_GL_ERROR(); 00348 //---------------------------------------------------------------- 00349 if ( bLen > 1 ) { 00350 if ( m_cpLinkerLog ) { 00351 delete [] m_cpLinkerLog; 00352 m_cpLinkerLog = NULL; 00353 } 00354 if ( (m_cpLinkerLog = new GLchar[bLen]) == NULL ) { 00355 //if ( (m_cpLinkerLog = (GLchar *) malloc (bLen*sizeof(char))) == NULL ) { 00356 #ifdef TAPs_USE_WXWIDGETS 00357 wxLogError( wxT( "ERROR: Cound not allocate linker log buffer" ) ); 00358 #else 00359 std::cerr << "ERROR: Cound not allocate linker log buffer\n"; 00360 #endif 00361 return gbGLSLErrStr[3]; 00362 } 00363 glGetProgramInfoLog( m_uiProgramObject, bLen, &sLen, m_cpLinkerLog ); 00364 CHECK_GL_ERROR(); 00365 } 00366 if ( m_cpLinkerLog ) { 00367 return static_cast<char *>( m_cpLinkerLog ); 00368 } 00369 else { 00370 return gbGLSLErrStr[6]; 00371 } 00372 //---------------------------------------------------------------- 00373 return gbGLSLErrStr[7]; 00374 } 00375 //----------------------------------------------------------------------------- 00376 // Begin this shader. OpenGL calls will go through shader 00377 void GLSLProgramObject::BeginGLSL () 00378 { 00379 if ( !gbIsGLSLSupported ) return; 00380 if ( m_uiProgramObject == NULL ) return; 00381 if ( !m_stcbShaderActive ) return; 00382 if ( m_bLinkStatus ) { 00383 glUseProgram( m_uiProgramObject ); 00384 CHECK_GL_ERROR(); 00385 } 00386 } 00387 //----------------------------------------------------------------------------- 00388 // Stop using this shader. OpenGL calls will go through regular pipeline. 00389 void GLSLProgramObject::EndGLSL () 00390 { 00391 if ( !gbIsGLSLSupported ) return; 00392 if ( !m_stcbShaderActive ) return; 00393 glUseProgram( NULL ); 00394 CHECK_GL_ERROR(); 00395 } 00396 //----------------------------------------------------------------------------- 00397 bool GLSLProgramObject::SetUniform1f ( char *var, GLfloat x ) 00398 { 00399 if ( !gbIsGLSLSupported ) return false; 00400 if ( !m_stcbShaderActive ) return true; 00401 //---------------------------------------------------------------- 00402 GLint loc = GetUniLoc( var ); 00403 if ( loc == -1 ) return false; // cannot find variable 00404 //---------------------------------------------------------------- 00405 glUniform1f( loc, x ); 00406 return true; 00407 } 00408 //----------------------------------------------------------------------------- 00409 bool GLSLProgramObject::SetUniform2f ( char *var, GLfloat x, GLfloat y ) 00410 { 00411 if ( !gbIsGLSLSupported ) return false; 00412 if ( !m_stcbShaderActive ) return true; 00413 //---------------------------------------------------------------- 00414 GLint loc = GetUniLoc( var ); 00415 if ( loc == -1 ) return false; // cannot find variable 00416 //---------------------------------------------------------------- 00417 glUniform2f( loc, x, y ); 00418 return true; 00419 } 00420 //----------------------------------------------------------------------------- 00421 bool GLSLProgramObject::SetUniform3f ( char *var, GLfloat x, GLfloat y, GLfloat z ) 00422 { 00423 if ( !gbIsGLSLSupported ) return false; 00424 if ( !m_stcbShaderActive ) return true; 00425 //---------------------------------------------------------------- 00426 GLint loc = GetUniLoc( var ); 00427 if ( loc == -1 ) return false; // cannot find variable 00428 //---------------------------------------------------------------- 00429 glUniform3f( loc, x, y, z ); 00430 return true; 00431 } 00432 //----------------------------------------------------------------------------- 00433 bool GLSLProgramObject::SetUniform4f ( char *var, GLfloat x, GLfloat y, GLfloat z, GLfloat w ) 00434 { 00435 if ( !gbIsGLSLSupported ) return false; 00436 if ( !m_stcbShaderActive ) return true; 00437 //---------------------------------------------------------------- 00438 GLint loc = GetUniLoc( var ); 00439 if ( loc == -1 ) return false; // cannot find variable 00440 //---------------------------------------------------------------- 00441 glUniform4f( loc, x, y, z, w ); 00442 return true; 00443 } 00444 //----------------------------------------------------------------------------- 00445 bool GLSLProgramObject::SetUniform1i ( char *var, GLint x ) 00446 { 00447 if ( !gbIsGLSLSupported ) return false; 00448 if ( !m_stcbShaderActive ) return true; 00449 //---------------------------------------------------------------- 00450 GLint loc = GetUniLoc( var ); 00451 if ( loc == -1 ) return false; // cannot find variable 00452 //---------------------------------------------------------------- 00453 glUniform1i( loc, x ); 00454 return true; 00455 } 00456 //----------------------------------------------------------------------------- 00457 bool GLSLProgramObject::SetUniform2i ( char *var, GLint x, GLint y ) 00458 { 00459 if ( !gbIsGLSLSupported ) return false; 00460 if ( !m_stcbShaderActive ) return true; 00461 //---------------------------------------------------------------- 00462 GLint loc = GetUniLoc( var ); 00463 if ( loc == -1 ) return false; // cannot find variable 00464 //---------------------------------------------------------------- 00465 glUniform2i( loc, x, y ); 00466 return true; 00467 } 00468 //----------------------------------------------------------------------------- 00469 bool GLSLProgramObject::SetUniform3i ( char *var, GLint x, GLint y, GLint z ) 00470 { 00471 if ( !gbIsGLSLSupported ) return false; 00472 if ( !m_stcbShaderActive ) return true; 00473 //---------------------------------------------------------------- 00474 GLint loc = GetUniLoc( var ); 00475 if ( loc == -1 ) return false; // cannot find variable 00476 //---------------------------------------------------------------- 00477 glUniform3i( loc, x, y, z ); 00478 return true; 00479 } 00480 //----------------------------------------------------------------------------- 00481 bool GLSLProgramObject::SetUniform4i ( char *var, GLint x, GLint y, GLint z, GLint w ) 00482 { 00483 if ( !gbIsGLSLSupported ) return false; 00484 if ( !m_stcbShaderActive ) return true; 00485 //---------------------------------------------------------------- 00486 GLint loc = GetUniLoc( var ); 00487 if ( loc == -1 ) return false; // cannot find variable 00488 //---------------------------------------------------------------- 00489 glUniform4i( loc, x, y, z, w ); 00490 return true; 00491 } 00492 //----------------------------------------------------------------------------- 00493 bool GLSLProgramObject::SetUniform1fv ( char *var, GLsizei count, GLfloat *val ) 00494 { 00495 if ( !gbIsGLSLSupported ) return false; 00496 if ( !m_stcbShaderActive ) return true; 00497 //---------------------------------------------------------------- 00498 GLint loc = GetUniLoc( var ); 00499 if ( loc == -1 ) return false; // cannot find variable 00500 //---------------------------------------------------------------- 00501 glUniform1fv( loc, count, val ); 00502 return true; 00503 } 00504 //----------------------------------------------------------------------------- 00505 bool GLSLProgramObject::SetUniform2fv ( char *var, GLsizei count, GLfloat *val ) 00506 { 00507 if ( !gbIsGLSLSupported ) return false; 00508 if ( !m_stcbShaderActive ) return true; 00509 //---------------------------------------------------------------- 00510 GLint loc = GetUniLoc( var ); 00511 if ( loc == -1 ) return false; // cannot find variable 00512 //---------------------------------------------------------------- 00513 glUniform2fv( loc, count, val ); 00514 return true; 00515 } 00516 //----------------------------------------------------------------------------- 00517 bool GLSLProgramObject::SetUniform3fv ( char *var, GLsizei count, GLfloat *val ) 00518 { 00519 if ( !gbIsGLSLSupported ) return false; 00520 if ( !m_stcbShaderActive ) return true; 00521 //---------------------------------------------------------------- 00522 GLint loc = GetUniLoc( var ); 00523 if ( loc == -1 ) return false; // cannot find variable 00524 //---------------------------------------------------------------- 00525 glUniform3fv( loc, count, val ); 00526 return true; 00527 } 00528 //----------------------------------------------------------------------------- 00529 bool GLSLProgramObject::SetUniform4fv ( char *var, GLsizei count, GLfloat *val ) 00530 { 00531 if ( !gbIsGLSLSupported ) return false; 00532 if ( !m_stcbShaderActive ) return true; 00533 //---------------------------------------------------------------- 00534 GLint loc = GetUniLoc( var ); 00535 if ( loc == -1 ) return false; // cannot find variable 00536 //---------------------------------------------------------------- 00537 glUniform4fv( loc, count, val ); 00538 return true; 00539 } 00540 //----------------------------------------------------------------------------- 00541 bool GLSLProgramObject::SetUniform1iv ( char *var, GLsizei count, GLint *val ) 00542 { 00543 if ( !gbIsGLSLSupported ) return false; 00544 if ( !m_stcbShaderActive ) return true; 00545 //---------------------------------------------------------------- 00546 GLint loc = GetUniLoc( var ); 00547 if ( loc == -1 ) return false; // cannot find variable 00548 //---------------------------------------------------------------- 00549 glUniform1iv( loc, count, val ); 00550 return true; 00551 } 00552 //----------------------------------------------------------------------------- 00553 bool GLSLProgramObject::SetUniform2iv ( char *var, GLsizei count, GLint *val ) 00554 { 00555 if ( !gbIsGLSLSupported ) return false; 00556 if ( !m_stcbShaderActive ) return true; 00557 //---------------------------------------------------------------- 00558 GLint loc = GetUniLoc( var ); 00559 if ( loc == -1 ) return false; // cannot find variable 00560 //---------------------------------------------------------------- 00561 glUniform2iv( loc, count, val ); 00562 return true; 00563 } 00564 //----------------------------------------------------------------------------- 00565 bool GLSLProgramObject::SetUniform3iv ( char *var, GLsizei count, GLint *val ) 00566 { 00567 if ( !gbIsGLSLSupported ) return false; 00568 if ( !m_stcbShaderActive ) return true; 00569 //---------------------------------------------------------------- 00570 GLint loc = GetUniLoc( var ); 00571 if ( loc == -1 ) return false; // cannot find variable 00572 //---------------------------------------------------------------- 00573 glUniform3iv( loc, count, val ); 00574 return true; 00575 } 00576 //----------------------------------------------------------------------------- 00577 bool GLSLProgramObject::SetUniform4iv ( char *var, GLsizei count, GLint *val ) 00578 { 00579 if ( !gbIsGLSLSupported ) return false; 00580 if ( !m_stcbShaderActive ) return true; 00581 //---------------------------------------------------------------- 00582 GLint loc = GetUniLoc( var ); 00583 if ( loc == -1 ) return false; // cannot find variable 00584 //---------------------------------------------------------------- 00585 glUniform4iv( loc, count, val ); 00586 return true; 00587 } 00588 //----------------------------------------------------------------------------- 00589 bool GLSLProgramObject::SetUniformMatrix2fv ( char *var, GLsizei count, GLboolean transpose, GLfloat *val ) 00590 { 00591 if ( !gbIsGLSLSupported ) return false; 00592 if ( !m_stcbShaderActive ) return true; 00593 //---------------------------------------------------------------- 00594 GLint loc = GetUniLoc( var ); 00595 if ( loc == -1 ) return false; // cannot find variable 00596 //---------------------------------------------------------------- 00597 glUniformMatrix2fv( loc, count, transpose, val ); 00598 return true; 00599 } 00600 //----------------------------------------------------------------------------- 00601 bool GLSLProgramObject::SetUniformMatrix3fv ( char *var, GLsizei count, GLboolean transpose, GLfloat *val ) 00602 { 00603 if ( !gbIsGLSLSupported ) return false; 00604 if ( !m_stcbShaderActive ) return true; 00605 //---------------------------------------------------------------- 00606 GLint loc = GetUniLoc( var ); 00607 if ( loc == -1 ) return false; // cannot find variable 00608 //---------------------------------------------------------------- 00609 glUniformMatrix3fv( loc, count, transpose, val ); 00610 return true; 00611 } 00612 //----------------------------------------------------------------------------- 00613 bool GLSLProgramObject::SetUniformMatrix4fv ( char *var, GLsizei count, GLboolean transpose, GLfloat *val ) 00614 { 00615 if ( !gbIsGLSLSupported ) return false; 00616 if ( !m_stcbShaderActive ) return true; 00617 //---------------------------------------------------------------- 00618 GLint loc = GetUniLoc( var ); 00619 if ( loc == -1 ) return false; // cannot find variable 00620 //---------------------------------------------------------------- 00621 glUniformMatrix4fv( loc, count, transpose, val ); 00622 return true; 00623 } 00624 //----------------------------------------------------------------------------- 00625 void GLSLProgramObject::GetUniformfv ( char *name, GLfloat *val ) 00626 { 00627 if ( !gbIsGLSLSupported ) return; 00628 GLint loc = GetUniLoc( name ); 00629 /* 00630 GLint loc = glGetUniformLocation( m_uiProgramObject, name ); 00631 if ( loc == -1 ) { 00632 #ifdef TAPs_USE_WXWIDGETS 00633 wxLogError( wxT( "ERROR: Cannot find uniform variable \"%s\"" ), 00634 name ); 00635 #else 00636 std::cerr << "ERROR: cannot find uniform variable \"" 00637 << name << "\"\n"; 00638 #endif 00639 } 00640 //*/ 00641 glGetUniformfv( m_uiProgramObject, loc, val ); 00642 } 00643 //----------------------------------------------------------------------------- 00644 void GLSLProgramObject::GetUniformiv ( char *name, GLint *val ) 00645 { 00646 if ( !gbIsGLSLSupported ) return; 00647 GLint loc = GetUniLoc( name ); 00648 /* 00649 GLint loc = glGetUniformLocation( m_uiProgramObject, name ); 00650 if ( loc == -1 ) { 00651 #ifdef TAPs_USE_WXWIDGETS 00652 wxLogError( wxT( "ERROR: Cannot find uniform variable \"%s\"" ), 00653 name ); 00654 #else 00655 std::cerr << "ERROR: cannot find uniform variable \"" 00656 << name << "\"\n"; 00657 #endif 00658 } 00659 //*/ 00660 glGetUniformiv( m_uiProgramObject, loc, val ); 00661 } 00662 //----------------------------------------------------------------------------- 00663 bool GLSLProgramObject::SetVertexAttrib1f ( GLuint index, GLfloat x ) 00664 { 00665 if ( !gbIsGLSLSupported ) return false; 00666 glVertexAttrib1f( index, x ); 00667 return true; 00668 } 00669 //----------------------------------------------------------------------------- 00670 bool GLSLProgramObject::SetVertexAttrib2f ( GLuint index, GLfloat x, GLfloat y ) 00671 { 00672 if ( !gbIsGLSLSupported ) return false; 00673 glVertexAttrib2f( index, x, y ); 00674 return true; 00675 } 00676 //----------------------------------------------------------------------------- 00677 bool GLSLProgramObject::SetVertexAttrib3f ( GLuint index, GLfloat x, GLfloat y, GLfloat z ) 00678 { 00679 if ( !gbIsGLSLSupported ) return false; 00680 glVertexAttrib3f( index, x, y, z ); 00681 return true; 00682 } 00683 //----------------------------------------------------------------------------- 00684 bool GLSLProgramObject::SetVertexAttrib4f ( GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w ) 00685 { 00686 if ( !gbIsGLSLSupported ) return false; 00687 glVertexAttrib4f( index, x, y, z, w ); 00688 return true; 00689 } 00690 //----------------------------------------------------------------------------- 00691 GLint GLSLProgramObject::GetUniLoc ( const GLchar *name ) 00692 { 00693 GLint loc = glGetUniformLocation( m_uiProgramObject, name ); 00694 if ( loc == -1 ) { 00695 #ifdef TAPs_USE_WXWIDGETS 00696 wxLogError( wxT( "ERROR: Cannot find uniform variable \"%s\"" ), 00697 name ); 00698 #else 00699 std::cerr << "ERROR: Cannot find uniform variable \"" 00700 << name << "\"\n"; 00701 #endif 00702 } 00703 CHECK_GL_ERROR(); 00704 return loc; 00705 } 00706 //----------------------------------------------------------------------------- 00707 //============================================================================= 00708 //****************************************************************************** 00709 00710 00711 //****************************************************************************** 00712 //============================================================================= 00713 // class GLSLShader 00714 //----------------------------------------------------------------------------- 00715 // Constructor 00716 GLSLShader::GLSLShader () : 00717 m_iShaderType( 0 ), // 1 = Vertex Shader; 2 = Fragment Shader; else = NONE 00718 m_uiShaderObject( NULL ), // program object 00719 m_cpShaderSource( NULL ), // ASCII source code 00720 m_bCompileStatus( false ), // true if shader is compiled 00721 m_cpCompilerLog( NULL ), // compiler log message 00722 m_bMemAlloc( false ) // true if shader allocated memory 00723 { 00724 GFnInitGLSL(); 00725 } 00726 //----------------------------------------------------------------------------- 00727 // Destructor 00728 GLSLShader::~GLSLShader () 00729 { 00730 if ( m_cpCompilerLog ) { 00731 delete [] m_cpCompilerLog; 00732 m_cpCompilerLog = NULL; 00733 } 00734 if ( m_cpShaderSource ) { 00735 if ( m_bMemAlloc ) delete [] m_cpShaderSource; 00736 m_cpShaderSource = NULL; 00737 m_bMemAlloc = false; 00738 } 00739 if ( m_bCompileStatus ) { 00740 glDeleteShader( m_uiShaderObject ); 00741 CHECK_GL_ERROR(); 00742 } 00743 } 00744 /* 00745 //----------------------------------------------------------------------------- 00746 // Load a file 00747 int GLSLShader::Load ( char *fileName ) 00748 { 00749 std::ifstream file; 00750 if ( !file ) return -1; // File name is empty 00751 unsigned long len = GFnGetFileLength( fileName ); 00752 //--------------------------------------------------------------- 00753 file.open( fileName, std::ios::in ); 00754 //file.close(); 00755 //std::cout << "file len = " << len << std::cout; 00756 //char c; 00757 //std::cout << "Press any key to continue." << std::endl; 00758 //std::cin >> c; 00759 if ( len == 0 ) return -2; // File is empty 00760 //--------------------------------------------------------------- 00761 if ( m_cpShaderSource ) { // there is a loaded source, free it 00762 if ( m_bMemAlloc ) delete [] m_cpShaderSource; 00763 } 00764 m_cpShaderSource = new GLchar[len+1]; 00765 //m_cpShaderSource = (GLchar *) malloc (len+1); 00766 if ( m_cpShaderSource == NULL ) return -3; // cannot reserve memory 00767 m_bMemAlloc = true; 00768 //--------------------------------------------------------------- 00769 // len isn't always strlen because some characters are stripped in 00770 // ASCII read. 00771 // It is important to 0-terminate the real length later, 00772 // len is just the max location. 00773 //m_cpShaderSource[len] = 0; // '\0' 00774 m_cpShaderSource[len] = '\0'; 00775 //--------------------------------------------------------------- 00776 unsigned int i = 0; 00777 //while ( !file.eof() && i < len ) { 00778 //file.open( fileName, std::ios::in ); 00779 //while ( i < len ) { 00780 while ( file.good() ) { 00781 m_cpShaderSource[i++] = file.get(); // get a char from file 00782 //std::cout << m_cpShaderSource[i-1]; 00783 if ( i > len ) i = len; 00784 } 00785 m_cpShaderSource[i] = '\0'; // 0-terminate 00786 //std::cout << "\ni = " << i << std::endl; 00787 file.close(); 00788 //---------------------------------------------------------------- 00789 return 0; 00790 } 00791 //*/ 00792 //* 00793 //----------------------------------------------------------------------------- 00794 // Load a file 00795 int GLSLShader::Load ( char * fileName ) 00796 { 00797 if ( !fileName ) return -1; // File name is empty 00798 unsigned long len = GFnGetFileLength( fileName ); 00799 #ifdef TAPs_DEBUG_MODE 00800 #ifdef TAPs_USE_WXWIDGETS 00801 wxLogError( wxT( "len: %d" ), len ); 00802 #else 00803 std::cout << "len: " << len << "\n"; 00804 #endif 00805 #endif 00806 00807 if ( len == 0 ) return -2; // File is empty 00808 //---------------------------------------------------------------- 00809 if ( m_cpShaderSource ) { // there is a loaded source, free it 00810 if ( m_bMemAlloc ) delete [] m_cpShaderSource; 00811 } 00812 m_cpShaderSource = new GLchar[len+1]; 00813 //m_cpShaderSource = (GLchar *) malloc (len+1); 00814 if ( m_cpShaderSource == NULL ) return -3; // cannot reserve memory 00815 m_bMemAlloc = true; 00816 00817 // 00818 // Open the file 00819 // 00820 FILE *fh = fopen(fileName, "r"); 00821 if (!fh) 00822 return -1; 00823 00824 // 00825 // Get the shader from a file. 00826 // 00827 fseek(fh, 0, SEEK_SET); 00828 int count = static_cast<int>( fread(m_cpShaderSource, 1, len, fh) ); 00829 m_cpShaderSource[count] = '\0'; 00830 00831 if (ferror(fh)) 00832 count = 0; 00833 00834 fclose(fh); 00835 //---------------------------------------------------------------- 00836 return 0; 00837 } 00838 //*/ 00839 //----------------------------------------------------------------------------- 00840 // Load a file 00841 void GLSLShader::LoadFromMemory ( const char *program ) 00842 { 00843 if ( m_cpShaderSource == NULL ) { // there is a loaded source, free it 00844 if ( m_bMemAlloc ) delete [] m_cpShaderSource; 00845 } 00846 m_bMemAlloc = false; 00847 m_cpShaderSource = (GLchar *) program; 00848 } 00849 //----------------------------------------------------------------------------- 00850 // Get compiler log messages 00851 char * GLSLShader::GetCompilerLog () 00852 { 00853 if ( !gbIsGLSLSupported ) return gbGLSLErrStr[0]; 00854 if ( m_uiShaderObject == NULL ) return gbGLSLErrStr[1]; 00855 //---------------------------------------------------------------- 00856 int bLen = 0; int sLen = 0; 00857 glGetShaderiv( m_uiShaderObject, GL_INFO_LOG_LENGTH, &bLen ); 00858 CHECK_GL_ERROR(); 00859 //---------------------------------------------------------------- 00860 if ( bLen > 1 ) { 00861 if ( m_cpCompilerLog == NULL ) { 00862 delete [] m_cpCompilerLog; 00863 m_cpCompilerLog = NULL; 00864 } 00865 if ( (m_cpCompilerLog = new GLchar[bLen]) == NULL ) { 00866 //if ( (m_cpCompilerLog = (GLchar *) malloc (bLen*sizeof(char))) == NULL ) { 00867 #ifdef TAPs_USE_WXWIDGETS 00868 wxLogError( wxT( "ERROR: Could not allocate memory for compiler log buffer" ) ); 00869 #else 00870 std::cerr << "ERROR: Could not allocate memory for compiler log buffer\n"; 00871 #endif 00872 return gbGLSLErrStr[3]; 00873 } 00874 glGetShaderInfoLog( m_uiShaderObject, bLen, &sLen, m_cpCompilerLog ); 00875 CHECK_GL_ERROR(); 00876 } 00877 if ( m_cpCompilerLog == NULL ) return static_cast<char *>( m_cpCompilerLog ); 00878 else return gbGLSLErrStr[4]; 00879 //---------------------------------------------------------------- 00880 #ifdef TAPs_DEBUG_MODE 00881 #ifdef TAPs_USE_WXWIDGETS 00882 wxLogError( wxT( "blen: %d" ), bLen ); 00883 wxLogError( wxT( "slen: %d" ), sLen ); 00884 #else 00885 std::cout << "blen: " << bLen << std::endl; 00886 std::cout << "slen: " << sLen << std::endl; 00887 #endif 00888 #endif 00889 00890 return gbGLSLErrStr[5]; 00891 } 00892 //----------------------------------------------------------------------------- 00893 // Get compiler log messages 00894 bool GLSLShader::Compile () 00895 { 00896 if ( !gbIsGLSLSupported ) { 00897 #ifdef TAPs_DEBUG_MODE 00898 #ifdef TAPs_USE_WXWIDGETS 00899 wxLogError( wxT( "gbIsGLSLSupported == false" ) ); 00900 #else 00901 std::cout << "gbIsGLSLSupported == false\n" << std::endl; 00902 #endif 00903 #endif 00904 return false; 00905 } 00906 m_bCompileStatus = false; 00907 int compiled = 0; 00908 if ( m_cpShaderSource == NULL ) { 00909 #ifdef TAPs_DEBUG_MODE 00910 #ifdef TAPs_USE_WXWIDGETS 00911 wxLogError( wxT( "m_cpShaderSource == NULL (i.e. Shader is empty!)" ) ); 00912 #else 00913 std::cout << "m_cpShaderSource == NULL (i.e. Shader is empty!)\n" << std::endl; 00914 #endif 00915 #endif 00916 return false; // Shader is empty 00917 } 00918 //---------------------------------------------------------------- 00919 GLint length = static_cast<GLint>( strlen( (const char*) m_cpShaderSource ) ); 00920 #ifdef TAPs_DEBUG_MODE 00921 #ifdef TAPs_USE_WXWIDGETS 00922 wxLogError( wxT( "Source (Length %d ):" ), length ); 00923 wxLogError( wxT( "-------------------------------------------------------------" ) ); 00924 wxLogError( wxT( "%s" ), m_cpShaderSource ); 00925 wxLogError( wxT( "-------------------------------------------------------------" ) ); 00926 #else 00927 std::cout << "Source (Length " << length << "):\n"; 00928 std::cout << "-------------------------------------------------------------\n"; 00929 std::cout << m_cpShaderSource << "\n"; 00930 std::cout << "-------------------------------------------------------------\n"; 00931 #endif 00932 #endif 00933 glShaderSource( m_uiShaderObject, 1, (const GLchar **) &m_cpShaderSource, &length ); 00934 //glShaderSource( m_uiShaderObject, 1, (const GLchar **) &m_cpShaderSource, NULL ); 00935 #ifdef TAPs_DEBUG_MODE 00936 #ifdef TAPs_USE_WXWIDGETS 00937 wxLogError( wxT( "Pass glShaderSource" ) ); 00938 #else 00939 std::cout << "Pass glShaderSource\n"; 00940 #endif 00941 #endif 00942 CHECK_GL_ERROR(); 00943 00944 glCompileShader( m_uiShaderObject ); 00945 #ifdef TAPs_DEBUG_MODE 00946 #ifdef TAPs_USE_WXWIDGETS 00947 wxLogError( wxT( "Pass glCompileShader" ) ); 00948 #else 00949 std::cout << "Pass glCompileShader\n"; 00950 #endif 00951 #endif 00952 CHECK_GL_ERROR(); 00953 00954 glGetShaderiv( m_uiShaderObject, GL_COMPILE_STATUS, &compiled ); 00955 #ifdef TAPs_DEBUG_MODE 00956 #ifdef TAPs_USE_WXWIDGETS 00957 wxLogError( wxT( "Pass glGetObjectParameteriv" ) ); 00958 #else 00959 std::cout << "Pass glGetObjectParameteriv\n"; 00960 #endif 00961 #endif 00962 CHECK_GL_ERROR(); 00963 //---------------------------------------------------------------- 00964 if ( compiled ) m_bCompileStatus = true; 00965 return m_bCompileStatus; 00966 } 00967 //----------------------------------------------------------------------------- 00968 // Get localtion of the attribute 00969 GLint GLSLShader::GetAttribLoc ( char * cpAttribName ) 00970 { 00971 return glGetAttribLocation( m_uiShaderObject, cpAttribName ); 00972 } 00973 //----------------------------------------------------------------------------- 00974 //============================================================================= 00975 //****************************************************************************** 00976 00977 00978 //****************************************************************************** 00979 //============================================================================= 00980 // class GLSLVertexShader 00981 //----------------------------------------------------------------------------- 00982 // Constructor 00983 GLSLVertexShader::GLSLVertexShader () 00984 { 00985 m_iShaderType = 1; 00986 if ( gbIsGLSLSupported ) { 00987 m_uiShaderObject = glCreateShader( GL_VERTEX_SHADER ); 00988 CHECK_GL_ERROR(); 00989 } 00990 } 00991 //----------------------------------------------------------------------------- 00992 // Destructor 00993 GLSLVertexShader::~GLSLVertexShader () 00994 {} 00995 //----------------------------------------------------------------------------- 00996 //============================================================================= 00997 //****************************************************************************** 00998 00999 01000 //****************************************************************************** 01001 //============================================================================= 01002 // class GLSLFragmentShader 01003 //----------------------------------------------------------------------------- 01004 // Constructor 01005 GLSLFragmentShader::GLSLFragmentShader () 01006 { 01007 m_iShaderType = 2; 01008 if ( gbIsGLSLSupported ) { 01009 m_uiShaderObject = glCreateShader( GL_FRAGMENT_SHADER ); 01010 CHECK_GL_ERROR(); 01011 } 01012 } 01013 //----------------------------------------------------------------------------- 01014 // Destructor 01015 GLSLFragmentShader::~GLSLFragmentShader () 01016 {} 01017 //----------------------------------------------------------------------------- 01018 //============================================================================= 01019 //****************************************************************************** 01020 01021 01022 //****************************************************************************** 01023 //============================================================================= 01024 // class GLSLShaderManager 01025 //----------------------------------------------------------------------------- 01026 // Constructor 01027 GLSLShaderManager::GLSLShaderManager () 01028 {} 01029 //----------------------------------------------------------------------------- 01030 // Destructor 01031 GLSLShaderManager::~GLSLShaderManager () 01032 { 01033 // Delete objects 01034 std::vector<GLSLProgramObject *>::iterator pos = m_vProgramObjectList.begin(); 01035 while ( pos != m_vProgramObjectList.end() ) { 01036 GLSLProgramObject *obj = *pos; 01037 pos = m_vProgramObjectList.erase( pos ); 01038 delete obj; 01039 } 01040 } 01041 //----------------------------------------------------------------------------- 01042 // Load vertex/fragment shader from file 01043 GLSLProgramObject * GLSLShaderManager::LoadFromFile ( char * vertFile, char * fragFile ) 01044 { 01045 GLSLProgramObject * obj = new GLSLProgramObject(); 01046 GLSLVertexShader * vertShader = new GLSLVertexShader(); 01047 GLSLFragmentShader * fragShader = new GLSLFragmentShader(); 01048 //---------------------------------------------------------------- 01049 // Load vertex program 01050 if ( vertFile ) { 01051 #ifdef TAPs_DEBUG_MODE 01052 #ifdef TAPs_USE_WXWIDGETS 01053 wxLogError( wxT( "Load vertex program \"%s\"" ), vertFile ); 01054 #else 01055 std::cout << "Load vertex program \"" << vertFile << "\""; 01056 #endif 01057 #endif 01058 if ( vertShader->Load( vertFile ) ) { 01059 #ifdef TAPs_USE_WXWIDGETS 01060 wxLogError( wxT( "ERROR: Could not load a vertex shader named \"%s\"" ), 01061 vertFile ); 01062 #else 01063 std::cerr << "ERROR: Could not load a vertex shader named \"" 01064 << vertFile << "\"!\n"; 01065 #endif 01066 delete obj; 01067 delete vertShader; 01068 delete fragShader; 01069 return NULL; 01070 } 01071 #ifdef TAPs_DEBUG_MODE 01072 #ifdef TAPs_USE_WXWIDGETS 01073 wxLogError( wxT( " is successful!" ) ); 01074 #else 01075 std::cout << " is successful!\n"; 01076 #endif 01077 #endif 01078 } 01079 //---------------------------------------------------------------- 01080 // Load fragment program 01081 if ( fragFile ) { 01082 #ifdef TAPs_DEBUG_MODE 01083 #ifdef TAPs_USE_WXWIDGETS 01084 wxLogError( wxT( "Load fragment program \"%s\"" ), fragFile ); 01085 #else 01086 std::cout << "Load fragment program \"" << fragFile << "\""; 01087 #endif 01088 #endif 01089 if ( fragShader->Load( fragFile ) ) { 01090 #ifdef TAPs_USE_WXWIDGETS 01091 wxLogError( wxT( "ERROR: Could not load a fragment shader named \"%s\"" ), 01092 fragFile ); 01093 #else 01094 std::cerr << "ERROR: Could not load a fragment shader named \"" 01095 << fragFile << "\"!\n"; 01096 #endif 01097 delete obj; 01098 delete vertShader; 01099 delete fragShader; 01100 return NULL; 01101 } 01102 #ifdef TAPs_DEBUG_MODE 01103 #ifdef TAPs_USE_WXWIDGETS 01104 wxLogError( wxT( " is successful!" ) ); 01105 #else 01106 std::cout << " is successful!\n"; 01107 #endif 01108 #endif 01109 } 01110 //---------------------------------------------------------------- 01111 // Compile vertex program 01112 if ( vertFile ) { 01113 #ifdef TAPs_DEBUG_MODE 01114 #ifdef TAPs_USE_WXWIDGETS 01115 wxLogError( wxT( "Compile vertex program \"%s\"" ), vertFile ); 01116 #else 01117 std::cout << "Compile vertex program \"" << vertFile << "\""; 01118 #endif 01119 #endif 01120 if ( !vertShader->Compile() ) { 01121 #ifdef TAPs_USE_WXWIDGETS 01122 wxLogError( wxT( "ERROR: \"%s\" Compilation Error:" ), vertFile ); 01123 wxLogError( wxT( "%s" ), vertShader->GetCompilerLog() ); 01124 #else 01125 std::cerr << "ERROR: \"" << vertFile << "\" Compilation Error:\n"; 01126 std::cerr << vertShader->GetCompilerLog() << "\n"; 01127 #endif 01128 delete obj; 01129 delete vertShader; 01130 delete fragShader; 01131 return NULL; 01132 } 01133 #ifdef TAPs_DEBUG_MODE 01134 #ifdef TAPs_USE_WXWIDGETS 01135 wxLogError( wxT( " is successful!" ) ); 01136 #else 01137 std::cout << " is successful!\n"; 01138 #endif 01139 #endif 01140 } 01141 //---------------------------------------------------------------- 01142 // Compile fragment program 01143 if ( fragFile ) { 01144 #ifdef TAPs_DEBUG_MODE 01145 #ifdef TAPs_USE_WXWIDGETS 01146 wxLogError( wxT( "Compile fragment program \"%s\"" ), fragFile ); 01147 #else 01148 std::cout << "Compile fragment program \"" << fragFile << "\""; 01149 #endif 01150 #endif 01151 if ( !fragShader->Compile() ) { 01152 #ifdef TAPs_USE_WXWIDGETS 01153 wxLogError( wxT( "ERROR: \"%s\" Compilation Error:" ), fragFile ); 01154 wxLogError( wxT( "%s" ), fragShader->GetCompilerLog() ); 01155 #else 01156 std::cerr << "ERROR: \"" << fragFile << "\" Compilation Error:\n"; 01157 std::cerr << fragShader->GetCompilerLog() << "\n"; 01158 #endif 01159 delete obj; 01160 delete vertShader; 01161 delete fragShader; 01162 return NULL; 01163 } 01164 #ifdef TAPs_DEBUG_MODE 01165 #ifdef TAPs_USE_WXWIDGETS 01166 wxLogError( wxT( " is successful!" ) ); 01167 #else 01168 std::cout << " is successful!\n"; 01169 #endif 01170 #endif 01171 } 01172 //---------------------------------------------------------------- 01173 // Add to the object 01174 if ( vertFile ) obj->AddShader( vertShader ); 01175 if ( fragFile ) obj->AddShader( fragShader ); 01176 //---------------------------------------------------------------- 01177 // Link 01178 if ( !obj->Link() ) { 01179 #ifdef TAPs_USE_WXWIDGETS 01180 wxLogError( wxT( "ERROR: Cound not link a shader object created from \"%s\" and \"%s\"" ), 01181 vertFile, fragFile ); 01182 wxLogError( wxT( "%s" ), obj->GetLinkerLog() ); 01183 #else 01184 std::cerr << "ERROR: Cound not link a shader object created from \"" 01185 << vertFile << "\" and \"" << fragFile << "\"\n"; 01186 std::cerr << obj->GetLinkerLog() << "\n"; 01187 #endif 01188 delete obj; 01189 delete vertShader; 01190 delete fragShader; 01191 return NULL; 01192 } 01193 //---------------------------------------------------------------- 01194 m_vProgramObjectList.push_back( obj ); 01195 obj->MemageMemory(); 01196 //---------------------------------------------------------------- 01197 return obj; 01198 } 01199 //----------------------------------------------------------------------------- 01200 // Load vertex/fragment shader from memory 01201 GLSLProgramObject * GLSLShaderManager::LoadFromMemory ( const char *vertMem, const char *fragMem ) 01202 { 01203 GLSLProgramObject *obj = new GLSLProgramObject(); 01204 GLSLVertexShader *vertShader = new GLSLVertexShader; 01205 GLSLFragmentShader *fragShader = new GLSLFragmentShader; 01206 //---------------------------------------------------------------- 01207 // Load vertex program 01208 if ( vertMem ) { 01209 vertShader->LoadFromMemory( vertMem ); 01210 } 01211 //---------------------------------------------------------------- 01212 // Load fragment program 01213 if ( fragMem ) { 01214 fragShader->LoadFromMemory( fragMem ); 01215 } 01216 //---------------------------------------------------------------- 01217 // Compile vertex program 01218 if ( vertMem ) { 01219 if ( !vertShader->Compile() ) { 01220 #ifdef TAPs_USE_WXWIDGETS 01221 wxLogError( wxT( "ERROR: \"%s\" Compilation Error:" ), 01222 vertMem ); 01223 wxLogError( wxT( "%s" ), vertShader->GetCompilerLog() ); 01224 #else 01225 std::cerr << "ERROR: \"" << vertMem << "\" Compilation Error:\n"; 01226 std::cerr << vertShader->GetCompilerLog() << "\n"; 01227 #endif 01228 delete obj; 01229 delete vertShader; 01230 delete fragShader; 01231 return NULL; 01232 } 01233 } 01234 //---------------------------------------------------------------- 01235 // Compile fragment program 01236 if ( fragMem ) { 01237 if ( !fragShader->Compile() ) { 01238 #ifdef TAPs_USE_WXWIDGETS 01239 wxLogError( wxT( "ERROR: \"%s\" Compilation Error:" ), 01240 fragMem ); 01241 wxLogError( wxT( "%s" ), fragShader->GetCompilerLog() ); 01242 #else 01243 std::cerr << "ERROR: \"" << fragMem << "\" Compilation Error:\n"; 01244 std::cerr << fragShader->GetCompilerLog() << "\n"; 01245 #endif 01246 delete obj; 01247 delete vertShader; 01248 delete fragShader; 01249 return NULL; 01250 } 01251 } 01252 //---------------------------------------------------------------- 01253 // Add to the object 01254 if ( vertMem ) obj->AddShader( vertShader ); 01255 if ( fragMem ) obj->AddShader( fragShader ); 01256 //---------------------------------------------------------------- 01257 // Link 01258 if ( !obj->Link() ) { 01259 #ifdef TAPs_USE_WXWIDGETS 01260 wxLogError( wxT( "ERROR: Cound not link a shader object created from \"%s\" and \"%s\"" ), 01261 vertMem, fragMem ); 01262 wxLogError( wxT( "%s" ), obj->GetLinkerLog() ); 01263 #else 01264 std::cerr << "ERROR: Cound not link a shader object created from \"" 01265 << vertMem << "\"\n and \"\n" << fragMem << "\"\n"; 01266 std::cerr << obj->GetLinkerLog() << "\n"; 01267 #endif 01268 delete obj; 01269 delete vertShader; 01270 delete fragShader; 01271 return NULL; 01272 } 01273 //---------------------------------------------------------------- 01274 m_vProgramObjectList.push_back( obj ); 01275 obj->MemageMemory(); 01276 //---------------------------------------------------------------- 01277 return obj; 01278 } 01279 //----------------------------------------------------------------------------- 01280 // Delete all shader objects 01281 bool GLSLShaderManager::Delete ( GLSLProgramObject *obj ) 01282 { 01283 std::vector<GLSLProgramObject *>::iterator pos; 01284 for ( pos = m_vProgramObjectList.begin(); pos != m_vProgramObjectList.end(); ++pos ) { 01285 if ( *pos == obj ) { 01286 m_vProgramObjectList.erase( pos ); 01287 delete obj; 01288 return true; 01289 } 01290 } 01291 return false; 01292 } 01293 //----------------------------------------------------------------------------- 01294 //============================================================================= 01295 //****************************************************************************** 01296 01297 01298 //============================================================================= 01299 END_NAMESPACE_TAPs__OpenGL 01300 //345678901234567890123456789012345678901234567890123456789012345678901234567890 01301 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8