OpenGL 2D hud in a 3D application

I am trying to create a hud in my OpenGL application. Looking around, it seems that this can be done with orthoprojection, but so far I have not been able to correctly display the program. What happens, instead of rendering on top of my display, I get odd graphic crashes, as shown here:

enter image description here

If I comment on the hud code, everything will be fine.

glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); //Set up projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Using gluPerspective. It 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(); 
+4
source share
2 answers

Once you have finished rendering the HUD, you need to enable depth recording again

 glDepthMask(GL_TRUE); 
+4
source

When you clear buffers when rendering your HUD, everything that was (your 3D scene) will also be cleared. Therefore, do not flush the buffer twice.

+2
source

Source: https://habr.com/ru/post/1384500/


All Articles