Opengl drawing a 2d overlay on a 3d scene problem

时间秒杀一切 提交于 2019-11-26 22:51:49

问题


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();

回答1:


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.




回答2:


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




回答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:


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.




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/4280185/opengl-drawing-a-2d-overlay-on-a-3d-scene-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!