Graphics / Reference / Uniform variables

Uniform variables

Uniform variables are program-wide shader inputs shared across the vertices or fragments processed by a draw. They are located by name from the linked program and set from application code.

Uniform locations are obtained from the linked shader program by name:

locProj = glGetUniformLocation(program,"uProj"); locModel = glGetUniformLocation(program,"uModel"); locColor = glGetUniformLocation(program,"uColor");

The uniforms must be declared by the shader stages that use them and remain active in the linked program to have usable locations. A uniform value is shared by the shader invocations that use it during a draw unless the application changes that value. Here uProj and uModel are consumed by the vertex shader, while uColor is consumed by the fragment shader.

For the 2D setup used here:

proj = glm::ortho(0,fbWidth,0,fbHeight,-1,1); model = glm::mat4(1.0);

glUniformMatrix4fv(locProj,1,GL_FALSE,glm::value_ptr(proj)); glUniformMatrix4fv(locModel,1,GL_FALSE,glm::value_ptr(model));

uProj is an orthographic projection spanning the framebuffer dimensions, and uModel is initially the identity matrix. The model matrix acts on the source position first; the projection then produces the clip-space position written by the vertex shader to gl_Position. Later pipeline steps perform the transformations from clip space toward the final framebuffer location. uColor can be changed between draw calls to give different primitives different fragment-shader output colours.

Builds on

Read