30 #include "glslShaders.h"
34 void GLSLShaders::_printLog( GLhandleARB shader,
const std::string &type )
37 glGetObjectParameterivARB( shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
42 std::vector<GLcharARB> log;
46 glGetInfoLogARB( shader, length, &written, &log[0] );
47 LBERROR <<
"Shader error: " << type << std::endl
48 << &log[0] << std::endl;
54 GLhandleARB GLSLShaders::_loadShader(
const std::string &shader,
57 GLhandleARB handle = glCreateShaderObjectARB( shaderType );
58 const char* cstr = shader.c_str();
59 glShaderSourceARB( handle, 1, &cstr, 0 );
60 glCompileShaderARB( handle );
63 glGetObjectParameterivARB( handle, GL_OBJECT_COMPILE_STATUS_ARB, &status );
64 if( status != GL_FALSE )
67 _printLog( handle,
"Compiling" );
68 glDeleteObjectARB( handle );
73 bool GLSLShaders::_cleanupOnError( GLhandleARB vShader, GLhandleARB fShader )
76 glDeleteObjectARB( vShader );
79 glDeleteObjectARB( fShader );
81 glDeleteObjectARB( _program );
87 bool GLSLShaders::loadShaders(
const std::string &vShader,
88 const std::string &fShader,
89 const GLEWContext* glewContext )
94 LBASSERT( glewContext );
95 _glewContext = glewContext;
97 _program = glCreateProgramObjectARB();
99 GLhandleARB vertexShader = _loadShader( vShader, GL_VERTEX_SHADER_ARB );
101 return _cleanupOnError();
103 glAttachObjectARB( _program, vertexShader );
105 GLhandleARB fragmentShader = _loadShader( fShader, GL_FRAGMENT_SHADER_ARB );
106 if( !fragmentShader )
107 return _cleanupOnError( vertexShader );
109 glAttachObjectARB( _program, fragmentShader );
111 glLinkProgramARB( _program );
114 glGetObjectParameterivARB( _program, GL_OBJECT_LINK_STATUS_ARB, &status );
115 if( status != GL_FALSE )
117 _shadersLoaded =
true;
121 _printLog( _program,
"Linking" );
122 return _cleanupOnError( vertexShader, fragmentShader );
126 void GLSLShaders::unloadShaders()
128 if( !_shadersLoaded )
131 LBASSERT( _glewContext );
132 LBASSERT( _program );
134 glDeleteObjectARB( _program );
135 _shadersLoaded =
false;