Graphics Reference
In-Depth Information
If i has the value 2, val must be a 2 × 2
matrix; if 3, a 3 × 3 matrix; and if 4, a 4 x 4
matrix. If transpose has value GL_FALSE ,
the matrix is taken to be in standard
OpenGL column major order, while if
transpose has value GL_TRUE , the matrix
is taken to be in row-major order. The
value of count is the number of matrices
that are being passed, so if you are only
passing a single matrix, that value is 1.
In Chapter 7 we talked about how it would sometimes be nice to be able
to separate the Model and the Viewing matrices, instead of having them pre-
combined into one ModelView matrix, as OpenGL does. If you are willing to
manipulate the contents of those matrices yourself, then using matrix uniform
variables is a good way to accomplish this. 1
If you have defined a struct as a uniform variable, you cannot set the entire
struct at once; you must use the functions above to set each field individually.
As an example, let's suppose that you wanted to pass a light location into
your shaders. The following very short code fragment, to be used in your appli-
cation, stores a Cfloat[3] variable named lightLoc in an application-defined
uniform vec3 variable whose name is “ uLightLocation ”. Note the use of the
glGetUniformLocation function to find the location of the uniform variable
and of the glProgramUniform3fv function to set that uniform variable, as well
as the check to ensure that the variable was actually found.
Notice that none of the glUniform*
routines take a program handle as
one of its arguments. Those routines
set uniform variables in the currently
active shader program. So, be sure
that you call glUseProgram( ) on
the correct program before setting
that program's variables.
// in the shader:
uniform vec3 uLightLocation;
// in the C / C++ application (after linking
// the shader program):
float lightLoc[3] = { 0., 100., 0. };
GLint location = glGetUniformLocation( program,
“uLightLocation” );
if( location < 0 )
fprintf(stderr, “Uniform variable 'uLightLocation' not
found\n”);
else
glProgramUniform3fv( program, location, 3, lightLoc );
1. Appendix B shows a C++ class that allows you to easily manipulate your own matrices.
Search WWH ::




Custom Search