Back to index

Tutorial 7: Uniforms

Uniforms are variables that can be passed from a program running on the CPU, to a shader program running on the GPU.

More recent versions of OpenGL have deprecated the default matrix stack/operations, so instead the modelview matrix must be manually setup and passed to the vertex shader as a uniform variable.

Exercises:

In this tutorial:

Using uniform variables

Red sine wave

Uniform variables are passed to shaders using the appropriate glUniform function. Uniform variables can be passed as a single value, as a 2/3/4 component vector, or as a matrix.

With your code from last week, use the following vertex/fragment shaders (ignore lighting for now):

// shader.vert
void main(void)
{
    vec4 esVert = gl_Vertex;
    vec4 csVert = gl_ProjectionMatrix * esVert;
    gl_Position = csVert;
}


// shader.frag
uniform vec3 color;

void main(void)
{
    gl_FragColor = vec4(color, 1);
}

Here are shaders that show an example of this:

// shader.vert
uniform vec3 uColor;
varying vec3 vColor;

void main(void)
{
    vec4 esVert = gl_Vertex;
    vec4 csVert = gl_ProjectionMatrix * esVert;
    gl_Position = csVert;

    vColor = uColor;
}

// shader.frag
varying vec3 vColor;

void main(void)
{
    gl_FragColor = vec4(vColor, 1);
}

Passing modelview matrix as uniform

Now using the lighting shader from last week (or the color shader above), we will pass the modelview matrix to the vertex shader so that we can transform the vertex to eye space coordinates on the GPU instead of on the CPU.

Converting to eye space

Transforming vertices using matrices is typically much faster (and easier) to do on the GPU - due to its parallelism - than on the CPU, which is why converting vertices to eye space is usually done in the vertex shader rather than on the CPU in the application program.

Passing normal matrix

Just as the modelview matrix is used to transform vertices to eye space, the normal matrix is used to transform normals for lighting calculations. The normal matrix can be thought of as the modelview matrix, with translation information removed, and then corrected for any scaling applied. This can be calculated as the transposed inverse of the modelview matrix and is already being done and stored in a 3x3 matrix called normalMatrix in the application program.

Extra: Calculating sine wave (to be continued next week)

Extra: Projection Matrix (to be continued next week)

You can also pass to and use the projection matrix in the vertex shader in the same way as the modelview matrix. This will have the effect of transforming vertices from eye space to clip space. Currently the program calls glOrtho to set the projection matrix, which is used in the vertex shader as gl_ProjectionMatrix.