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

#include <CUSpriteShader.h>

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

Public Member Functions

 SpriteShader ()
 
 ~SpriteShader ()
 
void dispose () override
 
bool init ()
 
bool init (std::string vsource, std::string fsource)
 
bool init (const char *vsource, const char *fsource)
 
GLint getPositionAttr () const
 
GLint getColorAttr () const
 
GLint getTexCoodAttr () const
 
GLint getPerspectiveUni () const
 
GLint getTextureUni () const
 
void setPerspective (const Mat4 &matrix)
 
const Mat4getPerspective ()
 
void setTexture (const std::shared_ptr< Texture > &texture)
 
std::shared_ptr< TexturegetTexture ()
 
const std::shared_ptr< Texture > & getTexture () const
 
void attach (GLuint vArray, GLuint vBuffer)
 
void bind () override
 
void unbind () override
 
- Public Member Functions inherited from cugl::Shader
 Shader ()
 
 ~Shader ()
 
std::string getVertSource () const
 
std::string getFragSource () const
 
bool isReady () const
 
bool isActive () const
 
GLuint getProgram () const
 

Static Public Member Functions

static std::shared_ptr< SpriteShaderalloc ()
 
static std::shared_ptr< SpriteShaderalloc (std::string vsource, std::string fsource)
 
static std::shared_ptr< SpriteShaderalloc (const char *vsource, const char *fsource)
 

Protected Member Functions

bool compile () override
 
bool validateVariable (GLint variable, const char *name)
 

Additional Inherited Members

- Static Protected Member Functions inherited from cugl::Shader
static bool validateShader (GLuint shader, const char *type)
 
static void logShaderError (GLuint shader)
 
static void logProgramError (GLuint shader)
 
- Protected Attributes inherited from cugl::Shader
GLuint _program
 
GLuint _vertShader
 
GLuint _fragShader
 
const char * _vertSource
 
const char * _fragSource
 
bool _active
 

Detailed Description

This class is a GLSL shader to use with a sprite batch.

This class provides you the option to use your own shader sources. However, any shader used with this class must have the following properties.

First, it must provide three attributes corresponding to the Vertex2 class. These attributes must be names as follows.

 aPosition:      The position attribute

 aColor:         The color attribute

 aTexCoord:      The texture coordinate attribute

In addition, it must include the following two uniforms.

 uPerspective:   The perspective matrix (combined modelview projection)

 uTexture:       The shading texture

Any other attributes or uniforms will be ignored.

Constructor & Destructor Documentation

cugl::SpriteShader::SpriteShader ( )
inline

Creates an uninitialized shader with no source.

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

cugl::SpriteShader::~SpriteShader ( )
inline

Deletes this shader, disposing all resources.

Member Function Documentation

static std::shared_ptr<SpriteShader> cugl::SpriteShader::alloc ( )
inlinestatic

Returns a new shader with the default vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Returns
a new shader with the default vertex and fragment source.
static std::shared_ptr<SpriteShader> cugl::SpriteShader::alloc ( std::string  vsource,
std::string  fsource 
)
inlinestatic

Returns a new shader with the given vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Parameters
vsourceThe source string for the vertex shader.
fsourceThe source string for the fragment shader.
Returns
a new shader with the given vertex and fragment source.
static std::shared_ptr<SpriteShader> cugl::SpriteShader::alloc ( const char *  vsource,
const char *  fsource 
)
inlinestatic

Returns a new shader with the given vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Parameters
vsourceThe source string for the vertex shader.
fsourceThe source string for the fragment shader.
Returns
a new shader with the given vertex and fragment source.
void cugl::SpriteShader::attach ( GLuint  vArray,
GLuint  vBuffer 
)

Attaches the given memory buffer to this shader.

Because of limitations in OpenGL ES, we cannot draw anything without both a vertex buffer object and an vertex array object.

Parameters
vArrayThe vertex array object
vBufferThe vertex buffer object
void cugl::SpriteShader::bind ( )
overridevirtual

Binds this shader, making it active.

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

Reimplemented from cugl::Shader.

bool cugl::SpriteShader::compile ( )
overrideprotectedvirtual

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 from cugl::Shader.

void cugl::SpriteShader::dispose ( )
overridevirtual

Deletes the OpenGL shader and resets all attributes.

You must reinitialize the shader to use it.

Reimplemented from cugl::Shader.

GLint cugl::SpriteShader::getColorAttr ( ) const
inline

Returns the GLSL location for the color attribute

This method will return -1 if the program is not initialized.

Returns
the GLSL location for the color attribute
const Mat4& cugl::SpriteShader::getPerspective ( )
inline

Returns the current perspective matrix in use.

Returns
the current perspective matrix in use.
GLint cugl::SpriteShader::getPerspectiveUni ( ) const
inline

Returns the GLSL location for the perspective matrix uniform

This method will return -1 if the program is not initialized.

Returns
the GLSL location for the perspective matrix uniform
GLint cugl::SpriteShader::getPositionAttr ( ) const
inline

Returns the GLSL location for the position attribute

This method will return -1 if the program is not initialized.

Returns
the GLSL location for the position attribute
GLint cugl::SpriteShader::getTexCoodAttr ( ) const
inline

Returns the GLSL location for the texture coordinate attribute

This method will return -1 if the program is not initialized.

Returns
the GLSL location for the texture coordinate attribute
std::shared_ptr<Texture> cugl::SpriteShader::getTexture ( )
inline

Returns the current texture in use.

Returns
the current texture in use.
const std::shared_ptr<Texture>& cugl::SpriteShader::getTexture ( ) const
inline

Returns the current texture in use.

Returns
the current texture in use.
GLint cugl::SpriteShader::getTextureUni ( ) const
inline

Returns the GLSL location for the texture uniform

This method will return -1 if the program is not initialized.

Returns
the GLSL location for the testure uniform
bool cugl::SpriteShader::init ( )

Initializes this shader with the default vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Returns
true if initialization was successful.
bool cugl::SpriteShader::init ( std::string  vsource,
std::string  fsource 
)
inline

Initializes this shader with the given vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Parameters
vsourceThe source string for the vertex shader.
fsourceThe source string for the fragment shader.
Returns
true if initialization was successful.
bool cugl::SpriteShader::init ( const char *  vsource,
const char *  fsource 
)

Initializes this shader with the given vertex and fragment source.

The shader will compile the vertex and fragment sources and link them together. When compilation is complete, the shader will not be bound. However, any shader that was actively bound during compilation also be unbound as well.

Parameters
vsourceThe source string for the vertex shader.
fsourceThe source string for the fragment shader.
Returns
true if initialization was successful.
void cugl::SpriteShader::setPerspective ( const Mat4 matrix)

Sets the perspective matrix to use in the shader.

Parameters
matrixThe perspective matrix
void cugl::SpriteShader::setTexture ( const std::shared_ptr< Texture > &  texture)

Sets the texture in use in the shader

Parameters
textureThe shader texture
void cugl::SpriteShader::unbind ( )
overridevirtual

Unbinds this shader, making it no longer active.

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

Reimplemented from cugl::Shader.

bool cugl::SpriteShader::validateVariable ( GLint  variable,
const char *  name 
)
protected

Returns true if the GLSL variable was found in this shader.

If variable is not found, it will display error messages on the log.

Parameters
variableThe variable (reference) to test
nameThe variable (name) to test
Returns
true if the GLSL variable was found in this shader.

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