OpenGL 4.0: Building a C++ Shader Program Class

Exclusive offer: get 50% off this eBook here
OpenGL 4.0 Shading Language Cookbook

OpenGL 4.0 Shading Language Cookbook — Save 50%

Over 60 highly focused, practical recipes to maximize your OpenGL Shading language use

€23.99    €12.00
by David Wolff | August 2011 | Cookbooks Open Source Web Graphics & Video

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 by David Wolff, author of OpenGL 4.0 Shading Language Cookbook, we'll look at a design for a C++ class that can be used to manage a shader program.

 

OpenGL 4.0 Shading Language Cookbook

OpenGL 4.0 Shading Language Cookbook

Over 60 highly focused, practical recipes to maximize your OpenGL Shading language use

        Read more about this book      

(For more resources on this subject, see here.)

Getting ready

There's not much to prepare for with this one, you just need to build an 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.

The reader would benefit from the previous articles on Tips and Tricks for Getting Started with OpenGL and GLSL 4.0 and OpenGL 4.0: Using Uniform Blocks and Uniform Buffer Objects.

How to do it...

We'll use the following header file for our C++ class:

namespace GLSLShader {
enum GLSLShaderType {
VERTEX, FRAGMENT, GEOMETRY,TESS_CONTROL,
TESS_EVALUATION
};
};

class GLSLProgram
{

private:
int handle;
bool linked;
string logString;
int getUniformLocation(const char * name );
bool fileExists( const string & fileName );

public:
GLSLProgram();

bool compileShaderFromFile( const char * fileName,
GLSLShader::GLSLShaderType type );

bool compileShaderFromString( const string & source,
GLSLShader::GLSLShaderType type );

bool link();
void use();

string log();

int getHandle();
bool isLinked();

void bindAttribLocation( GLuint location,
const char * name);
void bindFragDataLocation( GLuint location,
const char * name );

void setUniform(const char *name,float x,float y,
float z);
void setUniform(const char *name, const vec3 & v);
void setUniform(const char *name, const vec4 & v);
void setUniform(const char *name, const mat4 & m);
void setUniform(const char *name, const mat3 & m);
void setUniform(const char *name, float val );
void setUniform(const char *name, int val );
void setUniform(const char *name, bool val );
void printActiveUniforms();
void printActiveAttribs();
};

The techniques involved in the implementation of these functions are covered in previous recipes. (Code available here). 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 string for storing the most recent log produced by a compile or link action (logString).

The two private functions are utilities used by other public functions. The getUniformLocation function is used by the setUniform functions to find the location of a uniform variable, and the fileExists function is used by compileShaderFromFile to check for file existence.

The constructor simply initializes linked to false, handle to zero, and logString to the empty string. The variable handle will be initialized by calling glCreateProgram when the first shader is compiled.

The compileShaderFromFile and compileShaderFromString functions attempt to compile a shader of the given type (the type is provided as the second argument). They create the shader object, load the source code, and then attempt to compile the shader. If successful, the shader object is attached to the OpenGL program object (by calling glAttachShader) and a value of true is returned. Otherwise, the log is retrieved and stored in logString, and a value of false is returned.

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 it in logString, and returns false.

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

The log function returns the contents of logString, which should contain the log of the most recent compile or link action.

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 and printActiveAttribs functions are useful mainly 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:

GLSLProgram prog;

if( ! prog.compileShaderFromFile("myshader.vert",
GLSLShader::VERTEX))
{
printf("Vertex shader failed to compile!\n%s",
prog.log().c_str());
exit(1);
}
if( ! prog.compileShaderFromFile("myshader.frag",
GLSLShader::FRAGMENT))
{
printf("Fragment shader failed to compile!\n%s",
prog.log().c_str());
exit(1);
}

// Possibly call bindAttribLocation or bindFragDataLocation
// here...

if( ! prog.link() )
{
printf("Shader program failed to link!\n%s",
prog.log().c_str());
exit(1);
}

prog.use();
prog.printActiveUniforms();
prog.printActiveAttribs();

prog.setUniform("ModelViewMatrix", matrix);
prog.setUniform("LightPosition", 1.0f, 1.0f, 1.0f);
...

Summary

This article covered the topic of Building a C++ Shader Program Class.


Further resources on this subject:


OpenGL 4.0 Shading Language Cookbook Over 60 highly focused, practical recipes to maximize your OpenGL Shading language use
Published: July 2011
eBook Price: €23.99
Book Price: €38.99
See more
Select your format and quantity:

About the Author :


David Wolff

David Wolff is an associate professor in the Computer Science and Computer Engineering Department at Pacific Lutheran University (PLU). He received a PhD in Physics and an MS in Computer Science from Oregon State University. He has been teaching computer graphics to undergraduates at PLU for over 10 years, using OpenGL.

Books From Packt


Blender 2.5 HOTSHOT
Blender 2.5 HOTSHOT

Blender 2.49 Scripting
Blender 2.49 Scripting

Away3D 3.6 Cookbook
Away3D 3.6 Cookbook

OpenSceneGraph 3.0: Beginner's Guide
OpenSceneGraph 3.0: Beginner's Guide

3D Graphics with XNA Game Studio 4.0
3D Graphics with XNA Game Studio 4.0

Panda3D 1.7 Game Developer's Cookbook
Panda3D 1.7 Game Developer's Cookbook

Papervision3D Essentials
Papervision3D Essentials

OGRE 3D 1.7 Beginner's Guide
OGRE 3D 1.7 Beginner's Guide


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software