1

在此处输入图像描述

在我尝试使用 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();
}
4

1 回答 1

0

glCullFace(GL_FRONT_AND_BACK);似乎比较可疑。GL_FRONT_AND_BACK将丢弃所有面,只允许线和点。

常见的经典设置如下所示:

glEnable(GL_CULL_FACE); //<-- missing
glCullFace(GL_BACK);

glEnable(GL_LIGHTING); //<-- missing
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0); //Despite culling is on anyway,
                                          //the back faces of 'solid' meshes don't
                                          //need illumination

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); //GL_ALWAYS allows back faces to cover front faces in your scene
于 2013-11-20T21:46:19.173 回答