Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends

VR3ShaderProgram Class Reference

The shader program class. More...

#include <VR3ShaderProgram.h>

Collaboration diagram for VR3ShaderProgram:
Collaboration graph
[legend]

List of all members.

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:

  • [in] v0, v1, v2, v3, value = uniform values
  • [in] count = number of values
  • [in] transpose = true if the matrices must be transposed
  • [in] unit_id = the texture unit id to load in the sampler
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 VR3ShaderProgramGetActiveProgram ()
 Gets the current VR3Scene object.

Private Member Functions

 VR3ShaderProgram (const VR3ShaderProgram &)
 Disable default copy constructor.
VR3ShaderProgramoperator= (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 VR3ShaderProgramms_activeprogram
 Currently active external shader (0 if none)

Friends

class VR3ShaderManager
 Friend class VR3ShaderManager to access the private data members.

Detailed Description

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:

  1. GLSL version is defined to be 3.30, do not attempt to use the version preprocessor directive.
  2. Fragment shaders may define the following varying out variable:
    • out vec4 fragColor0; - output color on the default (0) draw buffer (color attachment 0 if using VR3FBOs)
    • out vec4 fragColor1; - output color on the draw buffer 1 (color attachment 1 - use with VR3FBOs)
    • out vec4 fragColor2; - output color on the draw buffer 2 (color attachment 2 - use with VR3FBOs)
    • out vec4 fragColor3; - output color on the draw buffer 3 (color attachment 3 - use with VR3FBOs)
    • out vec4 fragColor4; - output color on the draw buffer 4 (color attachment 4 - use with VR3FBOs)
    • etc... (up to implementation dependent limit)
  3. Vertex, geometry and fragment shaders MAY access the following set of standard uniform variables:
    1. Transformations
      • uniform mat4 modelMatrix; - modeling matrix
      • uniform mat4 viewMatrix; - viewing matrix
      • uniform mat4 projMatrix; - projection matrix
    2. Material properties
      • uniform vec3 matEmission; - material emission color
      • uniform vec3 matAmbient; - material ambient color
      • uniform vec3 matDiffuse; - material diffuse color
      • uniform vec3 matSpecular; - material specular color
      • uniform float matOpacity; - material opacity
      • uniform float matShininess; - material shininess
    3. Texture properties
      • uniform bool diffFlag; - true if diffuse texture mapping uniforms are available for the current object
      • uniform sampler2D diffSampler; - diffuse texture sampler
      • uniform mat4 diffMatrix; - diffuse texture map coordinates transformation matrix
      • uniform bool lightFlag; - true if light mapping uniforms are available for the current object
      • uniform sampler2D lightSampler; - light map sampler
      • uniform mat4 lightMatrix; - light map coordinates transformation matrix
      • uniform bool normFlag; - true if normal mapping uniforms are available for the current object
      • uniform sampler2D normSampler; - normal map sampler
      • uniform mat4 normMatrix; - normal map coordinates transformation matrix
      • uniform bool dispFlag; - true if displacement mapping uniforms are available for the current object
      • uniform sampler2D dispSampler; - displacement map sampler
      • uniform mat4 dispMatrix; - displacement map coordinates transformation matrix
      • uniform int dispTF; - displacement map tessellation factor
      • uniform int dispBI; - displacement map bias
      • uniform int dispSC; - displacement map scale
    4. Light properties
    5. Environment mapping properties
      • uniform int envMode; - environment mapping mode (scene-dependent)
      • uniform samplerCube envDiffSampler; - environment diffuse map sampler
      • uniform samplerCube envSpecSampler; - environment specular map sampler
  4. Vertex shaders may access the following set of input vertex attributes:
    • in vec3 vertex; - vertex position object coordinates
    • in vec3 normal; - normal vector in object coordinates
    • in vec2 diffCoord; - diffuse texture coordinates
    • in vec2 lightCoord; - light map coordinates
    • in vec2 normCoord; - normal map coordinates
    • in vec2 dispCoord; - displacement map coordinates

Definition at line 117 of file VR3ShaderProgram.h.


Constructor & Destructor Documentation

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.

Parameters:
[in]shaderThe full shader source or the name of the file containing it
[in]code_in_partrue 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.

Parameters:
[in]vertexThe vertex shader source or the name of the file containing it
[in]geometryThe geometry shader source or the name of the file containing it (may be 0 if not used)
[in]fragmentThe fragment shader source or the name of the file containing it
[in]code_in_partrue 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.


Member Function Documentation

static VR3ShaderProgram* VR3ShaderProgram::GetActiveProgram (  ) [inline, static]

Gets the current VR3Scene object.

This static method gets the currently active program.

Returns:
The 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.

Parameters:
[in]filenameName of the source code file
[out]filestrThe 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.

Returns:
The array of standard uniform locations

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.

Parameters:
[in]shadersThe full program source code
[out]vertexThe resulting vertex shader source code
[out]geometryThe resulting geometry shader source code (empty if none)
[out]fragmentThe resulting fragment shader source code
Returns:
The loaded source code string (in dynamic memory)
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.


Friends And Related Function Documentation

friend class VR3ShaderManager [friend]

Friend class VR3ShaderManager to access the private data members.

Definition at line 119 of file VR3ShaderProgram.h.


Member Data Documentation

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.

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.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines