glFrustum 没有创造透视

glFrustum isn't creating perspective

我正在尝试使用 C++ 和 openGL 创建一个 3D 操作程序。我对 openGL 比较陌生,所以我经常需要查阅文档才能找到正确的函数来完成我想做的事情。我认为我对正交投影和透视投影有很好的理解(因为 glOrtho 创建一个正交投影,其中不同的 z 值看起来没有不同, glFrustum 创建一个透视投影,其中 z 值是仔细看大)。但是,当我在我的程序中换出 glOrthoglFrustum 时,我没有看到任何区别。我在下面复制了一个显示效果的小程序。作为参考,我将 openGL 与 freeglut 一起使用。

#include "GL/freeglut.h"
void initFunc()
{
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);

}

void displayFunc()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glColor3f(1.0f, 1.0f, 0.0f);
    glLineWidth(1.0f);
    glutWireTeapot(0.3);

    glTranslatef(0, -0.5, -0.5);
    glutWireTeapot(0.3);

    glutSwapBuffers();
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(0, 0);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutCreateWindow("Teapot Perspective");

    initFunc();

    glutDisplayFunc(displayFunc);

    glutMainLoop();
}

我正在绘制两个在 y 轴和 z 轴上稍微偏移的茶壶。据我了解,glOrtho 应该将两个茶壶呈现为相同,只有一个 y 偏移量,而 glFrustum 应该使其中一个茶壶比另一个大。但是,它们都对茶壶进行了相同的渲染。

我是不是漏掉了什么?我还需要采取其他步骤来正确设置透视投影吗?还是我误解了 glFrustum 的工作原理?我也尝试过使用 gluPerspective 而不是 glFrustum 但我似乎无法找到要使用的正确值。我尝试了 90 的 FOV、1 的纵横比和各种 z 值,但它们都没有产生茶壶,或者茶壶变形得面目全非。此外,gluPerspective 似乎与具有相应参数的 glFrustum 调用具有不同的行为。我不确定我在这里遗漏了什么。

在正交投影中,视图中的坐标 space 线性映射到剪辑 space 坐标,剪辑 space 坐标等于标准化设备坐标,因为 w分量为1(对于笛卡尔输入坐标)。
left、right、bottom、top、near 和 far 的值定义了一个框。盒子体积内的所有几何体都在视口上 "visible"。

glOrtho定义的正交投影矩阵是:

r = right, l = left, b = bottom, t = top, n = near, f = far

x:  2/(r-l)         0               0               0
y:  0               2/(t-b)         0               0
z:  0               0               -2/(f-n)        0
t:  -(r+l)/(r-l)    -(t+b)/(t-b)    -(f+n)/(f-n)    1

在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口的 2D 点的映射。
相机视锥体(截棱锥)中的眼睛 space 坐标映射到立方体(归一化设备坐标)。

透视投影矩阵可以由平截头体 (glFrustum) 定义。
距离 leftrightbottomtop 是近平面上从视图中心到平截头体侧面的距离。 nearfar 指定到平截头体上近平面和远平面的距离。

r = right, l = left, b = bottom, t = top, n = near, f = far

x:  2*n/(r-l)      0              0                0
y:  0              2*n/(t-b)      0                0
z:  (r+l)/(r-l)    (t+b)/(t-b)    -(f+n)/(f-n)    -1
t:  0              0              -2*f*n/(f-n)     0

你提出的这样的规范问题:

glFrustum(-1, 1, -1, 1, -1, 1);

没有定义适当的截锥体,因为近平面的值为负而远平面的值为正。

如果您要检查 OpenGL 错误(通过 glGetError),那么您会得到一个 INVALID_OPERATION 错误。

OpenGL 4.6 API Compatibility Profile Specification; 12.1. FIXED-FUNCTION VERTEX TRANSFORMATIONS; page 501: >

void Frustum( double l, double r, double b, double t, double n, double f );

the coordinates (l b −n)T and (r t −n)T specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, respectively (assuming that the eye is located at (0 0 0)T). f gives the distance from the eye to the far clipping plane.

Errors An INVALID_VALUE error is generated if n <= 0, f <= 0, l == r, b == t, or n == f.