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...
我有一个移动的3D场景设置,我想制作一个永远在顶部的固定2d GUI覆盖,当我尝试制作2d形状时,我什么也看不到。当我调用:glMatrixMode(GL_PROJECTION);我的3D场景消失了,我留下了一个空白的窗口......
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();
6 个解决方案
#1
4
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.
您必须以其他顺序绘制四边形。默认情况下,OpenGL使用逆时针前面的多边形。这意味着您没有看到多边形,因为您只看到它的背面。
You might take a look at glFrontFace.
你可以看一下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.
你可能想要使用glPushAttrib和glPopAttrib,以免弄乱你的状态。
#2
4
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
启用照明,z测试等
将有源矩阵模式设置为投影
加载标识并建立透视投影
将活动矩阵模式设置回modelview
画出一切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
禁用照明,z测试等
将有源矩阵模式设置为投影
加载标识并建立正交投影
将活动矩阵模式设置回modelview
绘制所有2D
- swap buffers
清洁屏幕
3D:启用光照,z测试等设置主动矩阵模式到投影载荷标识并建立透视投影设置主动矩阵模式回到模型视图绘制所有3D
2D:禁用光照,z测试等将活动矩阵模式设置为投影载荷标识并建立正交投影集活动矩阵模式返回模型视图绘制所有2D
Also, consider switching to shaders (and to a modern OpenGL version in general) if you want to make your life even easier :).
另外,如果你想让你的生活更轻松,可以考虑切换到着色器(以及一般的现代OpenGL版本)。
#3
3
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
#4
1
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.
另外,我还使用单独的FBO来做这些事情。通常不必一直重新绘制叠加层,因此可以根据需要将其渲染到FBO,并将其渲染为每帧的全屏四边形。它浪费了一些填充,但总的来说,我发现它通常更快,并使代码更清洁。
#5
1
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 ).
确保几何体(特别是几何体的z坐标,就第二个UI而言)大于*面(在z轴上的*面后面),否则,任何渲染都发生在近端前面 - 飞机将不会被看到。我假设您已经在代码中的其他位置定义了视锥(这是定义*面的位置)。
If the near-plane is 0.01f, then your vertex definitions could be
如果*面是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);
I believe in the MatrixMode( GL_MODELVIEW )
you are always looking into the -Z Axis. I hope this helps.
我相信MatrixMode(GL_MODELVIEW)你总是在研究-Z Axis。我希望这有帮助。
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.
我可能错了,但我认为DEPTH_TEST是指最终渲染对象的z缓冲,我认为它不会禁用*面值。
#6
0
' 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
'
#1
4
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.
您必须以其他顺序绘制四边形。默认情况下,OpenGL使用逆时针前面的多边形。这意味着您没有看到多边形,因为您只看到它的背面。
You might take a look at glFrontFace.
你可以看一下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.
你可能想要使用glPushAttrib和glPopAttrib,以免弄乱你的状态。
#2
4
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
启用照明,z测试等
将有源矩阵模式设置为投影
加载标识并建立透视投影
将活动矩阵模式设置回modelview
画出一切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
禁用照明,z测试等
将有源矩阵模式设置为投影
加载标识并建立正交投影
将活动矩阵模式设置回modelview
绘制所有2D
- swap buffers
清洁屏幕
3D:启用光照,z测试等设置主动矩阵模式到投影载荷标识并建立透视投影设置主动矩阵模式回到模型视图绘制所有3D
2D:禁用光照,z测试等将活动矩阵模式设置为投影载荷标识并建立正交投影集活动矩阵模式返回模型视图绘制所有2D
Also, consider switching to shaders (and to a modern OpenGL version in general) if you want to make your life even easier :).
另外,如果你想让你的生活更轻松,可以考虑切换到着色器(以及一般的现代OpenGL版本)。
#3
3
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
#4
1
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.
另外,我还使用单独的FBO来做这些事情。通常不必一直重新绘制叠加层,因此可以根据需要将其渲染到FBO,并将其渲染为每帧的全屏四边形。它浪费了一些填充,但总的来说,我发现它通常更快,并使代码更清洁。
#5
1
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 ).
确保几何体(特别是几何体的z坐标,就第二个UI而言)大于*面(在z轴上的*面后面),否则,任何渲染都发生在近端前面 - 飞机将不会被看到。我假设您已经在代码中的其他位置定义了视锥(这是定义*面的位置)。
If the near-plane is 0.01f, then your vertex definitions could be
如果*面是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);
I believe in the MatrixMode( GL_MODELVIEW )
you are always looking into the -Z Axis. I hope this helps.
我相信MatrixMode(GL_MODELVIEW)你总是在研究-Z Axis。我希望这有帮助。
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.
我可能错了,但我认为DEPTH_TEST是指最终渲染对象的z缓冲,我认为它不会禁用*面值。
#6
0
' 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
'