我已经读过很多教程,但还没有掌握如何在没有灾难发生的情况下用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和三维场景消失了。我做错了什么?我错过了什么?我知道我应该推送投影矩阵,然后是模型视图矩阵,然后执行glOrtho2D(),然后将两个矩阵从堆栈中弹出。那不管用。我厌倦了按随机顺序推矩阵和弹出矩阵。
发布于 2014-05-07 04:04:49
如果您的HUD和通常一样是一个覆盖层,您可能希望禁用深度测试&在绘制HUD内容之前可能需要深度写入:
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
这样,HUD的Z值就无关紧要了。别忘了在画场景之前重新启用。
正如其他人所观察到的,您也存在矩阵堆栈问题。独立的堆栈与投影和模型视图矩阵相关联。现在,您正在推动投影矩阵和弹出模型视图矩阵,因此您正在溢出一个,而另一个则不足。你需要以对称的方式推和弹出相同的模式。假设您在每种模式下都使用一个glLoadIdentity()
,那么您可能根本不需要使用矩阵堆栈。
编辑:一个更直接的答案
以下是您的呈现代码应该是什么样子:
void render()
{
//Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//*************************************************************************
//* Prepare to render main scene:
//*************************************************************************
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( cam.getEye().getX(), cam.getEye().getY(), cam.getEye().getZ() );
glRotatef( cam.getTheta(), 0.0f, 1.0f, 0.0f );
//*************************************************************************
//* Draw Main Scene Here:
//*************************************************************************
// ...
//*************************************************************************
//* Prepare to render HUD overlay:
//*************************************************************************
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//*************************************************************************
//* Draw HUD here:
//*************************************************************************
// ...
// Finish the frame:
glutSwapBuffers(); //Send scene to the screen to be shown
}
现在,由于您将在每个呈现调用期间两次调整投影矩阵,所以您应该从gluPerspective()函数中删除resizeWindow()。另外,我从render函数中删除了glClearColor()调用--最好在程序开始时设置它一次,然后忘记它。状态更改是昂贵的,因此您希望尽可能避免它们。
https://stackoverflow.com/questions/23506751
复制相似问题