18

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Elgoog
  • 2,205
  • 7
  • 36
  • 48
  • What's the difference between `SCREEN_WIDTH` and `x` (used in `glViewport`)? If you comment out the last six lines, but leave the 2-D setup in place, does the 3-D rendering still disappear? Can you narrow it down to one line or command that interferes with the 3-D part? – Ben Voigt Mar 29 '11 at 03:51
  • @Ben Voigt: If i comment out the last 6 lines(starting at glbegin()) the 3d rendering still doesn't appear. SCREEN_WIDTH and X are the same, I made two vars without realizing and haven't tidied it up yet. – Elgoog Mar 29 '11 at 03:58
  • Ok, keep commenting 2-D setup code out until the 3-D world comes back. Either a line at a time or use a binary search to divide and conquer. Probably you're changing OpenGL state that the 3-D code assumed was left at its default, like texturing or a shader or something like that. – Ben Voigt Mar 29 '11 at 03:59
  • @Ben Voigt: If I comment out from the 2nd `glMatrixMode(GL_PROJECTION);` the 3d render displays – Elgoog Mar 29 '11 at 04:27

3 Answers3

37

Took a little while to figure it out, so just in case others have the same issues:

    ...After Drawing 3d Stuff...

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
//glPushMatrix();        ----Not sure if I need this
glLoadIdentity();
glDisable(GL_CULL_FACE);

glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0);
    glVertex2f(0.0, 0.0);
    glVertex2f(10.0, 0.0);
    glVertex2f(10.0, 10.0);
    glVertex2f(0.0, 10.0);
glEnd();

// Making sure we can render 3d again
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
//glPopMatrix();        ----and this?

...Then swap buffers...

:)

Elgoog
  • 2,205
  • 7
  • 36
  • 48
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.

geofftnz
  • 9,954
  • 2
  • 42
  • 50
3
glViewport(0, 0, x, y); //You need to do this only once on viewport resize

//Setup for 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(40.0, (GLdouble)x/(GLdouble)y, 0.5, 20.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BIT);

// ... Render 3D ...

//Setup for 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BIT);

// ... Render 2D ...

SwapBuffers;

Note that there's no need to handle Push/Pop of matrixes if you render 2D completely on top of 3D.

Kromster
  • 7,181
  • 7
  • 63
  • 111