glslShaders.cpp

00001 
00002 /* Copyright (c) 2007,       Maxim Makhinya
00003    Copyright (c) 2008,       Stefan Eilemann <eile@equalizergraphics.com> 
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License version 2.1 as published
00007  * by the Free Software Foundation.
00008  *  
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00012  * details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this library; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #include "glslShaders.h"
00020 
00021 namespace eVolve
00022 {
00023 void GLSLShaders::_printLog( GLhandleARB shader, const std::string &type )
00024 {
00025     GLint length;
00026     glGetObjectParameterivARB( shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
00027 
00028     if( length <= 1 )
00029         return;
00030 
00031     std::vector<GLcharARB> log;
00032     log.resize( length );
00033 
00034     GLint written;
00035     glGetInfoLogARB( shader, length, &written, &log[0] );
00036     EQERROR << "Shader error: " << type << std::endl
00037             << &log[0] << std::endl;
00038 
00039     return;
00040 }
00041 
00042 
00043 GLhandleARB GLSLShaders::_loadShader( const std::string &shader,
00044                                                  GLenum shaderType )
00045 {
00046     GLhandleARB handle = glCreateShaderObjectARB( shaderType );
00047     const char* cstr   = shader.c_str();
00048     glShaderSourceARB(  handle, 1, &cstr, 0 );
00049     glCompileShaderARB( handle );
00050 
00051     GLint status;
00052     glGetObjectParameterivARB( handle, GL_OBJECT_COMPILE_STATUS_ARB, &status );
00053     if( status == GL_FALSE )
00054     {
00055         glDeleteObjectARB( handle );
00056         _printLog( handle, "Compiling" );
00057         return 0;
00058     }
00059 
00060     return handle;
00061 }
00062 
00063 
00064 bool GLSLShaders::loadShaders( const std::string &vShader,
00065                                const std::string &fShader,
00066                                      GLEWContext* glewCtx )
00067 {
00068     if( _shadersLoaded )
00069         return true;
00070 
00071     EQASSERT( glewCtx );
00072     _glewCtx = glewCtx;
00073 
00074     _program = glCreateProgramObjectARB();
00075 
00076     GLhandleARB vertexShader = _loadShader( vShader, GL_VERTEX_SHADER_ARB );
00077     glAttachObjectARB( _program, vertexShader );
00078 
00079     GLhandleARB fragmentShader = _loadShader( fShader, GL_FRAGMENT_SHADER_ARB );
00080     glAttachObjectARB( _program, fragmentShader );
00081 
00082     glLinkProgramARB( _program );
00083 
00084     GLint status;
00085     glGetObjectParameterivARB( _program, GL_OBJECT_LINK_STATUS_ARB, &status );
00086     if( status == GL_FALSE )
00087     {
00088         _printLog( _program, "Linking" );
00089         
00090         if( fragmentShader )
00091             glDeleteObjectARB( fragmentShader );
00092         if( vertexShader )
00093             glDeleteObjectARB( vertexShader );
00094 
00095         glDeleteObjectARB( _program );
00096         _program = 0;
00097         return false;
00098     }
00099 
00100     _shadersLoaded = true;
00101     return true;
00102 }
00103 
00104 void GLSLShaders::unloadShaders()
00105 {
00106     if( !_shadersLoaded )
00107         return;
00108 
00109     EQASSERT( _glewCtx );
00110     EQASSERT( _program );
00111 
00112     glDeleteObjectARB( _program );
00113     _shadersLoaded = false;
00114     _program       = 0;
00115 }
00116 
00117 }
Generated on Mon Aug 10 18:58:39 2009 for Equalizer 0.9 by  doxygen 1.5.8