Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicating GLM::perspective in code

I'm having trouble understanding glm::perspective. I know what it does, but don't understand the mechanism. Does anyone know what the source code / procedure is?

like image 718
AmazingVal Avatar asked Mar 25 '14 00:03

AmazingVal


1 Answers

It's all open-source, look at the code:

template <typename valType>
GLM_FUNC_QUALIFIER detail::tmat4x4<valType, defaultp> perspective
(
    valType const & fovy,
    valType const & aspect,
    valType const & zNear,
    valType const & zFar
)
{
    assert(aspect != valType(0));
    assert(zFar != zNear);

#ifdef GLM_FORCE_RADIANS
    valType const rad = fovy;
#else
#   pragma message("GLM: perspective function taking degrees as a parameter is deprecated.     #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    valType const rad = glm::radians(fovy);
#endif

    valType tanHalfFovy = tan(rad / valType(2));

    detail::tmat4x4<valType, defaultp> Result(valType(0));
    Result[0][0] = valType(1) / (aspect * tanHalfFovy);
    Result[1][1] = valType(1) / (tanHalfFovy);
    Result[2][2] = - (zFar + zNear) / (zFar - zNear);
    Result[2][3] = - valType(1);
    Result[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
    return Result;
}

...which creates a matrix as per the gluPerspective() documentation.

like image 131
genpfault Avatar answered Oct 04 '22 13:10

genpfault