c++ - glFrustum 没有创造透视

标签 c++ opengl perspectivecamera projection-matrix

我正在尝试使用 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 调用具有不同的行为。我不确定我在这里遗漏了什么。

最佳答案

在正交投影中, View 空间中的坐标线性映射到裁剪空间坐标,并且裁剪空间坐标等于归一化设备坐标,因为 w分量为 1(对于笛卡尔输入坐标)。
left、right、bottom、top、near 和 far 的值定义了一个框。盒子体积内的所有几何体在视口(viewport)上都是“可见的”。

正交投影矩阵,由 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 点到视口(viewport)的 2D 点的映射。
视锥体(截棱锥)中的眼空间坐标映射到立方体(归一化设备坐标)。

透视投影矩阵可以由平截头体 ( glFrustum ) 定义。
距离left , right , bottomtop , 是近平面上从 View 中心到平截头体侧面的距离。 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.

关于c++ - glFrustum 没有创造透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462082/

相关文章:

c++ - OpenGL/C++ 将所有纹理 Assets 作为静态成员存储在单独的类中

opengl - 为什么 golang gomobile basic example 为 vec4 属性设置 3-float 大小?

javascript - 如何将相机补间到 three.js 中的对象?

opengl - 光线转换 : how to properly apply a projection matrix?

c++ - 静态内存中数组的大小可以在 C++ 运行时更改吗?怎么来的?

c++ - 一个线程结束后调用另一个线程中的方法

c++ - 开始学习 OpenGL,需要帮助解决这个问题

c++ - 相机的错误纵横比计算(简单的光线转换器)

c++ - 关于构建 shell 时的 fork()

c++ - insert(std::map<int,struct>) 段错误(核心已转储)