Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenGL 4 Shading Language Cookbook, Second Edition

You're reading from   OpenGL 4 Shading Language Cookbook, Second Edition Acquiring the skills of OpenGL Shading Language is so much easier with this cookbook. You'll be creating graphics rather than learning theory, gaining a high level of capability in modern 3D programming along the way.

Arrow left icon
Product type Paperback
Published in Dec 2013
Publisher Packt
ISBN-13 9781782167020
Length 394 pages
Edition 2nd Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
David A Wolff David A Wolff
Author Profile Icon David A Wolff
David A Wolff
David Wolff David Wolff
Author Profile Icon David Wolff
David Wolff
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with GLSL FREE CHAPTER 2. The Basics of GLSL Shaders 3. Lighting, Shading, and Optimization 4. Using Textures 5. Image Processing and Screen Space Techniques 6. Using Geometry and Tessellation Shaders 7. Shadows 8. Using Noise in Shaders 9. Particle Systems and Animation 10. Using Compute Shaders Index

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 an enum for the various shader types:

namespace GLSLShader {
  enum GLSLShaderType {
        VERTEX = GL_VERTEX_SHADER, 
        FRAGMENT = GL_FRAGMENT_SHADER, 
        GEOMETRY = GL_GEOMETRY_SHADER, 
        TESS_CONTROL = GL_TESS_CONTROL_SHADER, 
        TESS_EVALUATION = GL_TESS_EVALUATION_SHADER, 
        COMPUTE = GL_COMPUTE_SHADER
  };
};

The program class itself has the following interface:

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

  int  getUniformLocation(const char * name );

   // A few other helper functions

public:
  GLSLProgram();
  ~GLSLProgram();

  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();
};

Tip

Code Download Tip

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.

Full source code for all of the recipes in this text is also available on GitHub at: https://github.com/daw42/glslcookbook.

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 {
  prog.compileShader("myshader.vert");
  prog.compileShader("myshader.frag");
  prog.link();
  prog.validate();
  prog.use();
} catch( GLSLProgramException &e ) {
  cerr << e.what() << endl;
  exit(EXIT_FAILURE);
}


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

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

See also

You have been reading a chapter from
OpenGL 4 Shading Language Cookbook, Second Edition - Second Edition
Published in: Dec 2013
Publisher: Packt
ISBN-13: 9781782167020
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime