1
\$\begingroup\$

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();     
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Do you remember to clear the depth buffer? glClear(GL_DEPTH_BUFFER_BIT | ... ); \$\endgroup\$
    – Nox
    Dec 13, 2014 at 18:51
  • \$\begingroup\$ Yes I do it in my render method \$\endgroup\$
    – TechAUmNu
    Dec 13, 2014 at 18:52
  • \$\begingroup\$ do you set the depth range anywhere? \$\endgroup\$
    – rfreytag
    Dec 13, 2014 at 19:51
  • \$\begingroup\$ Did you create your GL context with a depth buffer? Also, regarding "Do I need to draw by order of what is closest to the camera? (I thought OpenGL did that for you)" - it actually doesn't. GL will draw objects in the order that you send the data to it, and does no object sorting of it's own. The depth buffer (and depth test) is what will test if an incoming object is in front of or behind what's already drawn, and discard it if behind. \$\endgroup\$ Dec 13, 2014 at 21:08
  • \$\begingroup\$ @pfannkuchen_gesicht - the initial glDepthRange is {0, 1} - there's no need to set it if you don't need to change it from those values. See opengl.org/sdk/docs/man2/xhtml/glDepthRange.xml \$\endgroup\$ Dec 13, 2014 at 21:12

1 Answer 1

1
\$\begingroup\$

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));
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I did the same thing once. I bet you facepalmed a bit when you figured this out, didn't you? It would be nice if drivers spit out a warning to ARB_debug_output when you do glClear(DEPTH_BUFFER_BIT) on a context that doesn't have a depth buffer. \$\endgroup\$
    – bcrist
    Dec 14, 2014 at 5:04
  • \$\begingroup\$ Yeah its stupid that you can do that \$\endgroup\$
    – TechAUmNu
    Dec 15, 2014 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .