title: OpenGL depth test is not working
body: <p>I am trying to create a model using OpenGL, and I tried to enable depth testing.</p>
  
  <p>I use these commands in my <code>main</code>:</p>
  
  <pre><code>glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glEnable(GL_DEPTH_TEST);
  </code></pre>
  
  <p>And this in my display:</p>
  
  <pre><code>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  </code></pre>
  
  <p>I even tried adding:</p>
  
  <pre><code>glDepthFunc(GL_LEQUAL);
  </code></pre>
  
  <p>And it doesn't work. I can still see what I think are depth problems.
  Here is a video showing the problem: <a href="https://www.youtube.com/watch?v=OafrRH4Mzjc" rel="nofollow noreferrer">https://www.youtube.com/watch?v=OafrRH4Mzjc</a></p>
  
  <ul>
  <li>Note: In that video, the board is build right to left, top to bottom, so the first angle is OK, but any other angle is bad.</li>
  </ul>
  
  <p>What am I missing?</p>
  
  <hr>
  
  <p><strong>Edit</strong>: Minimal example file of reproduction:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#define _CRT_SECURE_NO_WARNINGS
  
  #define SIZE_MOVES 17
  
  #include &lt;stdio.h&gt;
  
  /* Include the GLUT library. This file (glut.h) contains gl.h and glu.h */
  #include &lt;GL\glew.h&gt;
  #include &lt;GL\freeglut.h&gt;
  
  static int  left_click = GLUT_UP;
  static int  right_click = GLUT_UP;
  static int  xold;
  static int  yold;
  static float rotate_x = 146;
  static float rotate_y = -26;
  
  int width, height;
  
  GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat light_position[] = { 0, 5, -10, 0 };
  
  GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
  GLfloat mat_shininess[] = { 1 };
  
  
  // colors
  const GLfloat colors[2][4] = {
      { 1.0, 1.0, 1.0, 1.0 }, //white
      { 0.0, 0.0, 0.0, 1.0 } //black
  };
  
  // rgb
  const GLfloat rgb[3][4] = {
      { 1.0, 0.0, 0.0, 1.0 },
      { 0.0, 1.0, 0.0, 1.0 },
      { 0.0, 0.0, 1.0, 1.0 }
  };
  
  void resetMaterial() {
      GLfloat c[] = { 1, 1, 1, 1 };
      glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  }
  
  void drawSquare(int color) {
      glPushMatrix(); {
          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[color]);
  
          glScalef(1, 0.5, 1);
          glutSolidCube(1);
      } glPopMatrix();
  }
  
  void drawBoard() {
      for (int i = 0; i &lt; 8; i++)
          for (int j = 0; j &lt; 8; j++) {
              glPushMatrix(); {
                  glTranslatef(i + 0.5, 0, j + 0.5);
                  drawSquare((i + j) % 2);
              } glPopMatrix();
          }
  }
  
  void drawAxes() {
      glBegin(GL_LINES); {
          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[0]);
          glVertex3f(-2, 0, 0); glVertex3f(5, 0, 0);
          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[1]);
          glVertex3f(0, -2, 0); glVertex3f(0, 5, 0);
          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[2]);
          glVertex3f(0, 0, -2); glVertex3f(0, 0, 5);
      } glEnd();
  }
  
  void letThereBeLight() {
      /*Add ambient light*/
      GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
  
      /*Add positioned light*/
      GLfloat lightColor1[] = { 0.2f, 0.2f, 0.1f, 1.0f };
      GLfloat lightPosition1[] = { -8, 8, 5, 0.0f };
  
      glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor1);
      glLightfv(GL_LIGHT0, GL_POSITION, lightPosition1);
  
      /*Add directed light*/
      GLfloat lightColor2[] = { 0.3, 0.3, 0.3, 1.0f };
      GLfloat lightPosition2[] = { 8, 8, -5, 1.0f };
      glLightfv(GL_LIGHT1, GL_AMBIENT, lightColor2);
      glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2);
  }
  
  void display(void) {
      // Clear frame buffer and depth buffer
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      // Set up viewing transformation, looking down -Z axis
      glLoadIdentity();
      gluLookAt(0, 5, -15, 0, 0, 3, 0, 1, 0);
  
      letThereBeLight();
  
      resetMaterial();
  
      // Rotate view:
      glPushMatrix(); {
          glRotatef(rotate_y, 1, 0, 0);
          glRotatef(rotate_x, 0, 1, 0);
  
          glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  
          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[0]);
  
          glPushMatrix(); {
              glTranslatef(-4, 0, -4); // Move to center
              drawBoard();
          } glPopMatrix();
  
          drawAxes(); // For debuging
      } glPopMatrix();
  
      /* End */
      glFlush();
      glutSwapBuffers();
  }
  
  void mouseFunc(int button, int state, int x, int y) {
      if (GLUT_LEFT_BUTTON == button)
          left_click = state;
  
      xold = x;
      yold = y;
  }
  
  void motionFunc(int x, int y) {
      if (GLUT_DOWN == left_click) {
          rotate_y = rotate_y + (y - yold) / 5.f;
          rotate_x = rotate_x + (x - xold) / 5.f;
          glutPostRedisplay();
      }
  
      xold = x;
      yold = y;
  }
  
  
  void reshapeFunc(int new_width, int new_height) {
      width = new_width;
      height = new_height;
  
      glViewport(0, 0, width, height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(50, width / height, 1, 20);
  
      glMatrixMode(GL_MODELVIEW);
  
      glutPostRedisplay();
  }
  
  int main(int argc, char **argv) {
      /* Creation of the window */
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
      glutInitWindowSize(900, 600);
      glEnable(GL_DEPTH_TEST);
  
      glutCreateWindow("Chess");
  
      glEnable(GL_CULL_FACE);
      glCullFace(GL_BACK);
  
      glEnable(GL_POLYGON_SMOOTH);
      glEnable(GL_LINE_SMOOTH);
  
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_LIGHT1);
      glShadeModel(GL_SMOOTH);
      glDisable(GL_COLOR_MATERIAL);
  
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  
      /* Declaration of the callbacks */
      glutDisplayFunc(&amp;display);
      glutReshapeFunc(&amp;reshapeFunc);
      glutMouseFunc(&amp;mouseFunc);
      glutMotionFunc(&amp;motionFunc);
  
      /* Loop */
      glutMainLoop();
  
      /* Never reached */
      return 0;
  }
  </code></pre>