Equalizer 1.0

glslShaders.cpp

00001 
00002 /* Copyright (c) 2007,       Maxim Makhinya
00003    Copyright (c) 2008,       Stefan Eilemann <eile@equalizergraphics.com> 
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * - Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  * - Redistributions in binary form must reproduce the above copyright notice,
00011  *   this list of conditions and the following disclaimer in the documentation
00012  *   and/or other materials provided with the distribution.
00013  * - Neither the name of Eyescale Software GmbH nor the names of its
00014  *   contributors may be used to endorse or promote products derived from this
00015  *   software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "glslShaders.h"
00031 
00032 namespace eVolve
00033 {
00034 void GLSLShaders::_printLog( GLhandleARB shader, const std::string &type )
00035 {
00036     GLint length;
00037     glGetObjectParameterivARB( shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
00038 
00039     if( length <= 1 )
00040         return;
00041 
00042     std::vector<GLcharARB> log;
00043     log.resize( length );
00044 
00045     GLint written;
00046     glGetInfoLogARB( shader, length, &written, &log[0] );
00047     EQERROR << "Shader error: " << type << std::endl
00048             << &log[0] << std::endl;
00049 
00050     return;
00051 }
00052 
00053 
00054 GLhandleARB GLSLShaders::_loadShader( const std::string &shader,
00055                                                  GLenum shaderType )
00056 {
00057     GLhandleARB handle = glCreateShaderObjectARB( shaderType );
00058     const char* cstr   = shader.c_str();
00059     glShaderSourceARB(  handle, 1, &cstr, 0 );
00060     glCompileShaderARB( handle );
00061 
00062     GLint status;
00063     glGetObjectParameterivARB( handle, GL_OBJECT_COMPILE_STATUS_ARB, &status );
00064     if( status == GL_FALSE )
00065     {
00066         glDeleteObjectARB( handle );
00067         _printLog( handle, "Compiling" );
00068         return 0;
00069     }
00070 
00071     return handle;
00072 }
00073 
00074 
00075 bool GLSLShaders::loadShaders( const std::string &vShader,
00076                                const std::string &fShader,
00077                                const GLEWContext* glewContext )
00078 {
00079     if( _shadersLoaded )
00080         return true;
00081 
00082     EQASSERT( glewContext );
00083     _glewContext = glewContext;
00084 
00085     _program = glCreateProgramObjectARB();
00086 
00087     GLhandleARB vertexShader = _loadShader( vShader, GL_VERTEX_SHADER_ARB );
00088     glAttachObjectARB( _program, vertexShader );
00089 
00090     GLhandleARB fragmentShader = _loadShader( fShader, GL_FRAGMENT_SHADER_ARB );
00091     glAttachObjectARB( _program, fragmentShader );
00092 
00093     glLinkProgramARB( _program );
00094 
00095     GLint status;
00096     glGetObjectParameterivARB( _program, GL_OBJECT_LINK_STATUS_ARB, &status );
00097     if( status == GL_FALSE )
00098     {
00099         _printLog( _program, "Linking" );
00100         
00101         if( fragmentShader )
00102             glDeleteObjectARB( fragmentShader );
00103         if( vertexShader )
00104             glDeleteObjectARB( vertexShader );
00105 
00106         glDeleteObjectARB( _program );
00107         _program = 0;
00108         return false;
00109     }
00110 
00111     _shadersLoaded = true;
00112     return true;
00113 }
00114 
00115 void GLSLShaders::unloadShaders()
00116 {
00117     if( !_shadersLoaded )
00118         return;
00119 
00120     EQASSERT( _glewContext );
00121     EQASSERT( _program );
00122 
00123     glDeleteObjectARB( _program );
00124     _shadersLoaded = false;
00125     _program       = 0;
00126 }
00127 
00128 }
Generated on Sun May 8 2011 19:11:06 for Equalizer 1.0 by  doxygen 1.7.3