Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends

vl::GLSLProgram Class Reference

Wraps a GLSL program to which you can bind vertex, fragment and geometry shaders. More...

#include <GLSL.hpp>

Inheritance diagram for vl::GLSLProgram:
vl::RenderState vl::Object

List of all members.

Public Member Functions

 GLSLProgram ()
 Constructor.
 ~GLSLProgram ()
 Destructor. Calls deleteProgram().
virtual const char * className ()
 Returns the name of the class.
virtual ERenderState type () const
void createProgram ()
 Calls glCreateProgram() in order to acquire a GLSL program handle, see also http://www.opengl.org/sdk/docs/man/xhtml/glCreateProgram.xml for more information.
void deleteProgram ()
 Deletes the GLSL program calling glDeleteProgram(handle()), see also http://www.opengl.org/sdk/docs/man/xhtml/glDeleteProgram.xml for more information.
unsigned int handle () const
 The handle of the GLSL program as returned by glCreateProgram()
bool useProgram () const
 Equivalent to glUseProgram(handle()), see also http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml for more information.
void apply (const Camera *, OpenGLContext *ctx) const
 Calls useProgram()
bool linkProgram (bool force_relink=false)
 Links the GLSLProgram calling glLinkProgram(handle()) only if the program needs to be linked.
bool linkStatus () const
bool linked () const
 Returns true if the program has been succesfully linked.
void scheduleRelinking ()
 Schedules a relink of the GLSL program.
bool attachShader (GLSLShader *shader)
 Attaches the GLSLShader to this GLSLProgram.
bool detachShader (GLSLShader *shader)
 Removes a GLSLShader from the GLSLShader and schedules a relink of the program, see also http://www.opengl.org/sdk/docs/man/xhtml/glDetachShader.xml for more information.
String infoLog () const
 Returns the info log of this GLSL program using the OpenGL function glGetProgramInfoLog(), see also http://www.opengl.org/sdk/docs/man/xhtml/glGetProgramInfoLog.xml for more information.
bool validateProgram () const
 Returns true if the validation of this GLSL program is succesful, see also http://www.opengl.org/sdk/docs/man/xhtml/glValidateProgram.xml for more information.
void bindAttribLocation (unsigned int index, const std::string &name)
 Equivalent to glBindAttribLocation(handle(), index, name.c_str()) with the difference that this function will automatically create a GLSL program if none is present and it will schedule a re-link since the new specified bindings take effect after linking the GLSL program.
void addAutomaticAttribLocation (const char *attr_name, int attr_index)
 Adds an attribute name / index pair to the automatic attribute location binding list.
void removeAutomaticAttribLocation (const char *attr_name)
 Removes an attribute from the automatic attribute location binding list.
void setAutomaticAttribLocations (const std::map< std::string, int > &attrib_bindings)
 Defines which attribute should be automatically bound to which attribute index at GLSL program linking time.
const std::map< std::string,
int > & 
automaticAttribLocations () const
 Returns which attribute name should be automatically bound to which attribute index at GLSL program linking time.
void clearAutomaticAttribLocations ()
 Clears the automatic attribute location binding list.
int getAttribLocation (const char *name) const
 Eqivalento to glGetAttribLocation(handle(), name).
int shaderCount () const
 Returns the number of GLSLShader objects bound to this GLSLProgram.
const GLSLShadershader (int i) const
 Returns the i-th GLSLShader objects bound to this GLSLProgram.
GLSLShadershader (int i)
 Returns the i-th GLSLShader objects bound to this GLSLProgram.
void detachAllShaders ()
 Removes all the previously linked shaders and schedules a relinking.
void bindFragDataLocation (int color_number, const std::string &name)
void unbindFragDataLocation (const std::string &name)
int fragDataLocationBinding (const std::string &name) const
void setGeometryVerticesOut (int vertex_count)
 See GL_ARB_geometry_shader4's GL_GEOMETRY_VERTICES_OUT_EXT.
int geometryVerticesOut () const
 See GL_ARB_geometry_shader4's GL_GEOMETRY_VERTICES_OUT_EXT.
void setGeometryInputType (EGeometryInputType type)
 See GL_ARB_geometry_shader4's GL_GEOMETRY_INPUT_TYPE_EXT.
EGeometryInputType geometryInputType () const
 See GL_ARB_geometry_shader4's GL_GEOMETRY_INPUT_TYPE_EXT.
void setGeometryOutputType (EGeometryOutputType type)
 See GL_ARB_geometry_shader4's GL_GEOMETRY_OUTPUT_TYPE_EXT.
EGeometryOutputType geometryOutputType () const
 See GL_ARB_geometry_shader4's GL_GEOMETRY_OUTPUT_TYPE_EXT.
bool applyUniformSet (const UniformSet *uniforms) const
 Applies a set of uniforms to the currently bound GLSL program.
int getUniformLocation (const std::string &name) const
 Returns the binding index of the uniform with the given name.
void getUniformfv (int location, float *params) const
 Equivalent to glGetUniformfv(handle(), location, params)
void getUniformfv (const std::string &name, float *params) const
 Equivalent to getUniformfv(getUniformLocation(name), params)
void getUniformiv (int location, int *params) const
 Equivalent to glGetUniformiv(handle(), location, params)
void getUniformiv (const std::string &name, int *params) const
 Equivalent to getUniformiv(getUniformLocation(name)
void getUniform (int location, fvec2 &vec) const
void getUniform (int location, fvec3 &vec) const
void getUniform (int location, fvec4 &vec) const
void getUniform (int location, fmat2 &mat) const
void getUniform (int location, fmat3 &mat) const
void getUniform (int location, fmat4 &mat) const
void getUniform (int location, ivec2 &vec) const
void getUniform (int location, ivec3 &vec) const
void getUniform (int location, ivec4 &vec) const
void getUniform (const std::string &name, fvec2 &vec) const
void getUniform (const std::string &name, fvec3 &vec) const
void getUniform (const std::string &name, fvec4 &vec) const
void getUniform (const std::string &name, fmat2 &mat) const
void getUniform (const std::string &name, fmat3 &mat) const
void getUniform (const std::string &name, fmat4 &mat) const
void getUniform (const std::string &name, ivec2 &vec) const
void getUniform (const std::string &name, ivec3 &vec) const
void getUniform (const std::string &name, ivec4 &vec) const
UniformSetuniformSet ()
 Returns a GLSLProgram's static UniformSet. Static uniforms are those uniforms whose value is constant across one rendering as opposed to Shader uniforms that change across Shaders and Actor uniforms that change across Actors.
const UniformSetuniformSet () const
 Returns a GLSLProgram's static UniformSet. Static uniforms are those uniforms whose value is constant across one rendering as opposed to Shader uniforms that change across Shaders and Actor uniforms that change across Actors.
void setUniformSet (UniformSet *uniforms)
 Sets a GLSLProgram's static UniformSet.
void setUniform (Uniform *uniform)
 Utility function using uniformSet(). Adds a Uniform to this program's static uniform set.
UniformgetUniform (const std::string &name)
 Utility function using uniformSet(). Returns the specified Uniform. Returns NULL if there isn't such a Uniform.
UniformgocUniform (const std::string &name)
 Utility function using uniformSet(). Gets or creates the specified Uniform.
void eraseUniform (const std::string &name)
 Utility function using uniformSet(). Erases the specified uniform.
void eraseUniform (const Uniform *uniform)
 Utility function using uniformSet(). Erases the specified uniform.
void eraseAllUniforms ()
 Utility function using uniformSet(). Erases all the uniforms.

Static Public Member Functions

static int maxVertexAttributes ()
 Equivalent to glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max )

Protected Attributes

std::vector< ref< GLSLShader > > mShaders
std::map< std::string, int > mFragDataLocation
std::map< std::string, int > mUniformLocation
std::map< std::string, int > mAttribLocation
ref< UniformSetmUniformSet
unsigned int mHandle
bool mScheduleLink
int mGeometryVerticesOut
EGeometryInputType mGeometryInputType
EGeometryOutputType mGeometryOutputType

Friends

class Renderer

Detailed Description

Wraps a GLSL program to which you can bind vertex, fragment and geometry shaders.

Uniforms
You have 4 ways to set the value of a uniform:
  1. call useProgram() to activate the GLSLProgram and directly call glUniform* (see also getUniformLocation()).
  2. add a Uniform to the GLSLProgram UniformSet, see vl::GLSLProgram::uniformSet()
  3. add a Uniform to the Actor's UniformSet, see vl::Actor::uniformSet()
  4. add a Uniform to the Actor's Shader UniformSet, see vl::Shader::uniformSet()
Attribute Location Bindings
In order to explicity specify which attribute index should be bound to which attribute name you can do one of the following.
  1. call glBindAttribLocation() with the appropriate GLSLProgram::handle(). This is the most low level way of doing it.
  2. call bindAttribLocation() as you would do normally with glBindAttribLocation() but with the difference the you don't need to specify the GLSL program handle.
  3. create a list of attribute name/indices that will be automatically bound whenever the GLSLProgram is linked. In order to do so you can use the following functions setAutomaticAttribLocations(), automaticAttribLocations(), clearAutomaticAttribLocations(), addAutomaticAttribLocation(), removeAutomaticAttribLocation().

Note that for option #1 and #2 you need to relink the GLSLProgram in order for the changes to take effect (linkProgram(force_relink=true)). Option #2 and #3 automatically schedule a re-link of the GLSL program. See also http://www.opengl.org/sdk/docs/man/xhtml/glBindAttribLocation.xml

Remarks:
The Uniforms defined in the Actor, in the Shader and in the GLSLProgram must not overlap, that is, an Uniform name must belong to one and only one of them.
See also:

Constructor & Destructor Documentation

GLSLProgram::GLSLProgram (  )

Constructor.

GLSLProgram::~GLSLProgram (  )

Destructor. Calls deleteProgram().


Member Function Documentation

virtual const char* vl::GLSLProgram::className (  ) [inline, virtual]

Returns the name of the class.

Reimplemented from vl::RenderState.

virtual ERenderState vl::GLSLProgram::type (  ) const [inline, virtual]

Reimplemented from vl::RenderState.

void GLSLProgram::createProgram (  )

Calls glCreateProgram() in order to acquire a GLSL program handle, see also http://www.opengl.org/sdk/docs/man/xhtml/glCreateProgram.xml for more information.

Note:
The program is created only if handle() == 0
void GLSLProgram::deleteProgram (  )

Deletes the GLSL program calling glDeleteProgram(handle()), see also http://www.opengl.org/sdk/docs/man/xhtml/glDeleteProgram.xml for more information.

After this function handle() will return 0.

unsigned int vl::GLSLProgram::handle (  ) const [inline]

The handle of the GLSL program as returned by glCreateProgram()

See also:
http://www.opengl.org/sdk/docs/man/xhtml/glCreateProgram.xml
bool GLSLProgram::useProgram (  ) const

Equivalent to glUseProgram(handle()), see also http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml for more information.

void GLSLProgram::apply ( const Camera ,
OpenGLContext ctx 
) const [virtual]

Calls useProgram()

Implements vl::RenderState.

bool GLSLProgram::linkProgram ( bool  force_relink = false )

Links the GLSLProgram calling glLinkProgram(handle()) only if the program needs to be linked.

See also:
bool GLSLProgram::linkStatus (  ) const
bool vl::GLSLProgram::linked (  ) const [inline]

Returns true if the program has been succesfully linked.

void vl::GLSLProgram::scheduleRelinking (  ) [inline]

Schedules a relink of the GLSL program.

bool GLSLProgram::attachShader ( GLSLShader shader )

Attaches the GLSLShader to this GLSLProgram.

Note:
Attaching a shader triggers the compilation of the shader (if not already compiled) and relinking of the program.
bool GLSLProgram::detachShader ( GLSLShader shader )

Removes a GLSLShader from the GLSLShader and schedules a relink of the program, see also http://www.opengl.org/sdk/docs/man/xhtml/glDetachShader.xml for more information.

String GLSLProgram::infoLog (  ) const

Returns the info log of this GLSL program using the OpenGL function glGetProgramInfoLog(), see also http://www.opengl.org/sdk/docs/man/xhtml/glGetProgramInfoLog.xml for more information.

bool GLSLProgram::validateProgram (  ) const

Returns true if the validation of this GLSL program is succesful, see also http://www.opengl.org/sdk/docs/man/xhtml/glValidateProgram.xml for more information.

void GLSLProgram::bindAttribLocation ( unsigned int  index,
const std::string &  name 
)

Equivalent to glBindAttribLocation(handle(), index, name.c_str()) with the difference that this function will automatically create a GLSL program if none is present and it will schedule a re-link since the new specified bindings take effect after linking the GLSL program.

See also:
setAutomaticAttribLocations(), automaticAttribLocations(), clearAutomaticAttribLocations(), removeAutomaticAttribLocation(), addAutomaticAttribLocation()
void vl::GLSLProgram::addAutomaticAttribLocation ( const char *  attr_name,
int  attr_index 
) [inline]

Adds an attribute name / index pair to the automatic attribute location binding list.

Calling this function will schedule a re-linking of the GLSL program.

See also:
setAutomaticAttribLocations(), automaticAttribLocations(), clearAutomaticAttribLocations(), bindAttribLocation(), removeAutomaticAttribLocation()
void vl::GLSLProgram::removeAutomaticAttribLocation ( const char *  attr_name ) [inline]

Removes an attribute from the automatic attribute location binding list.

Calling this function will schedule a re-linking of the GLSL program.

See also:
setAutomaticAttribLocations(), automaticAttribLocations(), clearAutomaticAttribLocations(), bindAttribLocation(), addAutomaticAttribLocation()
void vl::GLSLProgram::setAutomaticAttribLocations ( const std::map< std::string, int > &  attrib_bindings ) [inline]

Defines which attribute should be automatically bound to which attribute index at GLSL program linking time.

Calling this function will schedule a re-linking of the GLSL program.

See also:
automaticAttribLocations(), clearAutomaticAttribLocations(), bindAttribLocation(), removeAutomaticAttribLocation(), addAutomaticAttribLocation()
const std::map<std::string, int>& vl::GLSLProgram::automaticAttribLocations (  ) const [inline]

Returns which attribute name should be automatically bound to which attribute index at GLSL program linking time.

See also:
setAutomaticAttribLocations(), clearAutomaticAttribLocations(), bindAttribLocation(), removeAutomaticAttribLocation(), addAutomaticAttribLocation()
void vl::GLSLProgram::clearAutomaticAttribLocations (  ) [inline]
int vl::GLSLProgram::getAttribLocation ( const char *  name ) const [inline]

Eqivalento to glGetAttribLocation(handle(), name).

Note:
The program must be linked before calling this function.
int GLSLProgram::maxVertexAttributes (  ) [static]

Equivalent to glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max )

int vl::GLSLProgram::shaderCount (  ) const [inline]

Returns the number of GLSLShader objects bound to this GLSLProgram.

const GLSLShader* vl::GLSLProgram::shader ( int  i ) const [inline]

Returns the i-th GLSLShader objects bound to this GLSLProgram.

GLSLShader* vl::GLSLProgram::shader ( int  i ) [inline]

Returns the i-th GLSLShader objects bound to this GLSLProgram.

void GLSLProgram::detachAllShaders (  )

Removes all the previously linked shaders and schedules a relinking.

void GLSLProgram::bindFragDataLocation ( int  color_number,
const std::string &  name 
)
void GLSLProgram::unbindFragDataLocation ( const std::string &  name )
int GLSLProgram::fragDataLocationBinding ( const std::string &  name ) const
void vl::GLSLProgram::setGeometryVerticesOut ( int  vertex_count ) [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_VERTICES_OUT_EXT.

int vl::GLSLProgram::geometryVerticesOut (  ) const [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_VERTICES_OUT_EXT.

void vl::GLSLProgram::setGeometryInputType ( EGeometryInputType  type ) [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_INPUT_TYPE_EXT.

EGeometryInputType vl::GLSLProgram::geometryInputType (  ) const [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_INPUT_TYPE_EXT.

void vl::GLSLProgram::setGeometryOutputType ( EGeometryOutputType  type ) [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_OUTPUT_TYPE_EXT.

EGeometryOutputType vl::GLSLProgram::geometryOutputType (  ) const [inline]

See GL_ARB_geometry_shader4's GL_GEOMETRY_OUTPUT_TYPE_EXT.

bool GLSLProgram::applyUniformSet ( const UniformSet uniforms ) const

Applies a set of uniforms to the currently bound GLSL program.

This function expects the GLSLProgram to be already bound, see useProgram().

int vl::GLSLProgram::getUniformLocation ( const std::string &  name ) const [inline]

Returns the binding index of the uniform with the given name.

void vl::GLSLProgram::getUniformfv ( int  location,
float *  params 
) const [inline]

Equivalent to glGetUniformfv(handle(), location, params)

void vl::GLSLProgram::getUniformfv ( const std::string &  name,
float *  params 
) const [inline]

Equivalent to getUniformfv(getUniformLocation(name), params)

void vl::GLSLProgram::getUniformiv ( int  location,
int *  params 
) const [inline]

Equivalent to glGetUniformiv(handle(), location, params)

void vl::GLSLProgram::getUniformiv ( const std::string &  name,
int *  params 
) const [inline]

Equivalent to getUniformiv(getUniformLocation(name)

void vl::GLSLProgram::getUniform ( int  location,
fvec2 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
fvec3 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
fvec4 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
fmat2 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
fmat3 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
fmat4 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
ivec2 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
ivec3 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( int  location,
ivec4 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fvec2 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fvec3 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fvec4 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fmat2 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fmat3 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
fmat4 mat 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
ivec2 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
ivec3 vec 
) const [inline]
void vl::GLSLProgram::getUniform ( const std::string &  name,
ivec4 vec 
) const [inline]
UniformSet* vl::GLSLProgram::uniformSet (  ) [inline]

Returns a GLSLProgram's static UniformSet. Static uniforms are those uniforms whose value is constant across one rendering as opposed to Shader uniforms that change across Shaders and Actor uniforms that change across Actors.

const UniformSet* vl::GLSLProgram::uniformSet (  ) const [inline]

Returns a GLSLProgram's static UniformSet. Static uniforms are those uniforms whose value is constant across one rendering as opposed to Shader uniforms that change across Shaders and Actor uniforms that change across Actors.

void vl::GLSLProgram::setUniformSet ( UniformSet uniforms ) [inline]

Sets a GLSLProgram's static UniformSet.

void vl::GLSLProgram::setUniform ( Uniform uniform ) [inline]

Utility function using uniformSet(). Adds a Uniform to this program's static uniform set.

Uniform* vl::GLSLProgram::getUniform ( const std::string &  name ) [inline]

Utility function using uniformSet(). Returns the specified Uniform. Returns NULL if there isn't such a Uniform.

Uniform* vl::GLSLProgram::gocUniform ( const std::string &  name ) [inline]

Utility function using uniformSet(). Gets or creates the specified Uniform.

void vl::GLSLProgram::eraseUniform ( const std::string &  name ) [inline]

Utility function using uniformSet(). Erases the specified uniform.

void vl::GLSLProgram::eraseUniform ( const Uniform uniform ) [inline]

Utility function using uniformSet(). Erases the specified uniform.

void vl::GLSLProgram::eraseAllUniforms (  ) [inline]

Utility function using uniformSet(). Erases all the uniforms.


Friends And Related Function Documentation

friend class Renderer [friend]

Member Data Documentation

std::vector< ref<GLSLShader> > vl::GLSLProgram::mShaders [protected]
std::map<std::string, int> vl::GLSLProgram::mFragDataLocation [protected]
std::map<std::string, int> vl::GLSLProgram::mUniformLocation [protected]
std::map<std::string, int> vl::GLSLProgram::mAttribLocation [protected]
unsigned int vl::GLSLProgram::mHandle [protected]

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

Visualization Library v2010.11.1123 Reference Documentation
Copyright 2005-2009 Michele Bosi. All rights reserved.
Updated on Thu Nov 18 2010 02:08:03.
Permission is granted to use this page to write and publish articles regarding Visualization Library.