Opengl在3d场景上绘制2d叠加问题
我设置了一个移动的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
嗯...根据您发布的代码片段,我相信您的场景消失是因为您对矩阵所做的操作 - 对我来说看起来有点混乱。该方法应如下所示:
此外,考虑切换到着色器(以及一般而言,现代 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:
Also, consider switching to shaders (and to a modern OpenGL version in general) if you want to make your life even easier :).
您必须以其他顺序绘制四边形。默认情况下,OpenGL 使用逆时针正面多边形。这意味着您看不到多边形,因为您只能看到它的背面。
您可以查看 glFrontFace。
编辑:
此外,如果这不起作用,您可以尝试禁用以下状态:
您可能需要使用
glPushAttrib
和glPopAttrib
以免弄乱您的状态。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:
You might want use
glPushAttrib
andglPopAttrib
in order not to mess your state.此外,我还使用单独的 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.
确保您的几何图形(特别是几何图形的 z 坐标,就 2d UI 而言)大于近平面(z 轴上的近平面后面),否则,发生在近平面前面的任何渲染- 飞机将不会被看到。我假设您已经在代码中的其他地方定义了视锥体(这是定义近平面的地方)。
如果近平面为 0.01f,那么您的顶点定义可能是
我相信您始终查看 -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
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.