在我尝试使用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();
}
答案 0 :(得分: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