Building a C++ shader program class
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) { } };
We'll use enum
for the various shader types:
namespace GLSLShader { enum GLSLShaderType { VERTEX = GL_VERTEX_SHADER, FRAGMENT = GL_FRAGMENT_SHADER, GEOMETRY = GL_GEOMETRY_SHADER...