UniformsGL 2.0

glUniformMatrix4fv

Sets a mat4 uniform — how model, view and projection matrices reach the shader.

Part of the family glUniform*

Signature

void glUniformMatrix4fv(GLint location, GLsizei count,
                        GLboolean transpose, const GLfloat *value);

How it works

Uploads one or more 4×4 float matrices. OpenGL and GLM both store matrices column by column (column-major), so with GLM transpose is always GL_FALSE and you pass glm::value_ptr(m).

Parameters

locationGLint

Uniform location from glGetUniformLocation. -1 is silently ignored.

countGLsizei

Number of matrices. 1 for a single mat4; N for mat4 bones[N].

transposeGLboolean

GL_TRUE if your data is row-major. With GLM: GL_FALSE.

valueconst GLfloat *

Pointer to 16 × count floats.

Tips & pitfalls

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

  • Sending the same projection and view to many programs? Put them in a uniform buffer instead.

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
1glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
2glUniformMatrix4fv(uProjection, 1, GL_FALSE, glm::value_ptr(projection));
Official Khronos documentation