title: JOGL Depth Buffering Not Working
body: <p>I have been using JOGL for a few days now and this is now becoming a major road block.  I can not get shapes to draw in the correct z-order, instead, they are drawn in the order they are given to OpenGL.</p>
  
  <p>I have spent the last few hours researching this and the general resolutions (and my reactions) seem to be the following:</p>
  
  <ul>
  <li><p>Make sure your frustum is correct</p>
  
  <ul>
  <li>I have double checked the frustum, it seems correct</li>
  <li>I have switched to gluLookAt instead of a custom built frustum</li>
  <li>I have switched to glOrthof just to make sure it is not perspective that is the problem.</li>
  <li>I have not even set ANY view at all, instead working in the -1, 1 range that seems to be default</li>
  </ul></li>
  <li><p>Make sure the following calls are in the init: </p>
  
  <ul>
  <li>gl.glEnable(GL.GL_DEPTH_TEST);</li>
  <li>gl.glDepthFunc(GL.GL_LEQUAL);</li>
  </ul></li>
  <li><p>Make sure you are clearing the depth buffer on each redraw</p>
  
  <ul>
  <li>gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);</li>
  </ul></li>
  </ul>
  
  <p>I have provided the very basic sample of a program below where this problem is occurring.  If you have JOGL and run it, you will see the red triangle at Z-position -0.5f ALWAYS on top while the triangles rotate around each other.  If you swap the two triangle vertex calls, the green triangle will then always be on top.</p>
  
  <p>This has been an immense headache for me so any insight will be helpful, either from JOGL or OpenGL in general, but I can't seem to see what is wrong.</p>
  
  <p>Also note that for brevity I removed the proper code to destroy the window.</p>
  
  <pre><code>import java.awt.Frame;
  
  import javax.media.opengl.GL;
  import javax.media.opengl.GL2;
  import javax.media.opengl.GLAutoDrawable;
  import javax.media.opengl.GLEventListener;
  import javax.media.opengl.awt.GLCanvas;
  import javax.media.opengl.glu.GLU;
  
  import com.jogamp.opengl.util.Animator;
  
  
  public class JOGLTest implements GLEventListener
  {
      static GLU glu = new GLU();
      static GLCanvas canvas = new GLCanvas();
      static Frame frame = new Frame("JOGL test");
      static Animator animator = new Animator(canvas);
  
      float rot = 0.0f;
  
      public void display(GLAutoDrawable glDrawable)
      {
          final GL2 gl = glDrawable.getGL().getGL2();
  
          rot++;
  
          gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  
          gl.glLoadIdentity();
  
          gl.glRotatef(rot, 0.0f, 1.0f, 0.0f);
  
          gl.glBegin(GL.GL_TRIANGLES);
  
          gl.glColor3f(0.0f, 1.0f, 0.0f);
          gl.glVertex3f(0.0f, 1.0f, 0.0f);
          gl.glVertex3f(-1.0f, -1.0f, 0.0f);
          gl.glVertex3f(1.0f, -1.0f, 0.0f);
  
          gl.glColor3f(1.0f, 0.0f, 0.0f);
          gl.glVertex3f(-1.0f, 1.0f, -0.5f);
          gl.glVertex3f(1.0f, 1.0f, -0.5f);
          gl.glVertex3f(0.0f, 0.0f, -0.5f);
  
          gl.glEnd();
      }
  
      public void dispose(GLAutoDrawable arg0)
      {
  
      }
  
      public void init(GLAutoDrawable glDrawable)
      {
          GL2 gl = glDrawable.getGL().getGL2();
          gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
          gl.glClearDepth(1.0f);
          gl.glEnable(GL.GL_DEPTH_TEST);
          gl.glDepthFunc(GL.GL_LEQUAL);
      }
  
      public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
              int arg4)
      {
  
      }
  
      public static void main(String[] args)
      {
          canvas.addGLEventListener(new JOGLTest());
          frame.add(canvas);
          frame.setSize(640, 480);
          frame.setVisible(true);
  
          animator.start();
          canvas.requestFocus();
      }
  }
  </code></pre>