CUGL
Cornell University Game Library
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
cugl::Shader Class Reference

#include <CUShader.h>

Inheritance diagram for cugl::Shader:
cugl::SpriteShader

Public Member Functions

 Shader ()
 
 ~Shader ()
 
virtual void dispose ()
 
std::string getVertSource () const
 
std::string getFragSource () const
 
bool isReady () const
 
bool isActive () const
 
GLuint getProgram () const
 
virtual void bind ()
 
virtual void unbind ()
 

Protected Member Functions

virtual bool compile ()
 

Static Protected Member Functions

static bool validateShader (GLuint shader, const char *type)
 
static void logShaderError (GLuint shader)
 
static void logProgramError (GLuint shader)
 

Protected Attributes

GLuint _program
 
GLuint _vertShader
 
GLuint _fragShader
 
const char * _vertSource
 
const char * _fragSource
 
bool _active
 

Detailed Description

This class is the base abstract class for any GLSL shader.

Specific shaders have attached attributes and uniforms. Since it is difficult to write a class that takes all of the possibilities into consideration (and is type-safe), we use subclasses to implement specific shaders. This class just provides support for compilation and binding.

This class is written to be agnostic about whether we are using OpenGL or OpenGLES. Because these shader languages are slightly different, subclasses should take this difference into account.

Constructor & Destructor Documentation

cugl::Shader::Shader ( )
inline

Creates an uninitialized shader with no source.

You must initialize the shader to add a source and compiled it.

cugl::Shader::~Shader ( )
inline

Deletes this shader, disposing all resources.

Member Function Documentation

virtual void cugl::Shader::bind ( )
virtual

Binds this shader, making it active.

Once bound, any OpenGL calls will then be sent to this shader.

Reimplemented in cugl::SpriteShader.

virtual bool cugl::Shader::compile ( )
protectedvirtual

Compiles this shader from the given vertex and fragment shader sources.

When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

If compilation fails, it will display error messages on the log.

Returns
true if compilation was successful.

Reimplemented in cugl::SpriteShader.

virtual void cugl::Shader::dispose ( )
virtual

Deletes the OpenGL shader and resets all attributes.

You must reinitialize the shader to use it.

Reimplemented in cugl::SpriteShader.

std::string cugl::Shader::getFragSource ( ) const
inline

Returns the source string for the fragment shader.

The string is empty if not defined.

Returns
the source string for the fragment shader.
GLuint cugl::Shader::getProgram ( ) const
inline

Returns the OpenGL program associated with this shader.

This method will return 0 if the program is not initialized.

Returns
the OpenGL program associated with this shader.
std::string cugl::Shader::getVertSource ( ) const
inline

Returns the source string for the vertex shader.

The string is empty if not defined.

Returns
the source string for the vertex shader.
bool cugl::Shader::isActive ( ) const
inline

Returns true if this shader is currently active.

The shader is active if its program is currently bound. Any OpenGL calls will then be sent to this shader.

Returns
true if this shader is currently active.
bool cugl::Shader::isReady ( ) const
inline

Returns true if this shader has been compiled and is ready for use.

Returns
true if this shader has been compiled and is ready for use.
static void cugl::Shader::logProgramError ( GLuint  shader)
staticprotected

Displays the program linker errors to the log.

If there were no errors, this method will do nothing.

Parameters
shaderThe program to test
static void cugl::Shader::logShaderError ( GLuint  shader)
staticprotected

Displays the shader compilation errors to the log.

If there were no errors, this method will do nothing.

Parameters
shaderThe shader to test
virtual void cugl::Shader::unbind ( )
virtual

Unbinds this shader, making it no longer active.

Once unbound, OpenGL calls will no longer be sent to this shader.

Reimplemented in cugl::SpriteShader.

static bool cugl::Shader::validateShader ( GLuint  shader,
const char *  type 
)
staticprotected

Returns true if the shader was compiled properly.

If compilation fails, it will display error messages on the log.

Parameters
shaderThe shader to test
typeThe shader type (vertex or fragment)
Returns
true if the shader was compiled properly.

Member Data Documentation

bool cugl::Shader::_active
protected

Whether or not this shader is currently active

GLuint cugl::Shader::_fragShader
protected

The OpenGL fragment shader for this shader

const char* cugl::Shader::_fragSource
protected

The source string for the fragment shader

GLuint cugl::Shader::_program
protected

The OpenGL program for this shader

GLuint cugl::Shader::_vertShader
protected

The OpenGL vertex shader for this shader

const char* cugl::Shader::_vertSource
protected

The source string for the vertex shader


The documentation for this class was generated from the following file: