FlowVis 1.0

FlowVis/shader.h

00001 #ifndef _SHADER_HPP_
00002 #define _SHADER_HPP_
00003 
00004 #include <GLEW/GLEW.h>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <string>
00008 
00009 using std::string;
00010 using std::cerr;
00011 using std::cout;
00012 using std::endl;
00013 
00014 // This is a very simple shader class
00015 
00016 class Shader
00017 {
00018 public:
00019 
00033         Shader(const string &path);
00034 
00056         Shader(const string &path, bool geometryShader, GLenum inputType, 
00057                 GLenum outputType, GLint maxOutVertices);
00058 
00059         ~Shader();
00060 
00061         // Bind the shader to the OGL state-machine
00062         void bind() const
00063         {
00064                 glUseProgram(_program);
00065         }
00066 
00067         // Unbind the shader
00068         void unbind() const
00069         {
00070                 glUseProgram(0);
00071         }
00072 
00073         // Query the location of a vertex attribute inside the shader.
00074         GLint get_attrib_location(const std::string &name) const
00075         {
00076                 return glGetAttribLocation(_program, name.c_str());
00077         }
00078 
00079         // Query the location of a uniform variable inside the shader.
00080         GLint get_uniform_location(const std::string &name) const
00081         {
00082                 return glGetUniformLocation(_program, name.c_str());
00083         }
00084 
00085         // Query OpenGL errors and print error messages to STDERR.
00086         static void get_errors(void);
00087 
00088         // Define the name of the variable inside the shader which represents the final color for each fragment.
00089         //void bind_frag_data_location(const std::string &name)
00090         //{
00091         //      if(_program > 0)
00092         //      {
00093         //              glBindFragDataLocation(_program, 0, name.c_str() );
00094         //              link();
00095         //      }
00096         //}
00097 
00098         // A little cast helper.
00099         // With this you can simply do "if (shader) {...}" to test if a
00100         // shader has been compiled successfully.
00101         operator bool ()
00102         {
00103                 return _success;
00104         }
00105 
00106 private:
00107 
00108         bool _success;
00109 
00110         GLuint _vertex_shader;
00111         GLuint _geometry_shader;
00112         GLuint _fragment_shader;
00113         GLuint _program;
00114 
00115         bool init(const string &path, bool geometryShader, GLenum inputType, 
00116                 GLenum outputType, GLint maxOutVertices);
00117 
00118         GLuint compile(GLenum type, const string &source);
00119         void link (bool geometryShader, GLenum inputType, 
00120                 GLenum outputType, GLint maxOutVertices);
00121 
00122         void shader_log(GLuint shader);
00123         void program_log(GLuint program);
00124 
00125         bool file_exists(const string &filename);
00126 
00127         string read_file(const string &filename);
00128 };
00129 
00130 #endif //#ifndef _SHADER_HPP_
00131 
 All Classes Functions Variables Friends