BARE2D
BARE2D::ShaderProgram Class Reference

The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment shader - to allow rendering. Each renderer should have its own program. More...

#include <ShaderProgram.hpp>

+ Collaboration diagram for BARE2D::ShaderProgram:

Public Member Functions

 ShaderProgram ()
 
 ~ShaderProgram ()
 
void compileShaders (const char *vertexShaderPath, const char *fragmentShaderPath)
 Compiles the shaders. Does not link them. More...
 
void compileShadersFromSource (const char *vertexSource, const char *fragmentSource)
 Similar to compileShaders, this just compiles the shaders. More...
 
void linkShaders (std::initializer_list< std::string > attributes)
 Links the compiled shaders together to create a coherent shader program. More...
 
template<class T >
void setUniform (const std::string uniform, T *data, unsigned int num=1)
 A generalized wrapper to find and set a uniform for this shader. More...
 
template<class T >
void setUniformMatrix (const std::string uniform, bool transpose, T *data, unsigned int num=1)
 Similar to setUniform. More...
 
bool doesUniformExist (const std::string uniform)
 
void bindFragOutputLocation (std::string outputVariableName, unsigned int location)
 A call to glBindFragDataLocation() - Causes an output variable of the fragment shader to output to a specific location. This is roughly equivalent to modern GLSL's layout(location=n) specifier, which is unusable until GLSL 330. More...
 
void use ()
 Activates this shader program for the renderer to use. More...
 
void unuse ()
 Deactivates this shader program. More...
 
void destroy ()
 Releases all bound objects and deletes any allocated stuff. More...
 
template<>
void setUniform (const std::string uniform, int *data, unsigned int num)
 
template<>
void setUniform (const std::string uniform, unsigned int *data, unsigned int num)
 
template<>
void setUniform (const std::string uniform, float *data, unsigned int num)
 

Private Member Functions

void compileShaderFromSource (const char *source, const std::string &name, GLuint id)
 Compiles a shader from source with OpenGL id. More...
 
GLint getUniformLocation (const std::string &uniform)
 Gets the location of a uniform in a program. More...
 

Private Attributes

unsigned int m_numberAttributes = 0
 
GLuint m_programID
 
GLuint m_vertexShaderID
 
GLuint m_fragmentShaderID
 

Detailed Description

The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment shader - to allow rendering. Each renderer should have its own program.

Definition at line 14 of file ShaderProgram.hpp.

Constructor & Destructor Documentation

◆ ShaderProgram()

BARE2D::ShaderProgram::ShaderProgram ( )

Definition at line 14 of file ShaderProgram.cpp.

◆ ~ShaderProgram()

BARE2D::ShaderProgram::~ShaderProgram ( )

Definition at line 19 of file ShaderProgram.cpp.

Member Function Documentation

◆ bindFragOutputLocation()

void BARE2D::ShaderProgram::bindFragOutputLocation ( std::string  outputVariableName,
unsigned int  location 
)

A call to glBindFragDataLocation() - Causes an output variable of the fragment shader to output to a specific location. This is roughly equivalent to modern GLSL's layout(location=n) specifier, which is unusable until GLSL 330.

Parameters
outputVariableNameThe name of the output variable (oftentimes just colour or something)
locationThe output location, corresponding to a colour attachment. Must be less than GL_MAX_DRAW_BUFFERS.

Definition at line 141 of file ShaderProgram.cpp.

References m_programID.

Referenced by BARE2D::Renderer::link().

+ Here is the caller graph for this function:

◆ compileShaderFromSource()

void BARE2D::ShaderProgram::compileShaderFromSource ( const char *  source,
const std::string &  name,
GLuint  id 
)
private

Compiles a shader from source with OpenGL id.

Parameters
sourceThe source to compile from.
nameThe name of the shader. This is normally associated with the key in the cache.
idThe ID of the shader that has been created with OpenGL calls.

Definition at line 183 of file ShaderProgram.cpp.

References BARE2D::SHADER_COMPILE_FAILURE, and BARE2D::throwFatalError().

Referenced by compileShadersFromSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compileShaders()

void BARE2D::ShaderProgram::compileShaders ( const char *  vertexShaderPath,
const char *  fragmentShaderPath 
)

Compiles the shaders. Does not link them.

Parameters
vertexShaderPathThe path to the vertex shader
fragmentShaderPathThe path to the fragment shader.

Definition at line 23 of file ShaderProgram.cpp.

References compileShadersFromSource(), BARE2D::ResourceManager::getAssetsPathPrefix(), and BARE2D::IOManager::readFileToBuffer().

Referenced by BARE2D::BasicRenderer::init(), BARE2D::FBORenderer::init(), BARE2D::TexturelessRenderer::init(), and BARE2D::ResourceManager::loadShaders().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compileShadersFromSource()

void BARE2D::ShaderProgram::compileShadersFromSource ( const char *  vertexSource,
const char *  fragmentSource 
)

Similar to compileShaders, this just compiles the shaders.

Parameters
vertexSourceThe source code for the vertex shader
fragmentSourceThe source code for the fragment shader

Definition at line 38 of file ShaderProgram.cpp.

References compileShaderFromSource(), destroy(), BARE2D::FRAGMENT_SHADER_FAILURE, BARE2D::GLSL_PROGRAM_FAILURE, m_fragmentShaderID, m_programID, m_vertexShaderID, BARE2D::throwFatalError(), and BARE2D::VERTEX_SHADER_FAILURE.

Referenced by compileShaders(), BARE2D::DebugRenderer::init(), and BARE2D::ResourceManager::loadShadersFromSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ destroy()

void BARE2D::ShaderProgram::destroy ( )

Releases all bound objects and deletes any allocated stuff.

Definition at line 172 of file ShaderProgram.cpp.

References m_fragmentShaderID, m_numberAttributes, m_programID, and m_vertexShaderID.

Referenced by compileShadersFromSource(), and BARE2D::Renderer::destroy().

+ Here is the caller graph for this function:

◆ doesUniformExist()

bool BARE2D::ShaderProgram::doesUniformExist ( const std::string  uniform)
Parameters
uniformThe uniform to check for
Returns
True if the uniform is in the compiled shader program, false otherwise.

Definition at line 213 of file ShaderProgram.cpp.

References getUniformLocation().

Referenced by BARE2D::FBORenderer::initUniforms().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getUniformLocation()

GLint BARE2D::ShaderProgram::getUniformLocation ( const std::string &  uniform)
private

Gets the location of a uniform in a program.

Parameters
uniformThe name of the uniform to find.
Returns
The location of the uniform.

Definition at line 125 of file ShaderProgram.cpp.

References m_programID, BARE2D::throwError(), and BARE2D::UNIFORM_NOT_FOUND.

Referenced by doesUniformExist(), and setUniform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linkShaders()

void BARE2D::ShaderProgram::linkShaders ( std::initializer_list< std::string >  attributes)

Links the compiled shaders together to create a coherent shader program.

Parameters
attributesAn initializer_list of std::string type of attributes to add to the program. These must link up with the renderer used to draw with this program.

Definition at line 75 of file ShaderProgram.cpp.

References m_fragmentShaderID, m_numberAttributes, m_programID, m_vertexShaderID, BARE2D::SHADER_LINK_FAILURE, and BARE2D::throwFatalError().

Referenced by BARE2D::Renderer::link().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setUniform() [1/4]

template<>
void BARE2D::ShaderProgram::setUniform ( const std::string  uniform,
float *  data,
unsigned int  num 
)

Definition at line 248 of file ShaderProgram.cpp.

References getUniformLocation().

+ Here is the call graph for this function:

◆ setUniform() [2/4]

template<>
void BARE2D::ShaderProgram::setUniform ( const std::string  uniform,
int *  data,
unsigned int  num 
)

Definition at line 224 of file ShaderProgram.cpp.

References getUniformLocation().

+ Here is the call graph for this function:

◆ setUniform() [3/4]

template<class T >
void BARE2D::ShaderProgram::setUniform ( const std::string  uniform,
T *  data,
unsigned int  num = 1 
)

A generalized wrapper to find and set a uniform for this shader.

Parameters
uniformThe name of the uniform in the shader
dataThe variable to actually set the uniform's value to. This can be a glm::{i,ui,f}vec{1,2,3,4}, an unsigned int, an int, or a float

Referenced by BARE2D::FBORenderer::initUniforms(), BARE2D::BumpyRenderer::initUniforms(), and BARE2D::BasicRenderer::preRender().

+ Here is the caller graph for this function:

◆ setUniform() [4/4]

template<>
void BARE2D::ShaderProgram::setUniform ( const std::string  uniform,
unsigned int *  data,
unsigned int  num 
)

Definition at line 236 of file ShaderProgram.cpp.

References getUniformLocation().

+ Here is the call graph for this function:

◆ setUniformMatrix()

template<class T >
void BARE2D::ShaderProgram::setUniformMatrix ( const std::string  uniform,
bool  transpose,
T *  data,
unsigned int  num = 1 
)

Similar to setUniform.

Parameters
uniformThe name of the uniform in the shader
transposeShould the matrix be transposed when converted? If false, each matrix is assumed to be supplied in column major order. Else, row major order
dataA pointer to the matrix, which is a 1D array of floats, ints, or unsigned ints.

Referenced by BARE2D::BasicRenderer::preRender(), BARE2D::TexturelessRenderer::preRender(), BARE2D::BumpyRenderer::preRender(), and BARE2D::FBORenderer::preRender().

+ Here is the caller graph for this function:

◆ unuse()

void BARE2D::ShaderProgram::unuse ( )

◆ use()

void BARE2D::ShaderProgram::use ( )

Activates this shader program for the renderer to use.

Definition at line 147 of file ShaderProgram.cpp.

References m_numberAttributes, and m_programID.

Referenced by BARE2D::Renderer::begin(), BARE2D::FBORenderer::begin(), BARE2D::FBORenderer::init(), BARE2D::Renderer::init(), BARE2D::BumpyRenderer::render(), BARE2D::FBORenderer::render(), BARE2D::Renderer::render(), and BARE2D::DebugRenderer::render().

+ Here is the caller graph for this function:

Field Documentation

◆ m_fragmentShaderID

GLuint BARE2D::ShaderProgram::m_fragmentShaderID
private

Definition at line 90 of file ShaderProgram.hpp.

Referenced by compileShadersFromSource(), destroy(), and linkShaders().

◆ m_numberAttributes

unsigned int BARE2D::ShaderProgram::m_numberAttributes = 0
private

Definition at line 86 of file ShaderProgram.hpp.

Referenced by destroy(), linkShaders(), unuse(), and use().

◆ m_programID

GLuint BARE2D::ShaderProgram::m_programID
private

◆ m_vertexShaderID

GLuint BARE2D::ShaderProgram::m_vertexShaderID
private

Definition at line 89 of file ShaderProgram.hpp.

Referenced by compileShadersFromSource(), destroy(), and linkShaders().


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