VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Public Member Functions | Protected Member Functions | List of all members

Implementation of Shader which compiles itself from shader code. More...

Inheritance diagram for ShaderBase:
Shader

Public Member Functions

 ShaderBase (const std::string &vertexShaderCode, const std::string &fragmentShaderCode, const AttributeSetPtr &attributes, const std::vector< std::string > &uniformNames)
 Constructs a ShaderBase with the given code for its vertex and fragment shader. More...
 
virtual bool isCurrent () const override
 Returns whether or not this shader is bound. More...
 
virtual bool isValid () const override
 Returns whether or not this shader is valid (compiled and linked) and can be used for rendering. More...
 
virtual bool isCompiled () const override
 Returns whether or not this shader has been processed for compilation. More...
 
virtual GLuint getProgramId () const override
 Returns the GL program id for this shader. More...
 
virtual void begin () override
 Binds this shader, compiling if necessary. More...
 
virtual void end () override
 Unbinds this shader if bound by a corresponding call to begin(). More...
 
virtual void compile () override
 Compiles this Shader's GLSL code and links it into a GL program. More...
 
virtual std::string getErrorLog () const override
 Retrieves the string describing compilation errors, if the call to compile() failed and isValid() is false. More...
 
void dispose ()
 Destroys GL resources that were allocated for this ShaderBase. More...
 
virtual int getAttributeLocation (int index) const override
 Returns the location of an indexed Attribute. More...
 
virtual int getUniformLocation (int index) const override
 Returns the location of an indexed uniform. More...
 
- Public Member Functions inherited from Shader
 Shader (const AttributeSetPtr &attributes, const std::vector< std::string > &uniformNames)
 Construct a Shader with the given list of attributes and uniforms. More...
 
const AttributeSetPtrgetAttributes () const
 Returns the list of indexed Attributes for this shader, as an AttributeSet. More...
 
const std::vector< std::string > & getUniformNames () const
 Returns the list of indexed uniforms. More...
 
void setIndexedUniform1f (int index, bool global, float value, float *stateValue)
 Set the value of a float scalar uniform at the given index. More...
 
void setIndexedUniform2f (int index, bool global, const Vector2 &value, Vector2 *stateValue)
 Set the value of a 2D float vector uniform at the given index. More...
 
void setIndexedUniform3f (int index, bool global, float value1, float value2, float value3, float *stateValue)
 Set the value of a 3D float vector uniform at the given index. More...
 
void setIndexedUniform4f (int index, bool global, float value1, float value2, float value3, float value4, float *stateValue)
 Set the value of a 4D float vector uniform at the given index. More...
 
void setIndexedUniform1i (int index, bool global, int value, int *stateValue)
 Set the value of an integer scalar uniform at the given index. More...
 
void setIndexedUniformMatrix4f (int index, bool global, bool transpose, const Matrix4 &value, Matrix4 *stateValue)
 Set the value of a 4x4 float matrix uniform at the given index. More...
 

Protected Member Functions

virtual void performCompile ()
 Compiles and initializes the GL shader program from shader code. More...
 

Detailed Description

Implementation of Shader which compiles itself from shader code.

Vertex and fragment shader code is passed to ShaderBase's constructor, along with attributes and uniforms (as expected by the base class, Shader). The shader code is stored, and compiled by a call to compile(), or the first time it is selected by a call to begin().

Note
This class does not currently restore GL resources on context change.

Constructor & Destructor Documentation

ShaderBase::ShaderBase ( const std::string &  vertexShaderCode,
const std::string &  fragmentShaderCode,
const AttributeSetPtr attributes,
const std::vector< std::string > &  uniformNames 
)

Constructs a ShaderBase with the given code for its vertex and fragment shader.

The GLSL shader code provided to this function will be stored for compilation. Compilation is performed by a call to compile(), or the first call to begin().

Parameters
vertexShaderCodeGLSL vertex shader code
fragmentShaderCodeGLSL fragment shader code
attributesAttributeSet containing the list of indexed attributes for this shader
uniformNamesList of indexed uniforms

Member Function Documentation

virtual void ShaderBase::begin ( )
overridevirtual

Binds this shader, compiling if necessary.

Implements Shader.

virtual void ShaderBase::compile ( )
overridevirtual

Compiles this Shader's GLSL code and links it into a GL program.

This method is automatically called by begin() as necessary. After the first compile attempt, isCompiled() returns true and isValid() will return whether compilation and linking was successful. Subsequent calls to compile() have no effect.

Implements Shader.

void ShaderBase::dispose ( )

Destroys GL resources that were allocated for this ShaderBase.

virtual void ShaderBase::end ( )
overridevirtual

Unbinds this shader if bound by a corresponding call to begin().

Implements Shader.

virtual int ShaderBase::getAttributeLocation ( int  index) const
overridevirtual

Returns the location of an indexed Attribute.

Indexed attributes are those that are named in the AttributeSet provided to the shader's constructor.

Parameters
indexIndex into the list of attributes

Implements Shader.

virtual std::string ShaderBase::getErrorLog ( ) const
overridevirtual

Retrieves the string describing compilation errors, if the call to compile() failed and isValid() is false.

Returns
String describing compilation errors

Implements Shader.

virtual GLuint ShaderBase::getProgramId ( ) const
overridevirtual

Returns the GL program id for this shader.

Implements Shader.

virtual int ShaderBase::getUniformLocation ( int  index) const
overridevirtual

Returns the location of an indexed uniform.

Indexed uniforms are those that are named in the list of uniforms provided to the shader's constructor.

Parameters
indexIndex into the list of uniforms

Implements Shader.

virtual bool ShaderBase::isCompiled ( ) const
overridevirtual

Returns whether or not this shader has been processed for compilation.

Note
This does not indicate whether compilation succeeded. To test for success, use isValid().

Implements Shader.

virtual bool ShaderBase::isCurrent ( ) const
overridevirtual

Returns whether or not this shader is bound.

Implements Shader.

virtual bool ShaderBase::isValid ( ) const
overridevirtual

Returns whether or not this shader is valid (compiled and linked) and can be used for rendering.

Implements Shader.

virtual void ShaderBase::performCompile ( )
protectedvirtual

Compiles and initializes the GL shader program from shader code.

Note
This method is virtual and can be overriden by subclasses to perform additional initialization on compilation.