4

I have been trying to create a hud in my OpenGL application. Having looked around, it seems the way to do it is with an ortho projection, but so far I have not been able to get the program to render correctly. What is happening is instead of rendering on top of my display, I'm getting odd graphical glitches as seen here:

enter image description here

If I comment out the hud code, everything renders perfectly.

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

//Set up projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Using gluPerspective. It's pretty easy and looks nice.
gluPerspective(fov, aspect, zNear, zFar);

//Set up modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//3D rendering

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,window_width,0,window_height); //left,right,bottom,top

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(0.0,0.0,1.0);
glBegin(GL_QUADS);
    glVertex2f(50,50);
    glVertex2f(50,100);
    glVertex2f(100,100);
    glVertex2f(100,50);
glEnd();
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Katherine1
  • 195
  • 1
  • 2
  • 14

2 Answers2

4

Once you're done rendering the HUD, you need to re-enable the depth writes

glDepthMask(GL_TRUE);
Bahbar
  • 17,760
  • 43
  • 62
2

When you clear the buffers when rendering your HUD, all that has been drawn so far (your 3D scene) will also be cleared. So don't clear the buffer twice.

cli_hlt
  • 7,072
  • 2
  • 26
  • 22
  • I commented out glClear in the hud section, and it had no visual effect on the program. – Katherine1 Dec 03 '11 at 20:13
  • Try to enable alpha blending and draw your quad transparent to check. It seems also that your quad might be too big in relation to the window size. I wonder - and cannot see why - it is not blue, however. And where the red triangle is from. Try moving a bit backward (translate 0,0,-1 or so) before drawing your hud. – cli_hlt Dec 03 '11 at 20:25
  • When I move, I have a black I don't know what that matches the initial view of the maze (it changes shape to match the configuration of the hall I'm looking at when I start) that obscures my sight and causes all manner of weirdness where I can see. – Katherine1 Dec 03 '11 at 20:30
  • I commented out the quad entirely and nothing has changed visually. – Katherine1 Dec 03 '11 at 20:33