我已经学习了许多教程,但仍然没有掌握如何在不发生某种灾难的情况下使用 3D 场景绘制2D HUD。我从这里和这里收集了一些示例代码,并计算出必须以某种方式处理矩阵的顺序(参见这里),以实现我所设定的目标。我已经制定了如下所示的渲染代码:
void render()
{
//Clear screen
glClearColor(0.2f, 0.0f, 0.2f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
// Movement as response to input:
glTranslatef( cam.getEye().getX(), cam.getEye().getY(), cam.getEye().getZ() ); // Translate our object along the y axis
glRotatef( cam.getTheta(), 0.0f, 1.0f, 0.0f ); // Rotate our object around the y axis
// No pushing/popping a matrix and no gluLookAt() used here.
// ==================== Drawing scene here: ======================
// ...
// =================================== HUD: ==================================
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//glPopMatrix();
glLoadIdentity();
gluOrtho2D(0, winW, 0, winH); //left,right,bottom,top
glMatrixMode(GL_MODELVIEW);
//glPushMatrix();
glPopMatrix();
glLoadIdentity();
// drawing the HUD rect here:
glBegin(GL_QUADS);
// ...
glEnd();
glPopMatrix();
glPopMatrix();
//glPushMatrix();
//glPushMatrix();
// ===============================================================================
glutSwapBuffers(); //Send scene to the screen to be shown
}
...但由于某种原因,它只显示我的深青色 HUD,而 3D 场景就消失了。我究竟做错了什么?我错过了什么?我知道我应该推送投影矩阵,然后是模型视图矩阵,然后执行 glOrtho2D(),然后将两个矩阵从堆栈中弹出。那没有用。我厌倦了以随机顺序摆弄推挤和弹出矩阵。