MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
Public Member Functions | List of all members
mingfx::ShaderProgram Class Reference

Detailed Description

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:

ShaderProgram shader_prog;
void MyGraphicsApp::InitOpenGL() {
shader_prog.AddVertexShaderFromFile(Platform::findFile("my_shader.vert", searchPath));
shader_prog.AddFragmentShaderFromFile(Platform::findFile("my_shader.frag", searchPath));
shader_prog.LinkProgram();
}
void MyGraphicsApp::DrawUsingOpenGL() {
// Activate the shader program
shader_prog.UseProgram();
// Pass uniforms and textures from C++ to the GPU Shader Program
shader_prog.SetUniform("ModelMatrix", modelMat);
shader_prog.SetUniform("ViewMatrix", viewMat);
shader_prog.SetUniform("ProjectionMatrix", projMat);
shader_prog.SetUniform("LightPosition", Point3(2,2,2));
shader_prog.BindTexture("SurfaceTexture", my_tex);
// Draw whatever geometry you want now
mesh1.Draw();
mesh2.Draw();
// Deactivate the shader program
shader_prog.StopProgram();
}
ShaderProgram()
Creates an empty ShaderProgram object.

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...
 

Constructor & Destructor Documentation

◆ ShaderProgram()

mingfx::ShaderProgram::ShaderProgram ( )

Creates an empty ShaderProgram object.

◆ ~ShaderProgram()

virtual mingfx::ShaderProgram::~ShaderProgram ( )
virtual

Member Function Documentation

◆ AddFragmentShaderFromFile()

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.

◆ AddFragmentShaderFromSource()

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.

◆ AddVertexShaderFromFile()

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.

◆ AddVertexShaderFromSource()

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.

◆ BindTexture() [1/2]

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.

◆ BindTexture() [2/2]

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.

◆ initialized()

bool mingfx::ShaderProgram::initialized ( )

Returns true if the shader program has been successfully compiled and linked.

◆ LinkProgram()

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.

◆ SetUniform() [1/9]

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.

◆ SetUniform() [2/9]

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.

◆ SetUniform() [3/9]

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.

◆ SetUniform() [4/9]

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.

◆ SetUniform() [5/9]

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.

◆ SetUniform() [6/9]

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.

◆ SetUniform() [7/9]

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.

◆ SetUniform() [8/9]

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.

◆ SetUniform() [9/9]

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.

◆ SetUniformArray1() [1/3]

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].

◆ SetUniformArray1() [2/3]

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].

◆ SetUniformArray1() [3/3]

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].

◆ SetUniformArray2() [1/3]

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].

◆ SetUniformArray2() [2/3]

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].

◆ SetUniformArray2() [3/3]

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].

◆ SetUniformArray3() [1/3]

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].

◆ SetUniformArray3() [2/3]

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].

◆ SetUniformArray3() [3/3]

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].

◆ SetUniformArray4() [1/3]

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].

◆ SetUniformArray4() [2/3]

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].

◆ SetUniformArray4() [3/3]

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].

◆ StopProgram()

void mingfx::ShaderProgram::StopProgram ( )

Call this after rendering geometry to deactivate the shader.

◆ UseProgram()

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.


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