为什么这个透视投影矩阵计算不能给出正确的结果

Why does this Perspective Projection Matrix Calculation not give the correct result?

本文关键字:结果 不能 计算 透视投影 为什么      更新时间:2023-10-16

我不太明白。我有一个OpenGL应用程序,我正在从旧的固定功能管道转换到可编程管道,并摆脱了我一直在使用的过时的功能…我不擅长数学,而且矩阵对我来说还是个新鲜事物。

然而,我已经得到了我的ModelView矩阵工作正常,但我似乎不能得到透视矩阵工作正确。

我的意思是,我可以使用固定函数弃用的函数,获得矩阵的值,并将其与我生成的计算值相匹配…ModelView吗?没有问题。角度?它不匹配,我不明白为什么。

这两个方法可以通过注释掉预处理器#define USEPROJMATRIX

来切换

typedef float vec_t;
const static vec_t identityMatrix[16] = { 1.0, 0.0, 0.0, 0.0, 
                                          0.0, 1.0, 0.0, 0.0, 
                                          0.0, 0.0, 1.0, 0.0, 
                                          0.0, 0.0, 0.0, 1.0 };
const static vec_t zeroMatrix[16] = { 0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0 };
const double pi = 3.1415926535897932384626433832795;
float Utils::Radians(const float& degrees)
{
    return degrees * (pi / 180);
}
vec_t* Utils::FrustumMatrix(vec_t* out_matrix, const vec_t& left, const vec_t& right, const vec_t& bottom, const vec_t& top, const vec_t& znear, const vec_t& zfar) // Replaced glFrustum()
{
    memcpy(out_matrix, zeroMatrix, 16*sizeof(vec_t));
    out_matrix[0] = (2.0 * znear) / (right - left);
    out_matrix[5] = (2.0 * znear) / (top - bottom);
    out_matrix[8] = (right + left) / (right - left);
    out_matrix[9] = (top + bottom) / (top - bottom);
    out_matrix[10] = -((zfar + znear) / (zfar - znear));
    out_matrix[11] = -1.0;
    out_matrix[14] = -((2.0 * zfar * znear) / (zfar - znear));
    return out_matrix;
}
vec_t* Utils::PerspectiveMatrix(vec_t* out_matrix, const vec_t& yFOVDegrees, const vec_t& aspectRatio, const vec_t& znear, const vec_t& zfar) // Replaces gluPerspective()
{
    vec_t range, left, right, bottom, top;
    range = tan(Radians(yFOVDegrees / 2)) * znear;
    left = -range * aspectRatio;
    right = range * aspectRatio;
    bottom = -range;
    top = range;
    return FrustumMatrix(out_matrix, left, right, bottom, top, znear, zfar);
}
#define USEPROJMATRIX
void GameLogic::OnResize(int width = 640, int height = 480)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
#ifndef USEPROJMATRIX
    glLoadMatrixf(identityMatrix);
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
#else
    vec_t pMatrix[16];
    memcpy(pMatrix, identityMatrix, sizeof(vec_t) * 16);
    Utils::PerspectiveMatrix(pMatrix, 45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
    glLoadMatrixf(pMatrix);
#endif
    vec_t projectionmatrix[16];
    glGetFloatv(GL_PROJECTION_MATRIX, projectionmatrix);
    /* Matrix calculation results in projectionmatrix equalling this:
       1.8106601, 0.00000000, 0.00000000, 0.00000000, 
       0.00000000, 2.4142134, 0.00000000, 0.00000000, 
       0.00000000, 0.00000000, -1.0020020, -1.0000000,
       0.00000000, 0.00000000, -2.0020020, 0.0000000
    */
    /* GL calculation
       1.8106601, 0.00000000, 0.00000000, 0.00000000,
       0.00000000, 2.4142137, 0.00000000, 0.00000000,
       0.00000000, 0.00000000, -1.0020020, -1.0000000,
       0.00000000, 0.00000000, -2.0020020, 0.00000000
    */
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(identityMatrix);
}

我通过直接观察和比较矩阵值来检查我的值。结果值在上面的评论中列出,我不知道为什么它们不匹配。手动计算的矩阵由于没有正确定位而在视觉上扭曲了渲染场景(一个简单的立方体偏移到中心左下方),我相信它也被渲染得更小了。

编辑根据尼古拉斯·博拉斯的回答修正了愚蠢的错误。但是,这仍然不能正常工作。注释中的值也被更新了。 edit2 以上代码现在可以正常工作了,问题解决了。我犯了愚蠢的错误。

这是你在gluPerspective调用中使用的:

(GLfloat)width / (GLfloat)height

这是你在Utils::PerspectiveMatrix调用中使用的:

width / height

第一个生成一个浮点值。第二个生成一个整数。我猜你想要第一个。