读书人

关于深度缓冲和深度测试的有关问题

发布时间: 2013-01-11 11:57:35 作者: rapoo

关于深度缓冲和深度测试的问题
是不是启用深度测试并不需要开启深度缓冲?
下面这个例子:


#include <GLUT.H>

void Display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
glEnable(GL_DEPTH_TEST);//注释掉则无遮挡效果

glTranslatef(0,0,-1);
glRectf(-2,2,2,-2);

glColor3f(0,1,0);
glTranslatef(0,0,-1);
glRectf(-2,2,2,-2);

glFlush();
}


void ChangeWindowSize(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat aspectRatio=(GLfloat)w/(GLfloat)h;
if(w<h)
glOrtho(-20,20,-20/aspectRatio,20/aspectRatio,20,-20);
else
glOrtho(-20*aspectRatio,20*aspectRatio,-20,20,20,-20);

gluPerspective(45,aspectRatio,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("TestDepth");
glutReshapeFunc(ChangeWindowSize);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}



我没有开启深度缓冲,也能有遮挡效果,对这个概念还是不清,求解释啊!
[解决办法]
glutInitDisplayMode()参数GLUT_DEPTH
Bit mask to select a window with a depth buffer.

glEnable参数GL_DEPTH_TEST
If enabled, do depth comparisons and update the depth buffer. Note that even if the depth buffer exists and the depth mask is non-zero, the depth buffer is not updated if the depth test is disabled.

所以只要glEnable(GL_DEPTH_TEST)就行了

读书人网 >图形图像

热点推荐