The shader program class. More...
#include <VR3ShaderProgram.h>
Public Member Functions | |
Constructor and Destructor | |
VR3ShaderProgram (const char *shader, bool code_in_par) | |
Constructor of the class. | |
VR3ShaderProgram (const char *vertex, const char *geometry, const char *fragment, bool code_in_par) | |
Constructor of the class. | |
~VR3ShaderProgram () | |
Destructor of the class. | |
Activation and Deactivation | |
The following set of functions is used to load a value in a uniform variable of an external loaded shader. The operation takes effect immediately and the value is retained until a new value is specified or the shader dies. The functions classification is as follows: SendUniformX - Load uniform values and vectors SendUniformXv - Load uniform arrays SendUniformMatrixXfv - Load uniform matrices and matrix arrays SendUnifirmTextures - Load samplers Parameters:
| |
void | Start () |
Starts the shader. | |
void | Stop () |
Stops the shader. | |
void | SendUniform1 (const char *varname, GLfloat v0) |
void | SendUniform2 (const char *varname, GLfloat v0, GLfloat v1) |
void | SendUniform3 (const char *varname, GLfloat v0, GLfloat v1, GLfloat v2) |
void | SendUniform4 (const char *varname, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) |
void | SendUniform1 (const char *varname, GLint v0) |
void | SendUniform2 (const char *varname, GLint v0, GLint v1) |
void | SendUniform3 (const char *varname, GLint v0, GLint v1, GLint v2) |
void | SendUniform4 (const char *varname, GLint v0, GLint v1, GLint v2, GLint v3) |
void | SendUniform1v (const char *varname, GLsizei count, GLfloat *value) |
void | SendUniform2v (const char *varname, GLsizei count, GLfloat *value) |
void | SendUniform3v (const char *varname, GLsizei count, GLfloat *value) |
void | SendUniform4v (const char *varname, GLsizei count, GLfloat *value) |
void | SendUniform1v (const char *varname, GLsizei count, GLint *value) |
void | SendUniform2v (const char *varname, GLsizei count, GLint *value) |
void | SendUniform3v (const char *varname, GLsizei count, GLint *value) |
void | SendUniform4v (const char *varname, GLsizei count, GLint *value) |
void | SendUniformMatrix2fv (const char *varname, GLsizei count, bool transpose, GLfloat *value) |
void | SendUniformMatrix3fv (const char *varname, GLsizei count, bool transpose, GLfloat *value) |
void | SendUniformMatrix4fv (const char *varname, GLsizei count, bool transpose, GLfloat *value) |
void | SendUniformTexture (const char *varname, unsigned int unit_id) |
Standard Uniform Locations | |
const GLint * | GetStdUniforms () |
Gets a pointer to the array of standard uniform locations. | |
Static Public Member Functions | |
static VR3ShaderProgram * | GetActiveProgram () |
Gets the current VR3Scene object. | |
Private Member Functions | |
VR3ShaderProgram (const VR3ShaderProgram &) | |
Disable default copy constructor. | |
VR3ShaderProgram & | operator= (const VR3ShaderProgram &) |
Disable default assignment operator. | |
Source Loading Functions | |
void | GetSource (const char *filename, std::string &filestr) |
Loads an entire source code file. | |
void | SplitShaders (const std::string &shaders, std::string &vertex, std::string &geometry, std::string &fragment) |
Splits a full shader program source code into its components. | |
Private Attributes | |
GLuint | m_program |
Shader program name. | |
std::unordered_map < std::string, GLint > | m_uniforms |
Uniform names to locations cache (non-standard uniforms) | |
GLint | m_std_uniforms [VR3_NUM_STD_UNIFORMS] |
Standard uniforms identifiers to locations. | |
Static Private Attributes | |
static VR3ShaderProgram * | ms_activeprogram |
Currently active external shader (0 if none) | |
Friends | |
class | VR3ShaderManager |
Friend class VR3ShaderManager to access the private data members. |
The shader program class.
The VR3ShaderProgram class implements the functionalities related to the general management of a shader. Shaders must be written in OpenGL Shading Language (GLSL) version 3.30 or below. This class gives programmers the ability of using external shaders in a very simple way: load a shader, set uniform parameters, activate it, and deactivate it when it's not needed anymore.
To load a whole shader program from a file it is necessary to create a text file with this syntax:
[VERTEX SHADER] .......... ...code... .......... [GEOMETRY SHADER] - optional .......... ...code... .......... [FRAGMENT SHADER] .......... ...code... ..........
Alternatively, it is possible to load a shader from three separate text files containing the vertex shader code and the fragment shader code or, wanting to incapsulate the shader codes inside an XVR script, the shaders code can be directly specified as strings.
The following rules apply to user-defined shaders:
Definition at line 117 of file VR3ShaderProgram.h.
VR3ShaderProgram::VR3ShaderProgram | ( | const VR3ShaderProgram & | ) | [private] |
Disable default copy constructor.
VR3ShaderProgram::VR3ShaderProgram | ( | const char * | shader, |
bool | code_in_par | ||
) |
Constructor of the class.
Creates a new VR3ShaderProgram object and initializes the object by building, compiling and linking the program specified with the input parameters.
[in] | shader | The full shader source or the name of the file containing it |
[in] | code_in_par | true if the shader source is the provided shader string, false if the string represent a file name |
VR3ShaderProgram::VR3ShaderProgram | ( | const char * | vertex, |
const char * | geometry, | ||
const char * | fragment, | ||
bool | code_in_par | ||
) |
Constructor of the class.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
[in] | vertex | The vertex shader source or the name of the file containing it |
[in] | geometry | The geometry shader source or the name of the file containing it (may be 0 if not used) |
[in] | fragment | The fragment shader source or the name of the file containing it |
[in] | code_in_par | true if the shader source is the provided shader strings, false if the strings represent a file name |
VR3ShaderProgram::~VR3ShaderProgram | ( | ) |
Destructor of the class.
Deletes the VR3ShaderProgram object.
static VR3ShaderProgram* VR3ShaderProgram::GetActiveProgram | ( | ) | [inline, static] |
Gets the current VR3Scene object.
This static method gets the currently active program.
Definition at line 247 of file VR3ShaderProgram.h.
void VR3ShaderProgram::GetSource | ( | const char * | filename, |
std::string & | filestr | ||
) | [private] |
Loads an entire source code file.
An entire file is loaded into a string returned to the caller, this is useful when we have to load a file containing the source code for a particolar vertex, geometry or fragment shader.
[in] | filename | Name of the source code file |
[out] | filestr | The string where the file is loaded into |
const GLint* VR3ShaderProgram::GetStdUniforms | ( | ) | [inline] |
Gets a pointer to the array of standard uniform locations.
This function is used by the VR3Mesh class during rendering to access the locations of uniform parameters, an array is returned containing the locations of all standard uniforms.
Definition at line 313 of file VR3ShaderProgram.h.
VR3ShaderProgram& VR3ShaderProgram::operator= | ( | const VR3ShaderProgram & | ) | [private] |
Disable default assignment operator.
void VR3ShaderProgram::SendUniform1 | ( | const char * | varname, |
GLint | v0 | ||
) |
void VR3ShaderProgram::SendUniform1 | ( | const char * | varname, |
GLfloat | v0 | ||
) |
void VR3ShaderProgram::SendUniform1v | ( | const char * | varname, |
GLsizei | count, | ||
GLint * | value | ||
) |
void VR3ShaderProgram::SendUniform1v | ( | const char * | varname, |
GLsizei | count, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniform2 | ( | const char * | varname, |
GLfloat | v0, | ||
GLfloat | v1 | ||
) |
void VR3ShaderProgram::SendUniform2 | ( | const char * | varname, |
GLint | v0, | ||
GLint | v1 | ||
) |
void VR3ShaderProgram::SendUniform2v | ( | const char * | varname, |
GLsizei | count, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniform2v | ( | const char * | varname, |
GLsizei | count, | ||
GLint * | value | ||
) |
void VR3ShaderProgram::SendUniform3 | ( | const char * | varname, |
GLfloat | v0, | ||
GLfloat | v1, | ||
GLfloat | v2 | ||
) |
void VR3ShaderProgram::SendUniform3 | ( | const char * | varname, |
GLint | v0, | ||
GLint | v1, | ||
GLint | v2 | ||
) |
void VR3ShaderProgram::SendUniform3v | ( | const char * | varname, |
GLsizei | count, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniform3v | ( | const char * | varname, |
GLsizei | count, | ||
GLint * | value | ||
) |
void VR3ShaderProgram::SendUniform4 | ( | const char * | varname, |
GLfloat | v0, | ||
GLfloat | v1, | ||
GLfloat | v2, | ||
GLfloat | v3 | ||
) |
void VR3ShaderProgram::SendUniform4 | ( | const char * | varname, |
GLint | v0, | ||
GLint | v1, | ||
GLint | v2, | ||
GLint | v3 | ||
) |
void VR3ShaderProgram::SendUniform4v | ( | const char * | varname, |
GLsizei | count, | ||
GLint * | value | ||
) |
void VR3ShaderProgram::SendUniform4v | ( | const char * | varname, |
GLsizei | count, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniformMatrix2fv | ( | const char * | varname, |
GLsizei | count, | ||
bool | transpose, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniformMatrix3fv | ( | const char * | varname, |
GLsizei | count, | ||
bool | transpose, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniformMatrix4fv | ( | const char * | varname, |
GLsizei | count, | ||
bool | transpose, | ||
GLfloat * | value | ||
) |
void VR3ShaderProgram::SendUniformTexture | ( | const char * | varname, |
unsigned int | unit_id | ||
) |
void VR3ShaderProgram::SplitShaders | ( | const std::string & | shaders, |
std::string & | vertex, | ||
std::string & | geometry, | ||
std::string & | fragment | ||
) | [private] |
Splits a full shader program source code into its components.
When a single file is loaded to recover the full shader program source code (with the above sintax), we need to split the single shader objects source code in order to compile and link the shader program. This function is used for that purpose.
[in] | shaders | The full program source code |
[out] | vertex | The resulting vertex shader source code |
[out] | geometry | The resulting geometry shader source code (empty if none) |
[out] | fragment | The resulting fragment shader source code |
void VR3ShaderProgram::Start | ( | ) |
Starts the shader.
This method starts the shader object which becomes the current shader. All the drawing operations after this call will be influenced by the operations set in the current shader. The current shader can be stopped using the Stop() method.
void VR3ShaderProgram::Stop | ( | ) |
Stops the shader.
This method stops the current shader object. If the stopped shader was the active one, all the drawing operations after this call will use the default shaders and will not be influenced by the shader.
friend class VR3ShaderManager [friend] |
Friend class VR3ShaderManager to access the private data members.
Definition at line 119 of file VR3ShaderProgram.h.
GLuint VR3ShaderProgram::m_program [private] |
Shader program name.
Definition at line 131 of file VR3ShaderProgram.h.
GLint VR3ShaderProgram::m_std_uniforms[VR3_NUM_STD_UNIFORMS] [private] |
Standard uniforms identifiers to locations.
Definition at line 137 of file VR3ShaderProgram.h.
std::unordered_map<std::string, GLint> VR3ShaderProgram::m_uniforms [private] |
Uniform names to locations cache (non-standard uniforms)
Definition at line 134 of file VR3ShaderProgram.h.
VR3ShaderProgram* VR3ShaderProgram::ms_activeprogram [static, private] |
Currently active external shader (0 if none)
When an external (user-provided) shader is activated, the standard shaders built using material properties are not used. When an object must be rendered, this variable is checked to understand if an external shader program is active (in such case, rendering takes place using the external shader instead of the mesh-dependent one).
Definition at line 128 of file VR3ShaderProgram.h.