javaopengllwjglzbuffer

OpenGL Z order messed up


I cannot figure out why some of my objects are being rendered on top of each other. I have Depth testing on.

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

Do I need to draw by order of what is closest to the camera? (I thought OpenGL did that for you.)

as you can see the cubes on the left are drawn correctly but the ones on the right are not

Setup code:

 private  void setUpStates() {
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);        
    glLightModel(GL_LIGHT_MODEL_AMBIENT, BufferTools.asFlippedFloatBuffer(new float[]{0, 0f, 0f, 1f}));         
    glLight(GL_LIGHT0, GL_CONSTANT_ATTENUATION,BufferTools.asFlippedFloatBuffer(new float[]{1, 1, 1, 1}) );

    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_DIFFUSE);   
    glMaterialf(GL_FRONT, GL_SHININESS, 50f);           
    camera.applyOptimalStates();       

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_TEXTURE_2D);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);



    glEnableClientState(GL_VERTEX_ARRAY);       
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

Render Code:

private void render() {

    // Clear the pixels on the screen and clear the contents of the depth buffer (3D contents of the scene)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Reset any translations the camera made last frame update
    glLoadIdentity();
    // Apply the camera position and orientation to the scene
    camera.applyTranslations();
    glLight(GL_LIGHT0, GL_POSITION, BufferTools.asFlippedFloatBuffer(500f, 100f, 500f, 1));        
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);       

    for(ChunkBatch cb : InterthreadHolder.getInstance().getBatches()){
        cb.draw(camera.x(), camera.y(), camera.z());
    }


}

The draw method in ChunkBatch:

    public void draw(float x, float y, float z) {
    shader.bind();
    shader.setUniform("cameraPosition", x,y,z);      
   for(ChunkVBO c : VBOs){  
        glBindBuffer(GL_ARRAY_BUFFER, c.vertexid);
        glVertexPointer(3, GL_FLOAT, 0, 0L);
        glBindBuffer(GL_ARRAY_BUFFER, c.colorid);
        glColorPointer(3, GL_FLOAT, 0, 0L);
        glBindBuffer(GL_ARRAY_BUFFER, c.normalid);
        glNormalPointer(GL_FLOAT, 0, 0L);
        glDrawArrays(GL_QUADS, 0, c.visibleFaces * 6);          
    }
    ShaderProgram.unbind();     
}

Solution

  • I had forgotten to ask for a depth buffer when creating my window:

    Before:

    Display.create(new PixelFormat(4,0,0,4));
    

    After

    Display.create(new PixelFormat(4,24,0,4));