1
\$\begingroup\$

I have a base application where a model is loaded in, however I need to manipulate the models positions and rotations independently so that each model in the scene can have its own rotation and position.

At the moment, I have only one object and I am trying to combine the rotation, scale and translation matrices to form the model's matrix but for some reason the model is not rendered.

glm::mat4 rotMatrix = eulerAngleXYZ(orientation.x, orientation.y, orientation.z);
glm::mat4 scaleMatrix = scale(mat4(), vec3(1.0f, 1.0f, 1.0f));
glm::mat4 transMatrix = translate(mat4(), vec3(1.0f, 1.0f, 1.0f));

glm::mat4 modelMatrix = transMatrix * rotMatrix * scaleMatrix;

glm::mat4 mvp = camera->getCameraMatrix() * modelMatrix;

glUniformMatrix4fv(matrixId, 1, GL_FALSE, &mvp[0][0]);
glUniformMatrix4fv(modelMatrixId, 1, GL_FALSE, &modelMatrix[0][0]);

When using the above code, I don't see anything on the screen, when I'd expect to see the object at position (1.0f, 1.0f, 1.0f) as that is the translation.

When I set the model matrix to glm::mat4(1.0f), I can then see the model correctly but it is placed at the origin and I cannot move it.

What am I doing wrong?

\$\endgroup\$
4
  • \$\begingroup\$ which version of glm are you using? \$\endgroup\$
    – user100681
    Mar 8, 2018 at 18:25
  • \$\begingroup\$ @GabrieleVierti I believe it's 0.9.7 \$\endgroup\$
    – jjmcc
    Mar 8, 2018 at 18:28
  • \$\begingroup\$ Since glm 0.9.9,matrices are initializated to 0-initalized matrices, instead of identity once. From that version it is required to initialize matrix types as: glm::mat4 mat = glm::mat4(1.0f). Have you tried creating a model matrix, and then doing something like : glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model,glm::vec3(0.0f,0.0f,0.0f)); model = glm::rotate(glm::radians(0.0f),glm::vec3(1,0,0)); [...] ? This way you just use ONE matrix and not 3 \$\endgroup\$
    – user100681
    Mar 8, 2018 at 18:32
  • \$\begingroup\$ @GabrieleVierti Oh that seemed to work, thanks. You can post an answer if you'd like \$\endgroup\$
    – jjmcc
    Mar 8, 2018 at 18:39

1 Answer 1

4
\$\begingroup\$

The problem is that you are multiplyng 3 different matrices, and putting the result into a fourth matrix, and then setting that into the shader.

What you need to do instead is create a single 4x4 matrix, modify it, and then pass that into the shader. Here is a simple example, where we create a model matrix, and set it to default values:

glm::mat4 model = glm::mat4(1.0f);

model = glm::translate(model,glm::vec3(0,0,0)); //position = 0,0,0
model = glm::rotate(model,glm::radians(0.0f),glm::vec3(1,0,0));//rotation x = 0.0 degrees
model = glm::rotate(model,glm::radians(0.0f),glm::vec3(0,1,0));//rotation y = 0.0 degrees
model = glm::rotate(model,glm::radians(0.0f),glm::vec3(0,0,1));//rotation z = 0.0 degrees
model = glm::scale(model,glm::vec3(1,1,1);//scale = 1,1,1
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .