How it works...

The state stored within a GLSLProgram object includes the handle to the OpenGL shader program object (handle), a bool variable indicating whether or not the program has been successfully linked (linked), and map, which is used to store uniform locations as they are discovered (uniformLocations).

The compileShader overloads will throw GLSLProgramException if the compilation fails. The first version determines the type of shader based on the filename extension. In the second version, the caller provides the shader type, and the third version is used to compile a shader, taking the shader's source code from a string. The filename can be provided as a third argument if the string was taken from a file, which is helpful for providing better error messages.

The GLSLProgramException error message will contain the contents of the shader log or program log when an error occurs.

The getUniformLocation private function is used by the setUniform functions to find the location of a uniform variable. It checks the uniformLocations map first, and if the location is not found, queries OpenGL for the location, and stores the result in the map before returning. The fileExists function is used by compileShaderFromFile to check for file existence.

The constructor simply initializes linked to false and handle to 0. The handle variable will be initialized by calling glCreateProgram when the first shader is compiled.

The link function simply attempts to link the program by calling glLinkProgram. It then checks the link status, and if successful, sets the linked variable to true and returns true. Otherwise, it gets the program log (by calling glGetProgramInfoLog), stores the result in GLSLProgramExceptionand throws it. If the link is successful, it calls findUniformLocations, which gets a list of all active uniform variables and stores their locations in the map named uniformLocationskeyed by their names. Regardless of whether the link is successful, it detaches and deletes all shader objects before returning or throwing an exception.  After all of this, it detaches and deletes the shader objects, because they are no longer needed.  

The use function simply calls glUseProgram if the program has already been successfully linked, otherwise it does nothing.

The getHandle and isLinked functions are simply getter functions that return handle to the OpenGL program object and the value of the linked variable.

The bindAttribLocation and bindFragDataLocation functions are wrappers around glBindAttribLocation and glBindFragDataLocation. Note that these functions should only be called prior to linking the program.

The setUniform overloaded functions are straightforward wrappers around the appropriate glUniform functions. As mentioned previously, the uniform locations are queried and stored when the program is linked, so each setUniform function checks the map to get the cached uniform location.

The destructor takes care of deleting the program object.

Finally, the printActiveUniforms, printActiveUniformBlocks, and printActiveAttribs functions are useful for debugging purposes. They simply display a list of the active uniforms/attributes to the standard output.

The following is a simple example of the use of the GLSLProgram class:

GLSLProgram prog; 
 
try { 
  prog.compileShader("myshader.vert.glsl"); 
  prog.compileShader("myshader.frag.glsl"); 
  prog.link(); 
  prog.validate(); 
  prog.use(); 
} catch( GLSLProgramException &e ) { 
  cerr << e.what() << endl; 
  exit(EXIT_FAILURE); 
} 
 
prog.setUniform("ModelViewMatrix", matrix); 
prog.setUniform("LightPosition", 1.0f, 1.0f, 1.0f); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.97.64