title: OpenGL Z-Index Problem?
body: <p>I am drawing a cube with LWJGL library for Java, and it draws fine, but when I rotate the cube, it seems like the front side will draw on top of the back side when it should be cut off.  Here is the code:</p>
  
  <p>Init code:</p>
  
  <pre><code>GL11.glViewport(0, 0, width, height);
  
  GL11.glMatrixMode(GL11.GL_PROJECTION);
  GL11.glLoadIdentity();
  
  GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 100.0f);
  
  GL11.glMatrixMode(GL11.GL_MODELVIEW);
  GL11.glLoadIdentity();
  </code></pre>
  
  <p>Cube Code:</p>
  
  <pre><code>    GL11.glTranslatef(0.0f, 0.0f, -6.0f);
  
      GL11.glRotatef(rquad, 0.0f, 1.0f, 0.0f);
  
      GL11.glBegin(GL11.GL_QUADS);
          GL11.glColor3f(0.0f, 1.0f, 0.0f);           // Set The Color To Green
          GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Top)
          GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Top)
          GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Bottom Left Of The Quad (Top)
          GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)
  
          GL11.glColor3f(1.0f, 0.5f, 0.0f);           // Set The Color To Orange
          GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Top Right Of The Quad (Bottom)
          GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Top Left Of The Quad (Bottom)
          GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Bottom)
          GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Bottom)
  
          GL11.glColor3f(1.0f, 0.0f, 0.0f);           // Set The Color To Red
          GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
          GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Front)
          GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Front)
          GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Front)
  
          GL11.glColor3f(1.0f, 1.0f, 0.0f);           // Set The Color To Yellow
          GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Back)
          GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Back)
          GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Back)
          GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Back)
  
          GL11.glColor3f(0.0f, 0.0f, 1.0f);           // Set The Color To Blue
          GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Left)
          GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Left)
          GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Left)
          GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Left)
  
          GL11.glColor3f(1.0f, 0.0f, 1.0f);           // Set The Color To Violet
          GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Right)
          GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
          GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Right)
          GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Right)
      GL11.glEnd();
  
      rquad += 0.1;
  </code></pre>
  
  <p>Pictures:</p>
  
  <p><img src="https://i.stack.imgur.com/BTo9G.png" alt="Cube Picture #1"></p>
  
  <p><img src="https://i.stack.imgur.com/hJwv9.png" alt="Cube Picture #2"></p>