Alex Rivera | Logout

Opengl: 2d HUD over 3D

Asked 2011-03-29T02:26:48.907
19

I have looked at some questions posted here on the matter and still cant work out why my 2d HUD appears but makes my 3d Rendered world disappear.

EDIT: It seems that the 2d scene is taking control of the entire screen so every now and then I can see the 3d scene glitching through the 2d scene. So even though I its only ment to be rendering a quad thats 10 x 10 pixels it renders this then blanks out the rest of the screen.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,-0.5,-6.0);

glPushMatrix();

..Draw some 3d stuff...

glPopMatrix();
// Start 2d
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0f, 255.0f, 1.0f);
glBegin(GL_QUADS);
    glVertex2f(0.0, 0.0);
    glVertex2f(10.0, 0.0);
    glVertex2f(10.0, 10.0);
    glVertex2f(0.0, 10.0);
glEnd();

Then I swap buffers

Here is the order of my code. Its like it makes the 3d space then makes the 2d space which in turn cancels out the 3d space.

Edit
Report

1 Answer

7

If you're overlaying a 2D ortho projection over 3D, you generally want to get the depth buffer out of the equation:

glDepthMask(GL_FALSE);  // disable writes to Z-Buffer
glDisable(GL_DEPTH_TEST);  // disable depth-testing

Of course, you'll want to reset these to their original values before doing your next 3D pass.

answered 2011-03-29T03:38:24.557

Your Answer