Shader Lib Specification
| Prev: In Action | Next: Header |
Enumeration for attribute types
attributes can be added as required.
enum AttribType {
VERTEX_COORD_ATTRIB,
NORMAL_ATTRIB,
TEXTURE_COORD_ATTRIB
TANGENT_ATTRIB,
BITANGENT_ATTRIB
};
Enumeration for shader types
enum ShaderType {
VERTEX_SHADER,
GEOMETRY_SHADER,
TESS_CONTROL_SHADER,
TESS_EVAL_SHADER,
FRAGMENT_SHADER,
COUNT_SHADER_TYPE
}
Functions
void init()
Call this function first for each instance. This should only be created after an OpenGL context is created.
Example
VSShaderLib myShader; myShader.init();
void loadShader( VSShaderLib::ShaderType st, std::string fileName)
Given a file name, load its text as the source of the shader, attaches it to the program and compiles it. The info log for the shader can be retrieved with getShaderInfoLog and the compile status can be checked with isShaderCompiled.
Example
shader.loadShader( VSShaderLib::VERTEX_SHADER, "shaders/dirlightdiffambpix.vert");
void setProgramOutput(int index, std::string name)
Binds fragment output location of variable name to index.
Example
shader.setProgramOutput(0,"outputF");
GLint getProgramOutput(std::string name)
Returns the fragment output location of fragment output variable name
Example
fragLoc = shader.getProgramOutput("outputF");
void setVertexAttribName( VSShaderLib::AttribType at, std::string name)
Defines the semantic for the attribute named name. Binds the attribute to the location specified by at.
Example
shader.setVertexAttribName( VSShaderLib::VERTEX_COORD_ATTRIB, "position");
void prepareProgram()
Links the program and discovers all uniforms, including uniform blocks. The info log for the program can be retrieved with getProgramInfoLog. We can also check if the program linked successfuly, and if it is a valid program, with isProgramLinked and isProgramValid respectively.
Example
shader.prepareProgram();
void setUniform(std::string name, void *value) void setUniform(std::string name, int value) void setUniform(std::string name, float value)
Sets the uniform name to value. The value can be a float, int, or a pointer to a float, int, float array, or int array.
Example
shader.setUniform("texUnit", 0);
float aVec[4] = {0.0f, 1.0f, 2.0f, 3.0f};
shader.setUniform("myVec", aVec);
static void setBlock(std::string name, void *value)
Block information is stored in static variables. The idea is that a block can be used in many different shaders, hence, two blocks with the same name in two different shaders are treated as the same block. Therefore, to set a block, no instance is required.
Use GL query commands to check for strides on the uniforms inside a block. Make sure that the data structure has the same length as the uniform block, and that every piece of data has the same alignment.
Example
VSShaderLib::setBlock("Matrices", matStruct);
static void setBlockUniform(std::string blockName, std::string uniformName, void *value)
Sets a uniform inside a named block. Take notice of the strides used in matrices and arrays. Use GL queries to make sure data is properly aligned.
Example:
VSShaderLib::setBlockUniform("Matrices", "NormalMatrix", mat);
static void setBlockUniformArrayElement( std::string blockName, std::string uniformName, int arrayIndex, void *value)
Sets an array component uniform inside a block.
Example
VSShaderLib::setBlockUniformArrayElement( "Material", "ColorComponent", 2, ambient);
GLuint getProgramIndex() GLuint getShaderIndex()
Returns the program and shader index
Example
glUseProgram(shader.getProgram());
std::string getProgramInfoLog() std::string getShaderInfoLog(VSShaderLib::ShaderType) std::string getAllInfoLogs()
Returns the respective info logs. The last function returns all info logs in a single string.
bool isProgramValid() bool isProgramLinked() bool isShaderCompiled(VSShaderLib::ShaderType)
Returns the status of several components of an instance
| Prev: In Action | Next: Header |
