If you are using C++, it can be very convenient to create classes to encapsulate some of the OpenGL objects. A prime example is the shader program object. In this recipe, we'll look at a design for a C++ class that can be used to manage a shader program.
Getting ready
There's not much to prepare for with this one, you just need a build environment that supports C++. Also, I'll assume that you are using GLM for matrix and vector support, if not just leave out the functions involving the GLM classes.
How to do it...
First, we'll use a custom exception class for errors that might occur during compilation or linking:
class GLSLProgramException : public std::runtime_error {
public:
GLSLProgramException( const string & msg ) : std::runtime_error(msg) { }
};
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The techniques involved in the implementation of these functions are covered in previous recipes in this chapter. Due to space limitations, I won't include the code here (it's available from this book's GitHub repository), but we'll discuss some of the design decisions in the next section.
How it works...
The state stored within a GLSLProgram object includes the handle to the OpenGL shader program object (handle), a Boolean variable indicating whether or not the program has been successfully linked (linked), and a map used to store uniform locations as they are discovered (uniformLocations).
The compileShader overloads will throw a 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 file name can be provided as a third argument in the case that the string was taken from a file, which is helpful for providing better error messages.
The GLSLProgramException's error message will contain the contents of the shader log or program log when an error occurs.
The private function getUniformLocation is used by the setUniform functions to find the location of a uniform variable. It checks the map uniformLocations 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 zero. The variable handle 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 variable linked to true and returns true. Otherwise, it gets the program log (by calling glGetProgramInfoLog), stores the result in a GLSLProgramException and throws it.
The use function simply calls glUseProgram if the program has already been successfully linked, otherwise it does nothing.
The functions getHandle and isLinked are simply "getter" functions that return the handle to the OpenGL program object and the value of the linked variable.
The functions bindAttribLocation and bindFragDataLocation 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. Each of them calls getUniformLocation to query for the variable's location before calling the glUniform function.
Finally, the printActiveUniforms, printActiveUniformBlocks, and printActiveAttribs functions are useful for debugging purposes. They simply display a list of the active uniforms/attributes to standard output.
The following is a simple example of the use of the GLSLProgram class: