c++openglglslglm-mathuniform

How to pass an array of mat4 as a uniform


I have to pass an array of mat4 as a uniform to my vertex shader like this:

in the vertex shader:

uniform mat4 u_jointMatrix[2];

in my C++ program I did like this:

glm::mat4 jointM[2];
//I filled jointM with 32 values (16 x 2)
int jointMatrixLoc = glGetUniformLocation(programT, "u_jointMatrix");
glUniformMatrix4fv(jointMatrixLoc, 1, GL_FALSE, glm::value_ptr(jointM[2]));

I wrote this because glUniformMatrix4fv(jointMatrixLoc, 1, GL_FALSE, glm::value_ptr(jointM)); doesn’t work.

I think what I did is incorrect. can someone confirm it?


Solution

  • jointM[2] address the 3rd element in an array with 2 elements. You have to specify a pointer to the first element:

    glUniformMatrix4fv(jointMatrixLoc, 1, GL_FALSE, glm::value_ptr(jointM[2]));

    glUniformMatrix4fv(jointMatrixLoc, 2, GL_FALSE, glm::value_ptr(jointM[0]));