OpenGL Z命令搞砸了

时间:2014-12-13 18:43:40

标签: java opengl lwjgl zbuffer

我无法弄清楚为什么我的某些物体会在彼此之上呈现。我有深度测试。

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

我是否需要按最接近相机的顺序绘制? (我以为OpenGL为你做了那件事。)

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

设置代码:

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

呈现代码:

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


}

ChunkBatch中的draw方法:

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

1 个答案:

答案 0 :(得分:1)

创建窗口时我忘了要求深度缓冲区:

在:

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

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