Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

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

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...