Opengl在3d场景上绘制2d叠加问题

发布于 2024-10-04 14:59:41 字数 682 浏览 3 评论 0原文

我设置了一个移动的 3D 场景,我想制作一个始终位于顶部的固定 2D GUI 叠加层,当我尝试制作 2D 形状时,我看不到任何东西。当我打电话时: glMatrixMode(GL_PROJECTION);我的 3D 场景消失了,只剩下一个空白窗口...

这是我用于叠加的代码

编辑:更新的代码

    glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glColor3f(1, 1, 1);
glPushMatrix();
glBegin(GL_QUADS); 
glVertex3f(-5.0f, 5.0f, 0.0f); 
glVertex3f(-5.0f, -5.0f, 0.0f); 
glVertex3f(5.0f, -5.0f, 0.0f); 
glVertex3f(5.0f, 5.0f, 0.0f); 
glEnd();   
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glutSwapBuffers();

I have a moving 3d scene set up, and I want to make a stationary 2d GUI overlay that is always on top, when I try making 2d shapes I don't see anything. When I call: glMatrixMode(GL_PROJECTION); my 3d scene disappears and I'm left with a blank window...

here is the code I'm using for the overlay

EDIT: updated code

    glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glColor3f(1, 1, 1);
glPushMatrix();
glBegin(GL_QUADS); 
glVertex3f(-5.0f, 5.0f, 0.0f); 
glVertex3f(-5.0f, -5.0f, 0.0f); 
glVertex3f(5.0f, -5.0f, 0.0f); 
glVertex3f(5.0f, 5.0f, 0.0f); 
glEnd();   
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glutSwapBuffers();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溺渁∝ 2024-10-11 14:59:41

嗯...根据您发布的代码片段,我相信您的场景消失是因为您对矩阵所做的操作 - 对我来说看起来有点混乱。该方法应如下所示:

  • 清洁 3D 屏幕
    • 启用照明、z 测试等
    • 将活动矩阵模式设置为投影
    • 加载身份并建立透视投影
    • 将活动矩阵模式设置回模型视图
    • 以 3D 方式绘制所有内容
  • 2D:
    • 禁用照明、z 测试等
    • 将活动矩阵模式设置为投影
    • 加载身份并建立正交投影
    • 将活动矩阵模式设置回模型视图
    • 绘制所有 2D 内容
  • 交换缓冲区

此外,考虑切换到着色器(以及一般而言,现代 OpenGL 版本)如果您想让您的生活变得更加轻松:)。

Hmm... Basing on the fragment of code you posted, I believe that your scene disappears because of what you're doing with your matrices - looks a bit chaotic to me. The approach should look like this:

  • clean the screen
  • 3D:
    • enable lighting, z-test, etc
    • set active matrix mode to projection
    • load identity and establish a perspective projection
    • set active matrix mode back to modelview
    • draw everything 3D
  • 2D:
    • disable lighting, z-test, etc
    • set active matrix mode to projection
    • load identity and establish an ortogonal projection
    • set active matrix mode back to modelview
    • draw everything 2D
  • swap buffers

Also, consider switching to shaders (and to a modern OpenGL version in general) if you want to make your life even easier :).

梨涡 2024-10-11 14:59:41

您必须以其他顺序绘制四边形。默认情况下,OpenGL 使用逆时针正面多边形。这意味着您看不到多边形,因为您只能看到它的背面。

您可以查看 glFrontFace

编辑:

此外,如果这不起作用,您可以尝试禁用以下状态:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_BLENDING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);

您可能需要使用 glPushAttribglPopAttrib 以免弄乱您的状态。

You must draw your quad in the other order. By default, OpenGL use counterclockwise front facing polygons. That means that you don't see your polygon because you see only its back face.

You might take a look at glFrontFace.

EDIT:

Also, if that doesn't work, you could try to disable the following states:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_BLENDING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);

You might want use glPushAttrib and glPopAttrib in order not to mess your state.

拥抱影子 2024-10-11 14:59:41
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1, 1, 1);
glBegin(GL_QUADS); 
    glVertex3f(20.0f, 20.0f, 0.0f); 
    glVertex3f(20.0f, -20.0f, 0.0f); 
    glVertex3f(-20.0f, -20.0f, 0.0f); 
    glVertex3f(-20.0f, 20.0f, 0.0f); 
glEnd();   
/// Now swap buffers
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1, 1, 1);
glBegin(GL_QUADS); 
    glVertex3f(20.0f, 20.0f, 0.0f); 
    glVertex3f(20.0f, -20.0f, 0.0f); 
    glVertex3f(-20.0f, -20.0f, 0.0f); 
    glVertex3f(-20.0f, 20.0f, 0.0f); 
glEnd();   
/// Now swap buffers
醉生梦死 2024-10-11 14:59:41

此外,我还使用单独的 FBO 来处理此类事情。通常,覆盖层不必一直重新绘制,因此可以根据需要将其渲染到 FBO,然后将其渲染为每帧的全屏四边形。它浪费了一些填充率,但总的来说,我发现它通常更快,并且使代码更加清晰。

In addition, I also use a separate FBO for these kind of things. Usually the overlay doesn't have to be redrawn all the time, so render it on demand to a FBO and just render it as a fullscreen quad each frame. It wastes some fillrate but in general I find it is usually faster anyway and makes the code so much cleaner.

甜警司 2024-10-11 14:59:41

确保您的几何图形(特别是几何图形的 z 坐标,就 2d UI 而言)大于近平面(z 轴上的近平面后面),否则,发生在近平面前面的任何渲染- 飞机将不会被看到。我假设您已经在代码中的其他地方定义了视锥体(这是定义近平面的地方)。

如果近平面为 0.01f,那么您的顶点定义可能是

glVertex3f(-5.0f, 5.0f, -0.02f); 
glVertex3f(-5.0f, -5.0f, -0.02f); 
glVertex3f(5.0f, -5.0f, -0.02f); 
glVertex3f(5.0f, 5.0f, -0.02f); 

我相信您始终查看 -Z 轴的 MatrixMode( GL_MODELVIEW )
我希望这有帮助。

我可能是错的,但我认为 DEPTH_TEST 指的是最终渲染对象的 z 缓冲,我不认为它会禁用近平面值。

Make sure your geometry ( specifically the z coordinates of your geometry, in terms of your 2d UI ) is greater than the near plane ( behind the near plane on the z-axis ), otherwise, any rendering which takes place in front of the near-plane will not be seen. I'm assuming you have defined your view frustum somewhere else in the code ( this is where the near-plane is defined ).

If the near-plane is 0.01f, then your vertex definitions could be

glVertex3f(-5.0f, 5.0f, -0.02f); 
glVertex3f(-5.0f, -5.0f, -0.02f); 
glVertex3f(5.0f, -5.0f, -0.02f); 
glVertex3f(5.0f, 5.0f, -0.02f); 

I believe in the MatrixMode( GL_MODELVIEW ) you are always looking into the -Z Axis.
I hope this helps.

I may be wrong but i think the DEPTH_TEST refers to the z-buffering of your final rendered object, i don't think it disables the near-plane value.

絕版丫頭 2024-10-11 14:59:41
'    glGetBooleanv(GL_BLEND,        &m_origin_blend);
glGetBooleanv(GL_DEPTH_TEST,&m_origin_depth);
glGetBooleanv(GL_CULL_FACE, &m_origin_cull);

setAlphaBlending(true);
setDepthTest(false);
setCullFace(false); //by stone

//ur draw core()

setAlphaBlending(m_origin_blend>0?true:false);
setDepthTest(m_origin_depth>0?true:false);
setCullFace(m_origin_cull>0?true:false); //by stone
'
'    glGetBooleanv(GL_BLEND,        &m_origin_blend);
glGetBooleanv(GL_DEPTH_TEST,&m_origin_depth);
glGetBooleanv(GL_CULL_FACE, &m_origin_cull);

setAlphaBlending(true);
setDepthTest(false);
setCullFace(false); //by stone

//ur draw core()

setAlphaBlending(m_origin_blend>0?true:false);
setDepthTest(m_origin_depth>0?true:false);
setCullFace(m_origin_cull>0?true:false); //by stone
'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文