在我尝试使用 glnormal 进行照明后,我的立方体看起来像这样(房子)。如果我摆脱 glnormal 那就很好,如果我要摆脱 glDepthFunc(GL_ALWAYS); 然后就好了,不知道这是怎么回事。
还有另一个问题,在我使用 glDepthFunc(GL_ALWAYS); 之后,我尝试添加其他东西,我将 z 轴坐标转换为房子后面,但我仍然看到它?我误解了深度测试吗?
最初设定
glEnable(GL_LIGHTING);
GLfloat light[] = {1,1,1,1};
GLfloat light_position[] = {20,20,-5,1};
glLightfv(GL_LIGHT0,GL_DIFFUSE,light);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glEnable(GL_LIGHT0);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_FRONT_AND_BACK);
// Draw stuff
drawbackground();
glDepthFunc(GL_ALWAYS);
绘制立方体的函数
void Cube::walk_gl(){
double xx = 0.0, yy=0.0;
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
glBegin(GL_QUADS);
glNormal3d(0, 0, -1);
glVertex3d(xx, yy, 1.0);
glVertex3d(xx+1.0, yy, 1.0);
glVertex3d(xx+1.0, yy+1.0, 1.0);
glVertex3d(xx, yy+1.0, 1.0);
glNormal3d(0, 0, 1);
glVertex3d(xx, yy, 0.0);
glVertex3d(xx+1.0, yy, 0.0);
glVertex3d(xx+1.0, yy+1.0, 0.0);
glVertex3d(xx, yy+1.0, 0.0);
glNormal3d(0, -1, 0);
glVertex3d(xx, yy, 0.0);
glVertex3d(xx+1.0, yy, 0.0);
glVertex3d(xx+1.0, yy, 1.0);
glVertex3d(xx, yy, 1.0);
glNormal3d(0, 1, 0);
glVertex3d(xx, yy+1.0, 0.0);
glVertex3d(xx+1.0, yy+1.0, 0.0);
glVertex3d(xx+1.0, yy+1.0, 1.0);
glVertex3d(xx, yy+1.0, 1.0);
glNormal3d(-1, 0, 0);
glVertex3d(xx, yy, 0.0);
glVertex3d(xx, yy+1.0, 0.0);
glVertex3d(xx, yy+1.0, 1.0);
glVertex3d(xx, yy, 1.0);
glNormal3d(1, 0, 0);
glVertex3d(xx+1.0, yy, 0.0);
glVertex3d(xx+1.0, yy+1.0, 0.0);
glVertex3d(xx+1.0, yy+1.0, 1.0);
glVertex3d(xx+1.0, yy, 1.0);
glEnd();
}