2014-12-13 190 views
1

我想不通爲什麼我的一些對象被渲染在彼此之上。我有深度測試。OpenGL Z訂單搞砸了

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:

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

你有一個深度緩衝? – 2014-12-13 18:55:57

+0

是的我已更新問題代碼 – TechAUmNu 2014-12-13 18:57:04

+1

@TechAUmNu:更新後的代碼仍然不顯示您有深度緩衝區。當你創建你的窗口/渲染環境或者你的FBO(如果你正在使用)時,這是你必須設置的東西。我建議您在選擇像素格式時請求24位深度緩衝區,這是現代硬件上最兼容的格式。 – 2014-12-13 19:20:08

回答

1

我已經忘了我創建窗口時,要求深度緩衝:

前:

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

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