如何正确设置我的平截头投影?

How do I correctly setup my frustum projection?

我正在尝试创建一个平截头体投影,以便我可以正确查看 3D 对象并在 3D 中导航 space。我知道在 0,0,0 处创建了一个锥形形状,您可以指定它的走向,但有几件事我不明白:

nearClipPlanefarClipPlane 到底有什么作用?为什么圆锥要远一点?

如何控制相机指向的位置和位置?

我见过有人只使用 1s-1s 创建截锥体,这是正确的方法吗?

我的代码:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(windowWidth / 2, windowWidth / 2, windowHeight / 2, windowHeight / 2, 0, -50);
glMatrixMode(GL_MODELVIEW);

while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //draw

    glfwSwapBuffers(window);
    glfwPollEvents();
}

首先,我建议使用新的 programmable pipeline 而不是旧的固定功能管道,因为它已经被弃用了十多年。首先,新的管道看起来很难,但是当你理解了它背后的概念时,它比旧的管道要简单得多。

What exactly do nearClipPlane and farClipPlane work?

位于近裁剪平面前面或远裁剪平面后面的对象将不会被渲染。 对于近平面和远平面,不应将负值传递给 glFrustum(),近平面的常用值为 0.1,远平面距离的常用值为 100-1000。

How do I control the position and where the camera points?

在 OpenGL 中,您不改变相机的位置,而是在相反的方向上变换整个场景(模型视图矩阵)。例如,如果你想将相机的位置设置为 10, 0, 5 并绕 y 轴旋转 45 度,你必须使用

glMatrixMode(GL_MODELVIEW);
/*
If you wouldn't generate a new identity matrix every frame, you would **add** the rotation/translation to the previous one, which obviously isn't what you want
*/
glLoadIdentity();
Vector3f cameraPosititon = new Vector3f(10, 0, 5);    

/*
rotate selected matrix about 45 degrees around y
With the 1 as third argument you select that you want to rotate over the y-axis.
If you would pass 1 as 2nd or 4th argument, you would rotate over the x or the z axis.*/
glRotatef(45, 0, 1, 0);
//translate selected matrix about x, y, z
glTranslatef(-1 * cameraPosition.x, -1 * cameraPosition.y, -1 * cameraPosition.z);

我已经将相机的位置乘以-1,因为你必须在相反的方向上变换整个场景才能获得看起来相机在四处移动的效果。