OpenGL 2D hud在3D应用程序中 [英] OpenGL 2D hud in 3D application

查看:597
本文介绍了OpenGL 2D hud在3D应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的OpenGL应用程序中创建一个hud。环顾四周,似乎做到的是一个正交投影,但到目前为止我还没有能够让程序正确地渲染。发生了什么,而不是在我的显示器顶部,我得到奇怪的图形故障,如这里所示:

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:

如果我注释掉hud代码,一切都会完美呈现。

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


推荐答案

完成HUD渲染后,重新启用深度写入

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

glDepthMask(GL_TRUE);

这篇关于OpenGL 2D hud在3D应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆