UniformsGL 2.0

glUniform4f

Sets a vec4 uniform from four separate floats.

Part of the family glUniform*

Signature

void glUniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);

How it works

Writes the four values into a vec4 uniform of the program in use; the typical case is a colour with alpha, uniform vec4 uColor. Changing it every frame from the CPU (a colour that pulses with time) is the first animation many people write.

The four values go in as written: for a colour they are red, green, blue and alpha from 0 to 1, not 0 to 255.

Parameters

locationGLint

Uniform location from glGetUniformLocation. -1 is silently ignored.

v0GLfloat

x / r component.

v1GLfloat

y / g component.

v2GLfloat

z / b component.

v3GLfloat

w / a component.

Tips & pitfalls

  • Uniforms are written to the program currently selected with glUseProgram.

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
1float green = (std::sin(glfwGetTime()) / 2.0f) + 0.5f;
2glUniform4f(uColor, 0.0f, green, 0.0f, 1.0f);
Official Khronos documentation