I am trying to render a 3D model using OpenGL. For the projection and transformation matrices, I am using glm. I've got my model on the screen and it works just like I intended it to; except one small problem.

I am setting the model's translation matrix as

glm::translate(glm::vec3(0, 0, 4))

to move the model a little bit forward so that I can see it. Since in OpenGL, by default, negative z is out towards the 'camera' and positive z is forward, I expected this to work but it doesn't. It only works if I set it to

glm::translate(glm::vec3(0, 0, -4))

But this seems weird to me, as I am setting my zNear to 0.01 and zFar to 1000. Is glm's z values flipped or am I doing something wrong here?

Here is my code:

glm::mat4 rotation = glm::mat4(1.0f);
glm::mat4 translation = glm::translate(glm::vec3(0, 0, -4));
glm::mat4 scale = glm::mat4(1.0f);

glm::mat4 modelMatrix = translation * rotation * scale;

glm::mat4 projectionMatrix = glm::perspective(70.0f, aspectRatio, 0.01f, 1000.0f);

glm::mat4 transformationMatrix = projectionMatrix * modelMatrix;

Since in OpenGL, by default, negative z is out towards the 'camera' and positive z is forward

You're wrong. OpenGL uses a right-handed coordinate-system where x is right, y is up and z is into the camera, therefore negative z is forwards.

| improve this answer | |
  • \$\begingroup\$ Oh, I always assumed the opposite. So setting zNear to 0.01 and zFar to 1000 in a perspective matrix is actually setting them to -0.01 and -1000 respectively? \$\endgroup\$ – Mertcan Ekiz Jul 31 '15 at 2:08
  • \$\begingroup\$ Well thats hard to explain. They are not stored directly, they are used to calculate the projection-matrix. But in some sense you could say that you'd see -0.1 to -1000.0, yes. \$\endgroup\$ – tkausl Jul 31 '15 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.