SWE
/import/home/rettenbs/src/SWE/src/opengl/shader.h
00001 #ifndef SHADER_H
00002 #define SHADER_H
00003 // =====================================================================
00004 // This file is part of SWE_CUDA (see file SWE_Block.cu for details).
00005 // 
00006 // Copyright (C) 2010,2011 Tobias Schnabel
00007 // Copyright (C) 2012      Sebastian Rettenberger
00008 // 
00009 // SWE_CUDA is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // SWE_CUDA is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with SWE_CUDA.  If not, see <http://www.gnu.org/licenses/>.
00021 // =====================================================================
00022 #include <SDL/SDL.h>
00023 #include <SDL/SDL_opengl.h>
00024 #include <iostream>
00025 #include <fstream>
00026 
00027 class Shader {
00028 public:
00029         // Constructor and Destructor
00030         // Load vertex shader file and fragment shader file 
00031         Shader(char const * vertexShaderFile,  char const * fragmentShaderFile) ;
00032         ~Shader();
00033 
00034         // Check if shaders are loaded
00035         bool shadersLoaded();
00036 
00037         // Shader control
00038         void enableShader();
00039         void disableShader();
00040 
00044         GLint getUniformLocation(const char* name)
00045         {
00046                 if (!shdrLoaded)
00047                         return -1;
00048                 return glGetUniformLocation(program, name);
00049         }
00050 
00054         void setUniform(GLint location, GLfloat value)
00055         {
00056                 if (location < 0)
00057                         return;
00058                 glUniform1f(location, value);
00059         }
00060 
00061 private:        
00062         // State flags
00063         bool shdrLoaded;
00064 
00065         // Helper functions
00066         bool readShaderFile(char const * filename, GLchar * & shaderSource, GLint & length);
00067         bool isExtensionSupported(const char* szTargetExtension );
00068         bool isShaderCompiled(GLuint shader, char const * prefix);
00069         bool isProgramLinked(GLuint program, char const * prefix);
00070 
00071         // Vertex-Shader
00072         GLuint   vertexShader;
00073         GLchar * vertexShaderSource;
00074         GLint    vertexShaderLength;
00075         
00076         // Fragment-Shader
00077         GLuint   fragmentShader;
00078         GLchar * fragmentShaderSource;
00079         GLint    fragmentShaderLength;
00080 
00081         // Shaders id
00082         GLuint program;
00083 
00085         static bool shdrSupport;
00086 
00087         // Shader extension function pointers
00088         static PFNGLCREATESHADERPROC glCreateShader;
00089         static PFNGLCREATEPROGRAMPROC glCreateProgram;
00090         static PFNGLATTACHSHADERPROC glAttachShader;
00091         static PFNGLCOMPILESHADERPROC glCompileShader;
00092         static PFNGLUSEPROGRAMPROC glUseProgram;
00093         static PFNGLDETACHSHADERPROC glDetachShader;
00094         static PFNGLDELETESHADERPROC glDeleteShader;
00095         static PFNGLLINKPROGRAMPROC glLinkProgram;
00096         static PFNGLSHADERSOURCEPROC glShaderSource;
00097         static PFNGLDELETEPROGRAMPROC glDeleteProgram;
00098         static PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
00099         static PFNGLUNIFORM1FPROC glUniform1f;
00100         
00101         // Shader objects extension pointers
00102         static PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
00103         static PFNGLGETSHADERIVPROC glGetShaderiv;
00104         static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
00105         static PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
00106 };
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends