OpenGL 2D hud in 3D application

笑着哭i 提交于 2019-11-27 18:47:32

问题


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:

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();

回答1:


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

glDepthMask(GL_TRUE);



回答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.



来源:https://stackoverflow.com/questions/8370537/opengl-2d-hud-in-3d-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!