我已经看过这里发布的一些问题但仍然无法解决为什么我的2d HUD出现但让我的3D渲染世界消失了。
编辑:似乎2d场景正在控制整个屏幕,所以我偶尔会看到3D场景在2d场景中出现故障。因此即使我只渲染一个10 x 10像素的四边形,它也会渲染出来,然后将剩余的屏幕空白。
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,-0.5,-6.0);
glPushMatrix();
..Draw some 3d stuff...
glPopMatrix();
// Start 2d
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0f, 255.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(0.0, 0.0);
glVertex2f(10.0, 0.0);
glVertex2f(10.0, 10.0);
glVertex2f(0.0, 10.0);
glEnd();
然后我交换缓冲区
这是我的代码的顺序。就像它使得3d空间然后形成2d空间,这反过来取消了3d空间。
答案 0 :(得分:35)
花了一点时间弄明白,以防其他人遇到同样的问题:
...After Drawing 3d Stuff...
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
//glPushMatrix(); ----Not sure if I need this
glLoadIdentity();
glDisable(GL_CULL_FACE);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(10.0, 0.0);
glVertex2f(10.0, 10.0);
glVertex2f(0.0, 10.0);
glEnd();
// Making sure we can render 3d again
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
//glPopMatrix(); ----and this?
...Then swap buffers...
:)
答案 1 :(得分:6)
如果您在3D上覆盖2D正投影,通常需要从等式中获取深度缓冲:
glDepthMask(GL_FALSE); // disable writes to Z-Buffer
glDisable(GL_DEPTH_TEST); // disable depth-testing
当然,在进行下一次3D传球之前,您需要将这些值重置为原始值。
答案 2 :(得分:2)
glViewport(0, 0, x, y); //You need to do this only once on viewport resize
//Setup for 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(40.0, (GLdouble)x/(GLdouble)y, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BIT);
// ... Render 3D ...
//Setup for 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BIT);
// ... Render 2D ...
SwapBuffers;
请注意,如果您在3D上完全渲染2D,则无需处理矩阵的推/弹。