How is aspect ratio used in perspective projection?

531 Views Asked by At

I am trying to figure out how aspect ratio is used in perspective projection. I would say I pretty much understand how perspective projection works. I need to fill in a few blanks, though, I have been reading song ho's tutorial on projection matrices (http://www.songho.ca/opengl/gl_projectionmatrix.html#ortho).

In my code I use the following function, utilizing the GLM library: glm::mat4 projection = glm::perspective(fov, aspect, zNear, zFar)

I believe I found the code this function in GLM's GitHub (https://github.com/g-truc/glm/blob/b3f87720261d623986f164b2a7f6a0a938430271/glm/ext/matrix_clip_space.inl). There are a few similar functions:

template<typename T>
GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> perspectiveRH_ZO(T fovy, T aspect, T zNear, T zFar)
{
    assert(abs(aspect - std::numeric_limits<T>::epsilon()) > static_cast<T>(0));

    T const tanHalfFovy = tan(fovy / static_cast<T>(2));

    mat<4, 4, T, defaultp> Result(static_cast<T>(0));
    Result[0][0] = static_cast<T>(1) / (aspect * tanHalfFovy);
    Result[1][1] = static_cast<T>(1) / (tanHalfFovy);
    Result[2][2] = zFar / (zNear - zFar);
    Result[2][3] = - static_cast<T>(1);
    Result[3][2] = -(zFar * zNear) / (zFar - zNear);
    return Result;
}

So, I am trying to connect this with Song Ho's tutorial on perspective projection.

Song Ho's Matrix

It is evident the code is written to assume a symmetrical viewing volume. If the code and the matrix I am matching are related...

How is 1 / aspect * tanHalfFovy related to 1 / r in the tutorial?

Same for the second column, how is 1 / tanHalfFovyrelated to 1 / t in the tutorial?