1

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();
}
cakester
  • 421
  • 4
  • 15
  • 1
    Why are you using `GL_ALWAYS` as the depth function? This basically means that whatever you draw last is what will be visible, irrespective of its depth or anything else that was drawn before it. Usually you want to use `GL_LESS` or `GL_LEQUAL` so that only pixels that are closer to you than what was already drawn will show up. Also, `GL_FRONT_AND_BACK` is not a valid enum for `glCullFace (...)`. You might as well not draw anything at all if you are going to cull both sides ;) – Andon M. Coleman Nov 20 '13 at 21:44
  • I didn't know GL_ALWAYS does that...but I have something else on the screen and I tried to put it before the house, I changed the z coordinates but it just gets bigger and it never gets in front of the house – cakester Nov 20 '13 at 21:48
  • @AndonM.Coleman: [`glCullFace()`](http://www.opengl.org/sdk/docs/man2/xhtml/glCullFace.xml) is documented as accepting `GL_FRONT_AND_BACK`. Pretty useless, as you pointed out, but perfectly valid. – genpfault Nov 20 '13 at 21:54

1 Answers1

0

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
Sam
  • 7,778
  • 1
  • 23
  • 49