Search code examples
openglmatrixtransformationprojectionglm-math

OpenGL Orthographical Projection


I have been playing around with OpenGL and matrix operations and I understand the concept of P * V * M but I cannot understand why changing the Z position of the 'camera' does not have the effect of zooming.

When using a perspective projection, changing the Z of the camera has the effect of zoom (as i'd expect).

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View       = glm::lookAt(
                            glm::vec3(0,0,3), // changing 3 to 8 will zoom out
                            glm::vec3(0,0,0),
                            glm::vec3(0,1,0)
                       );
glm::mat4 Model      = glm::mat4(1.0f);
gml::mat4 MVP        = Projection * View * Model;

However, when I use an ortho projection, changing the 3 to 8 or anything it does not have the effect of zooming out. I know they are very different projections but I am looking for an explanation (the math behind why it doesn't work would be especially helpful).

glm::mat4 Projection = glm::ortho(
    0.0f,
    128.0f,
    0.0f,
    72.0f,
    0.0f,
    100.0f
);

Solution

  • That's how orthographic projections work. Let's start with a perspective transform:

    You get the projection of an object by following a straight line to the camera:

    enter image description here

    If you move the camera closer, then you will see that the projected area increases:

    enter image description here

    Orthographic projections work differently. You get the projection by following a straight line that is perpendicular to the image plane:

    enter image description here

    And obviously, the size of the projected area does not depend on how far the camera is away from the object. That's because the projection lines will always be parallel and preserve the size of the object in the two directions of the image plane.