GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/GLGraphics/glsl_shader.h
00001 #ifndef __GLGRAPHICS_GLSL_SHADER_H__
00002 #define __GLGRAPHICS_GLSL_SHADER_H__
00003 
00004 #include <GL/glew.h>
00005 #include <string>
00006 
00007 /* It is a little tricky to make shader programs in C++ using GLSL but the problems are
00008         almost all silly little things. For instance, how do you robustly read from a text 
00009         file? How do you compile a shader and check for errors? In OpenGL What is the difference 
00010         between a GLSL program and a shader anyway?
00011 
00012         The short answer to the last question is this: A "shader" can be either a vertex shader, 
00013         a geometry shader (in OpenGL 2.0) or a fragment shader. A "program" is a linked combination
00014         of these three types of shaders. Note that you don't have to have a geometry shader.
00015         
00016         This API attempts to obviate the need for an answer to the first two questions by providing 
00017         an API for loading and compiling shaders and checking for errors. However, I don't
00018         include functions for creating the program, attaching shaders and linking. Why not??!
00019         
00020         Because the need for flexibility means that the API would be just as complex as just using
00021         the OpenGL functions directly! However, loading shaders and checking for errors in compiled shaders
00022         is different. It makes sense to wrap that. It also makes sense to wrap the error checking for 
00023         programs that are linked, so there is a function for that too.
00024         
00025         Since shader loading and error check sandwhich the two calls needed for compilation, the most 
00026         important function in this API, create_glsl_shader, loads a shader, compiles it, checks for errors 
00027         and returns the shader handle. There is also a version which creates a shader from a string.
00028         
00029         There is some code below to illustrate usage.
00030         
00031 <code snippet>
00032         // Create shaders directly from file
00033         GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
00034         GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, shader_path, "tri.geom");
00035         GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
00036 
00037         // Create the program
00038         prog_P0 = glCreateProgram();
00039         
00040         // Attach all shaders
00041         if(vs) glAttachShader(prog_P0, vs);
00042         if(gs) glAttachShader(prog_P0, gs);
00043         if(fs) glAttachShader(prog_P0, fs);
00044         
00045         // Specify input and output for the geometry shader. Note that this must be
00046         // done before linking the program.
00047         glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
00048         glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
00049         glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
00050 
00051         // Link the program object and print out the info log
00052         glLinkProgram(prog_P0);
00053         print_glsl_program_log(prog_P0);
00054         
00055         // Install program object as part of current state
00056         glUseProgram(prog_P0);
00057 
00058         // Set the value of a uniform
00059         glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
00060 </code snippet>
00061         
00062         Happy shader coding.
00063         
00064         Andreas Brentzen, 2007
00065         
00066         */
00067 
00068 namespace GLGraphics
00069 {
00071         void print_glsl_program_log(GLuint obj);
00072         
00075         const std::string read_glsl_source(const std::string& path, const std::string& file);
00076         
00080         GLuint create_glsl_shader(GLuint stype, const std::string& src);
00081                 
00086         GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file);
00087 }
00088 
00089 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations