关于深度缓冲和深度测试的问题
是不是启用深度测试并不需要开启深度缓冲?
下面这个例子:
#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;
}