Function familiesfamily

glUniform*

Set a uniform of the program in use; the end of the name says how many values and of what type.

Signature

void glUniform{1|2|3|4}{f|i|ui}[v](GLint location, …);
void glUniformMatrix{2|3|4}fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);

How it works

There is one glUniform function per shape of data, and the name spells the shape: a number (how many components), a letter (their type) and an optional v (passed through a pointer). Pick the one that matches the uniform's declaration in GLSL: float → glUniform1f, vec3 → glUniform3f or glUniform3fv, int and sampler2D → glUniform1i, mat4 → glUniformMatrix4fv.

All of them write to the program bound with glUseProgram. A mismatch between function and declaration (glUniform1f on an int) is GL_INVALID_OPERATION and the value is not changed, which is a frequent reason for a uniform that "does nothing".

Reading the name

1 2 3 4
Number of components: 1 = float/int, 3 = vec3, 4 = vec4.
f
float (GLSL float, vec2, vec3, vec4).
i
int (int, ivec*, bool, and every sampler).
ui
unsigned int (uint, uvec*).
v
"vector": the values come through a pointer, with a count, so arrays can be set in one call.
Matrix4fv
A matrix (always by pointer, always float): Matrix2fv, Matrix3fv, Matrix4fv, also Matrix3x4fv and similar.

Functions in this family

Example

example.cpp
1glUseProgram(prog);
2glUniform1f(glGetUniformLocation(prog, "uTime"), t);                  // float
3glUniform3f(glGetUniformLocation(prog, "uColor"), 1.0f, 0.5f, 0.2f);  // vec3
4glUniform1i(glGetUniformLocation(prog, "uTexture"), 0);               // sampler2D -> unit 0
5glUniformMatrix4fv(glGetUniformLocation(prog, "uModel"), 1, GL_FALSE, glm::value_ptr(model));