听起来您想要做的类似于在简单游戏中绘制 2D HUD 时所做的事情,或者只是一个持久的背景。我不是专家,但我最近才这样做。您想要做的是将投影矩阵更改为正交投影,渲染您的四边形,然后切换回您之前的任何投影。您可以使用矩阵堆栈来做到这一点,它完全独立于任何相机。
所以,首先:
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluOrtho2D(0, w, h, 0);
这会将一个新的投影矩阵推送到投影堆栈上(我在这个项目中使用了 glut,但它应该只转换为普通的 OpenGL)。然后,我得到窗口的宽度和高度,并使用 gluOrtho2D 设置我的正交投影。
下一个:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Draw your quad here in screen coordinates
Here I just push a new, clean matrix onto the modelview. You may not need to do this.
Lastly:
glPopMatrix() // Pops the matrix that we used to draw the quad
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Pops our orthographic projection matrix, which restores the old one
glMatrixMode(GL_MODELVIEW); // Puts us back into GL_MODELVIEW since this is probably what you want
I hope this helps. As you can tell, it requires no use of the eye position. This is mainly because when we use a new orthographic projection that perfectly fits our window, this is irrelevant. This may not put your quad in the back however, if this is executed after the other draw calls.
EDIT: Turning off depth testing and clearing the depth buffer could help as well, as Boojum suggests.