Book Image

OpenGL 4 Shading Language Cookbook - Second Edition

By : David Wolff
Book Image

OpenGL 4 Shading Language Cookbook - Second Edition

By: David Wolff

Overview of this book

OpenGL Shading Language (GLSL) is a programming language used for customizing parts of the OpenGL graphics pipeline that were formerly fixed-function, and are executed directly on the GPU. It provides programmers with unprecedented flexibility for implementing effects and optimizations utilizing the power of modern GPUs. With Version 4, the language has been further refined to provide programmers with greater power and flexibility, with new stages such as tessellation and compute. OpenGL 4 Shading Language Cookbook provides easy-to-follow examples that first walk you through the theory and background behind each technique, and then go on to provide and explain the GLSL and OpenGL code needed to implement it. Beginner level through to advanced techniques are presented including topics such as texturing, screen-space techniques, lighting, shading, tessellation shaders, geometry shaders, compute shaders, and shadows. OpenGL Shading Language 4 Cookbook is a practical guide that takes you from the fundamentals of programming with modern GLSL and OpenGL, through to advanced techniques. The recipes build upon each other and take you quickly from novice to advanced level code. You'll see essential lighting and shading techniques; examples that demonstrate how to make use of textures for a wide variety of effects and as part of other techniques; examples of screen-space techniques including HDR rendering, bloom, and blur; shadowing techniques; tessellation, geometry, and compute shaders; how to use noise effectively; and animation with particle systems. OpenGL Shading Language 4 Cookbook provides examples of modern shading techniques that can be used as a starting point for programmers to expand upon to produce modern, interactive, 3D computer graphics applications.
Table of Contents (17 chapters)
OpenGL 4 Shading Language Cookbook Second Edition
About the Author
About the Reviewers

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 {
  GLSLProgramException( const string & msg ) : std::runtime_error(msg) { }

We'll use an enum for the various shader types:

namespace GLSLShader {
  enum GLSLShaderType {

The program class itself has the following interface:

class GLSLProgram
  int  handle;
  bool linked;
  std::map<string, int> uniformLocations;

  int  getUniformLocation(const char * name );

   // A few other helper functions


  void compileShader( const char * filename )throw(GLSLProgramException);
  void compileShader( const char * filename, GLSLShader::GLSLShaderType type )throw(GLSLProgramException);
  void compileShader( const string & source, GLSLShader::GLSLShaderType type,const char * filename = NULL )throw(GLSLProgramException);
  void link()      throw(GLSLProgramException);
  void use()       throw(GLSLProgramException);
  void validate()  throw(GLSLProgramException);

  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();
  void   printActiveUniformBlocks();


Code Download Tip

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Full source code for all of the recipes in this text is also available on GitHub at:

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:

GLSLProgram prog;

try {
} catch( GLSLProgramException &e ) {
  cerr << e.what() << endl;


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

See also