#ifndef RTS_GLSHADERPROGRAM_H #define RTS_GLSHADERPROGRAM_H /********************************************************* //create a shader program rts_glShaderProgram myProgram; //initialize myProgram.Init(); //Attach shaders myProgram.AttachShader(GL_FRAGMENT_SHADER, "filename.glsl"); //Compile and link myProgram.Compile(); myProgram.Link(); myProgram.PrintLog(); //attach uniform variables myProgram.AttachTextureMap("texture", texture); myProgram.AttachGlobalUniform("light_intensity", &intensity); //use the program myProgram.BeginProgram(); //render myProgram.EndProgram(); **********************************************************/ #include "rts_glShaderObject.h" #include "rts_glShaderUniform.h" #include "rts_glTextureMap.h" #include using namespace std; class rts_glShaderProgram { private: void get_uniforms() { GLint num_uniforms; glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &num_uniforms); //get the number of uniform variables GLint max_name_length; glGetProgramiv(id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_name_length); //get the maximum uniform name length GLchar* name_buffer = new GLchar[max_name_length]; //create a buffer to store the name GLsizei length; //I'm not using these yet GLint size; GLenum type; //variable's data type GLint location; //GPU location of the variable for(int i=0; i shader_list; //list of opengl shaders vector uniform_list; //list of active uniform variables vector texture_list; //list of texture maps rts_glShaderProgram() { linked = false; id = 0; } void AttachShader(rts_glShaderObject shader) { if(id == 0) { Init(); } if(shader.id == 0) //if the shader is invalid { log = "Shader is invalid"; return; } //attach the shader to the program glAttachShader(id, shader.id); //attach the shader to the program in OpenGL CHECK_OPENGL_ERROR shader_list.push_back(shader); //push the shader onto our list for later access } //type = GL_FRAGMENT_SHADER or GL_VERTEX_SHADER void AttachShader(GLenum type, const char* filename) { rts_glShaderObject shader(type, filename); AttachShader(shader); } void AttachShader(GLenum type, rtsSourceCode source) { rts_glShaderObject shader(type, source); AttachShader(shader); } void PrintLog() { cout<::iterator iter; for(iter = shader_list.begin(); iter != shader_list.end(); iter++) { (*iter).Compile(); //(*iter).PrintLog(); } } void Link() { glLinkProgram(id); //link the current shader program GLint link_status; //test to see if the link went alright glGetProgramiv(id, GL_LINK_STATUS, &link_status); if(link_status != GL_TRUE) { linked = false; } else linked = true; GLsizei length; GLchar buffer[1000]; glGetProgramInfoLog(id, 1000, &length, buffer); log = buffer; get_uniforms(); //create the list of active uniform variables } void BeginProgram() { CHECK_OPENGL_ERROR if(id == 0) //if the program is invalid, return { log = "Invalid program, cannot use."; return; } if(!linked) { cout<<"Shader Program used without being linked."< 0) glActiveTexture(GL_TEXTURE0); CHECK_OPENGL_ERROR //return to OpenGL default shading glUseProgram(0); CHECK_OPENGL_ERROR } void PrintUniforms() { cout<<"Shader Uniforms: "<