MinGfx Toolkit
1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
|
A wrapper around GLSL shader programs.
This class supports loading vertex and fragment shaders from files or strings, compiling them, and then linking them into a shader program. Uniform variables within the shader programs can be set in order to pass parameters from C++ code into the shader program. Textures can also be bound to the shader. Example usage:
Definition at line 66 of file shader_program.h.
#include <shader_program.h>
Public Member Functions | |
ShaderProgram () | |
Creates an empty ShaderProgram object. More... | |
virtual | ~ShaderProgram () |
bool | AddVertexShaderFromFile (const std::string &file) |
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL()). This loads the shader from the file and compiles it. An error will be printed to stderr if there are any compilation errors. More... | |
bool | AddVertexShaderFromSource (const std::string &code) |
This loads and compiles a shader from a string. An error will be printed to stderr if there are any compilation errors. More... | |
bool | AddFragmentShaderFromFile (const std::string &file) |
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL()). This loads the shader from the file and compiles it. An error will be printed to stderr if there are any compilation errors. More... | |
bool | AddFragmentShaderFromSource (const std::string &code) |
This loads and compiles a shader from a string. An error will be printed to stderr if there are any compilation errors. More... | |
bool | LinkProgram () |
Call this after adding vertex and fragment shaders in order to link them together to create the full shader program. An error will be printed to stderr if there are any linking errors. More... | |
void | UseProgram () |
Call this first to make the shader program active, then call SetUniform() to pass data from your C++ program into the shader code via the named uniform variables that appear in the code. Then render whatever geometry you wish with your own glDrawArrays() call(s). Finally, call StopProgram() to turn off the shader program. More... | |
void | SetUniform (const std::string &name, const Point2 &p) |
Passes the x,y values of point p to the shader program and stores the result in the shader variable named name, which should be of type vec2. More... | |
void | SetUniform (const std::string &name, const Vector2 &v) |
Passes the x,y values of vector v to the shader program and stores the result in the shader variable named name, which should be of type vec2. More... | |
void | SetUniform (const std::string &name, const Point3 &p) |
Passes the x,y,z,1 values of point p to the shader program and stores the result in the shader variable named name, which should be of type vec4. More... | |
void | SetUniform (const std::string &name, const Vector3 &v) |
Passes the x,y,z,0 values of vector v to the shader program and stores the result in the shader variable named name, which should be of type vec4. More... | |
void | SetUniform (const std::string &name, const Matrix4 &m) |
Passes the column-major 16 float values of matrix m to the shader program and stores the result in the shader variable named name, which should be of type mat4. More... | |
void | SetUniform (const std::string &name, const Color &c) |
Passes the r,g,b,a values of color c to the shader program and stores the result in the shader variable named name, which should be of type vec4. More... | |
void | SetUniform (const std::string &name, int i) |
Passes the int to the shader program and stores the result in the shader variable named name, which should be of type int. More... | |
void | SetUniform (const std::string &name, unsigned int ui) |
Passes the unsigned int to the shader program and stores the result in the shader variable named name, which should be of type uint. More... | |
void | SetUniform (const std::string &name, float f) |
Passes the float to the shader program and stores the result in the shader variable named name, which should be of type float. More... | |
void | SetUniformArray1 (const std::string &name, int *i, int count) |
Passes an array of count ints to the shader program and stores the result in the shader variable named name, which should be of type int name[count]. More... | |
void | SetUniformArray1 (const std::string &name, unsigned int *ui, int count) |
Passes an array of count unsigned ints to the shader program and stores the result in the shader variable named name, which should be of type uint name[count]. More... | |
void | SetUniformArray1 (const std::string &name, float *f, int count) |
Passes an array of count floats to the shader program and stores the result in the shader variable named name, which should be of type float name[count]. More... | |
void | SetUniformArray2 (const std::string &name, int *i, int count) |
Passes an array of count 2D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec2 name[count]. More... | |
void | SetUniformArray2 (const std::string &name, unsigned int *ui, int count) |
Passes an array of count 2D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec2 name[count]. More... | |
void | SetUniformArray2 (const std::string &name, float *f, int count) |
Passes an array of count 2D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec2 name[count]. More... | |
void | SetUniformArray3 (const std::string &name, int *i, int count) |
Passes an array of count 3D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec3 name[count]. More... | |
void | SetUniformArray3 (const std::string &name, unsigned int *ui, int count) |
Passes an array of count 3D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec3 name[count]. More... | |
void | SetUniformArray3 (const std::string &name, float *f, int count) |
Passes an array of count 3D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec3 name[count]. More... | |
void | SetUniformArray4 (const std::string &name, int *i, int count) |
Passes an array of count 4D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec4 name[count]. More... | |
void | SetUniformArray4 (const std::string &name, unsigned int *ui, int count) |
Passes an array of count 4D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec4 name[count]. More... | |
void | SetUniformArray4 (const std::string &name, float *f, int count) |
Passes an array of count 4D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec4 name[count]. More... | |
void | BindTexture (const std::string &name, const Texture2D &tex) |
Binds a Texture2D to a sampler2D in the shader program. This version automatically selects an available texture unit, i.e., one not already used by this shader program. More... | |
void | BindTexture (const std::string &name, const Texture2D &tex, int texUnit) |
Binds a Texture2D to a sampler2D in the shader program. This version allows you to specify the texture unit to use. More... | |
void | StopProgram () |
Call this after rendering geometry to deactivate the shader. More... | |
bool | initialized () |
Returns true if the shader program has been successfully compiled and linked. More... | |
mingfx::ShaderProgram::ShaderProgram | ( | ) |
Creates an empty ShaderProgram object.
|
virtual |
bool mingfx::ShaderProgram::AddFragmentShaderFromFile | ( | const std::string & | file | ) |
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL()). This loads the shader from the file and compiles it. An error will be printed to stderr if there are any compilation errors.
bool mingfx::ShaderProgram::AddFragmentShaderFromSource | ( | const std::string & | code | ) |
This loads and compiles a shader from a string. An error will be printed to stderr if there are any compilation errors.
bool mingfx::ShaderProgram::AddVertexShaderFromFile | ( | const std::string & | file | ) |
Call during initialization but after the OpenGL context has been created (e.g., inside InitOpenGL()). This loads the shader from the file and compiles it. An error will be printed to stderr if there are any compilation errors.
bool mingfx::ShaderProgram::AddVertexShaderFromSource | ( | const std::string & | code | ) |
This loads and compiles a shader from a string. An error will be printed to stderr if there are any compilation errors.
void mingfx::ShaderProgram::BindTexture | ( | const std::string & | name, |
const Texture2D & | tex | ||
) |
Binds a Texture2D to a sampler2D in the shader program. This version automatically selects an available texture unit, i.e., one not already used by this shader program.
void mingfx::ShaderProgram::BindTexture | ( | const std::string & | name, |
const Texture2D & | tex, | ||
int | texUnit | ||
) |
Binds a Texture2D to a sampler2D in the shader program. This version allows you to specify the texture unit to use.
bool mingfx::ShaderProgram::initialized | ( | ) |
Returns true if the shader program has been successfully compiled and linked.
bool mingfx::ShaderProgram::LinkProgram | ( | ) |
Call this after adding vertex and fragment shaders in order to link them together to create the full shader program. An error will be printed to stderr if there are any linking errors.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Color & | c | ||
) |
Passes the r,g,b,a values of color c to the shader program and stores the result in the shader variable named name, which should be of type vec4.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Matrix4 & | m | ||
) |
Passes the column-major 16 float values of matrix m to the shader program and stores the result in the shader variable named name, which should be of type mat4.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Point2 & | p | ||
) |
Passes the x,y values of point p to the shader program and stores the result in the shader variable named name, which should be of type vec2.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Point3 & | p | ||
) |
Passes the x,y,z,1 values of point p to the shader program and stores the result in the shader variable named name, which should be of type vec4.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Vector2 & | v | ||
) |
Passes the x,y values of vector v to the shader program and stores the result in the shader variable named name, which should be of type vec2.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
const Vector3 & | v | ||
) |
Passes the x,y,z,0 values of vector v to the shader program and stores the result in the shader variable named name, which should be of type vec4.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
float | f | ||
) |
Passes the float to the shader program and stores the result in the shader variable named name, which should be of type float.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
int | i | ||
) |
Passes the int to the shader program and stores the result in the shader variable named name, which should be of type int.
void mingfx::ShaderProgram::SetUniform | ( | const std::string & | name, |
unsigned int | ui | ||
) |
Passes the unsigned int to the shader program and stores the result in the shader variable named name, which should be of type uint.
void mingfx::ShaderProgram::SetUniformArray1 | ( | const std::string & | name, |
float * | f, | ||
int | count | ||
) |
Passes an array of count floats to the shader program and stores the result in the shader variable named name, which should be of type float name[count].
void mingfx::ShaderProgram::SetUniformArray1 | ( | const std::string & | name, |
int * | i, | ||
int | count | ||
) |
Passes an array of count ints to the shader program and stores the result in the shader variable named name, which should be of type int name[count].
void mingfx::ShaderProgram::SetUniformArray1 | ( | const std::string & | name, |
unsigned int * | ui, | ||
int | count | ||
) |
Passes an array of count unsigned ints to the shader program and stores the result in the shader variable named name, which should be of type uint name[count].
void mingfx::ShaderProgram::SetUniformArray2 | ( | const std::string & | name, |
float * | f, | ||
int | count | ||
) |
Passes an array of count 2D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec2 name[count].
void mingfx::ShaderProgram::SetUniformArray2 | ( | const std::string & | name, |
int * | i, | ||
int | count | ||
) |
Passes an array of count 2D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec2 name[count].
void mingfx::ShaderProgram::SetUniformArray2 | ( | const std::string & | name, |
unsigned int * | ui, | ||
int | count | ||
) |
Passes an array of count 2D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec2 name[count].
void mingfx::ShaderProgram::SetUniformArray3 | ( | const std::string & | name, |
float * | f, | ||
int | count | ||
) |
Passes an array of count 3D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec3 name[count].
void mingfx::ShaderProgram::SetUniformArray3 | ( | const std::string & | name, |
int * | i, | ||
int | count | ||
) |
Passes an array of count 3D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec3 name[count].
void mingfx::ShaderProgram::SetUniformArray3 | ( | const std::string & | name, |
unsigned int * | ui, | ||
int | count | ||
) |
Passes an array of count 3D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec3 name[count].
void mingfx::ShaderProgram::SetUniformArray4 | ( | const std::string & | name, |
float * | f, | ||
int | count | ||
) |
Passes an array of count 4D float arrays to the shader program and stores the result in the shader variable named name, which should be of type vec4 name[count].
void mingfx::ShaderProgram::SetUniformArray4 | ( | const std::string & | name, |
int * | i, | ||
int | count | ||
) |
Passes an array of count 4D int arrays to the shader program and stores the result in the shader variable named name, which should be of type ivec4 name[count].
void mingfx::ShaderProgram::SetUniformArray4 | ( | const std::string & | name, |
unsigned int * | ui, | ||
int | count | ||
) |
Passes an array of count 4D unsigned int arrays to the shader program and stores the result in the shader variable named name, which should be of type uivec4 name[count].
void mingfx::ShaderProgram::StopProgram | ( | ) |
Call this after rendering geometry to deactivate the shader.
void mingfx::ShaderProgram::UseProgram | ( | ) |
Call this first to make the shader program active, then call SetUniform() to pass data from your C++ program into the shader code via the named uniform variables that appear in the code. Then render whatever geometry you wish with your own glDrawArrays() call(s). Finally, call StopProgram() to turn off the shader program.