C++ Opengl:3D 上的 2d HUD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5467218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:13:43  来源:igfitidea点击:

Opengl: 2d HUD over 3D

c++opengl

提问by Elgoog

I have looked at some questions posted here on the matter and still cant work out why my 2d HUD appears but makes my 3d Rendered world disappear.

我查看了此处发布的一些关于此事的问题,但仍然无法弄清楚为什么我的 2d HUD 出现但使我的 3d 渲染世界消失。

EDIT:It seems that the 2d scene is taking control of the entire screen so every now and then I can see the 3d scene glitching through the 2d scene. So even though I its only ment to be rendering a quad thats 10 x 10 pixels it renders this then blanks out the rest of the screen.

编辑:似乎 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();

Then I swap buffers

然后我交换缓冲区

Here is the order of my code. Its like it makes the 3d space then makes the 2d space which in turn cancels out the 3d space.

这是我的代码的顺序。它就像它制造了 3d 空间然后制造了 2d 空间,然后又抵消了 3d 空间。

回答by Elgoog

Took a little while to figure it out, so just in case others have the same issues:

花了一点时间才弄明白,以防万一其他人有同样的问题:

    ...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...

:)

:)

回答by geofftnz

If you're overlaying a 2D ortho projection over 3D, you generally want to get the depth buffer out of the equation:

如果您在 3D 上叠加 2D 正射投影,您通常希望从等式中去除深度缓冲区:

glDepthMask(GL_FALSE);  // disable writes to Z-Buffer
glDisable(GL_DEPTH_TEST);  // disable depth-testing

Of course, you'll want to reset these to their original values before doing your next 3D pass.

当然,在进行下一次 3D 传递之前,您需要将这些值重置为它们的原始值。

回答by Kromster

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;

Note that there's no need to handle Push/Pop of matrixes if you render 2D completely on top of 3D.

请注意,如果您完全在 3D 之上渲染 2D,则无需处理矩阵的 Push/Pop。