UniformsGL 2.0

glUniform2f

Sets a vec2 uniform of the program in use from two floats, such as the screen resolution.

Part of the family glUniform*

Signature

void glUniform2f(GLint location, GLfloat v0, GLfloat v1);

How it works

The name says what it takes: 2 values of type f (float), so it fits a uniform declared vec2. The same pattern names the whole family: glUniform1i (one int), glUniform3f (vec3), glUniform4fv (an array of vec4 by pointer), glUniformMatrix4fv (mat4).

The classic use is uResolution in a fragment shader, so gl_FragCoord.xy / uResolution gives coordinates from 0 to 1 whatever the window size.

Parameters

locationGLint

From glGetUniformLocation; -1 is silently ignored.

v0GLfloat

The x component.

v1GLfloat

The y component.

Tips & pitfalls

  • It writes to the program bound with glUseProgram, so call glUseProgram first (or use glProgramUniform2f).

  • The suffix must match the declaration: glUniform2f on an ivec2 or a vec3 raises GL_INVALID_OPERATION and changes nothing.

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 uniform is not a vec2.

Example

example.cpp
1glUseProgram(prog);
2glUniform2f(glGetUniformLocation(prog, "uResolution"), (float)width, (float)height);
Official Khronos documentation