質問

enter image description here

My cube looked like this (the house) after I tried to use glnormal for the lighting. If I get rid of glnormal then it's fine, if I were to get rid of glDepthFunc(GL_ALWAYS); then it's fine as well , dont know what's going on here.

Also another question, after I used glDepthFunc(GL_ALWAYS);, I tried to add something else which I translated the z axis coordinate to be behind the house but I still see it? Did I misunderstood depth testing?

initial setup

    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);

the function that draws the cube

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();
}
役に立ちましたか?

解決

glCullFace(GL_FRONT_AND_BACK); seems rather suspicious. GL_FRONT_AND_BACK would discard all faces, allowing lines and points only.

The common, classic setup is something like the following:

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top