c++openglprojectionglm-mathperspectivecamera

Why is GLM Perspective projection acting like Orthographic Projection


I Have a projection matrix in my C++ OpenGL Application.

glm::mat4 projection = glm::perspective(45.0f, 16.0f / 9.0f, 1.0f, 100.0f);

This Matrix is later sent as uniform to the Vertex Shader ->

Nade::Shader::SetMat4(app.shader->GetProgram(), "p", app.projection);

And Utilized inside the Vertex Shader

gl_Position = m * p * vec4(pos,1.0);

And then the Rendered Quad is moved at the Z Axis

object.Translate(0, 0, -0.05);

Observed Behavior: The Rendered Mesh behaves like it is within a Orthographic Matrix where it stays same in size but clips away at the far point

Expected Behavior: The Rendered Mesh reduces in size and clips away.

How can I fix this?


Solution

  • gl_Position = m * p * vec4(pos,1.0); is equivalent to gl_Position = m * (p * vec4(pos,1.0));, which means that the position is transformed by p before being transformed by m.

    Assuming p means "projection" and m means "modelview", then it should be:

    gl_Position = p * m * vec4(pos,1.0);

    You might be wondering: Why didn't this cause issues earlier?

    With an orthographic projection, and a camera looking down the z axis, the original code could still look like it works. That's because a zero-centered orthographic projection is basically just a scaling matrix.