UniformsGL 2.0
glUniform1i
Sets an int, bool or sampler uniform — e.g. tells a sampler which texture unit to read.
Part of the family glUniform*
Signature
void glUniform1i(GLint location, GLint v0);
How it works
The suffix of glUniform* encodes the type: 1i = one int. Besides plain ints and bools, this is how samplers are connected: a sampler2D uniform holds the NUMBER of the texture unit it reads from, so glUniform1i(loc, 2) means "read from GL_TEXTURE2".
Parameters
locationGLint
Uniform location from glGetUniformLocation. -1 is silently ignored.
v0GLint
The value. For samplers: the texture unit index (0, 1, 2…), NOT the texture ID.
Tips & pitfalls
Uniforms are written to the program currently selected with
glUseProgram.Passing the texture ID instead of the unit number is a very common bug.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_OPERATION
- No program in use, or the function's type/size does not match the uniform declared in GLSL.
Example
example.cpp
1glUseProgram(program);
2glUniform1i(glGetUniformLocation(program, "diffuseMap"), 0); // GL_TEXTURE0
3glUniform1i(glGetUniformLocation(program, "specularMap"), 1); // GL_TEXTURE1