在代码中复制GLM::透视图

Replicating GLM::perspective in code

本文关键字:透视图 GLM 复制 代码      更新时间:2023-10-16

我理解glm::透视有困难。我知道它的作用,但不明白它的原理。有人知道源代码/程序是什么吗?

这都是开源的,看看代码:

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;
}

…它根据gluPerspective()文档创建一个矩阵。