javagraphicsjogldepth-buffer

JOGL Depth Buffering Not Working


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.

I have spent the last few hours researching this and the general resolutions (and my reactions) seem to be the following:

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.

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.

Also note that for brevity I removed the proper code to destroy the window.

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

Solution

  • I have solved the problem over much deliberation.

    I created a GLCapabilities object and manually set the number of bits for a depth buffer while creating a GLCanvas. The code is as follows:

    GLProfile glp = GLProfile.getDefault();
    GLCapabilities caps = new GLCapabilities(glp);
    
    caps.setDepthBits(16);
    canvas = new GLCanvas(caps);