Often,
one would like for the OpenGL client to pass values to shaders. This can done by getting the memory location
of the variable in the shader after the shader has been linked. This can be achieved using:
GLint glGetUniformLocation(GLuint
programID, const char *name);
where programID is
the program ID and name is
the variable name. The
return value is the location of the variable, which can then be used to assign
values from the client to the shader.
Only communicate to shader programs
that are activated, by calling
glUseProgram() first.
A
uniform variable can be passed from the client to the shader using the
location. There
are a set of functions depending on whether the layout of the variable.
Uniform
(float below, replace “f” with “i” for integer):
- void glUniform1f(GLint location, GLfloat v0);
- void glUniform2f(GLint location, GLfloat v0, GLfloat
v1);
- void glUniform3f(GLint location, GLfloat v0, GLfloat
v1, GLfloat v2);
- void glUniform4f(GLint location, GLfloat v0, GLfloat
v1, GLfloat v2, GLfloat
v3);
- GLint glUniform{1,2,3,4}fv(GLint
location, GLsizei count, GLfloat *v);
- GLint glUniformMatrix{2,3,4}fv(GLint
location, GLsizei count, GLboolean transpose, GLfloat *v);