2016-08-01 47 views
0

我正在研究一個簡單的iOS應用程序,以瞭解OpenGLES 2.0。在該項目中,我渲染了四角形的金字塔形狀,其中一些滑塊用於調整金字塔頂點的高度,以及圍繞y軸旋轉modalViewMatrix。我試圖找到爲什麼..在將這個物體逆時針旋轉到三角形出現在其他三角形前面的點後,我可以看到近三角形。但是,當以順時針方向旋轉到相同點時,近三角形是不透明的並且遮擋最遠的三角形。GLKit中的三角形透視

我認爲原因是缺乏深度渲染緩衝區,但在設置屬性view.drawableDepthFormat = GLKViewDrawableDepthFormat16;後,行爲依然存在。

作爲參考,這是我繪製完成的drawRect函數。唯一的其他代碼是在viewDidLoad和xcode項目的全局範圍內完成的here

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { 

    [self.baseEffect prepareToDraw]; 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 


    glBindBuffer(GL_ARRAY_BUFFER,pos); 



    glEnableVertexAttribArray(GLKVertexAttribPosition); 

    const GLvoid * off1 = NULL + offsetof(SceneVertex, position) ; 

    glVertexAttribPointer(GLKVertexAttribPosition,    // Identifies the attribute to use 
         3,    // number of coordinates for attribute 
         GL_FLOAT,   // data is floating point 
         GL_FALSE,   // no fixed point scaling 
         sizeof(SceneVertex),   // total num bytes stored per vertex 
         off1); 






    glEnableVertexAttribArray(GLKVertexAttribNormal); 


    const GLvoid * off2 = NULL + offsetof(SceneVertex, normal) ; 

    glVertexAttribPointer(GLKVertexAttribNormal,    // Identifies the attribute to use 
         3,    // number of coordinates for attribute 
         GL_FLOAT,   // data is floating point 
         GL_FALSE,   // no fixed point scaling 
         sizeof(SceneVertex),   // total num bytes stored per vertex 
         off2); 


    GLenum error = glGetError(); 
    if(GL_NO_ERROR != error) 
    { 
     NSLog(@"GL Error: 0x%x", error); 
    } 


    int sizeOfTries = sizeof(triangles); 
    int sizeOfSceneVertex = sizeof(SceneVertex); 

    int numArraysToDraw = sizeOfTries/sizeOfSceneVertex; 

    glDrawArrays(GL_TRIANGLES, 0, numArraysToDraw); 

} 
+0

您是否啓用深度測試和深度寫入以及在任何位置設置深度測試?此外,你可以嘗試禁用背面剔除,看看是否有所作爲(glDisable(GL_CULL_FACE)) – Columbo

+0

我寫的關於深度緩衝區的唯一線是設置drawableDepthFormat屬性。有沒有一個布爾值可以在GLKView上設置? –

回答

0

僅僅爲了擁有深度緩衝區是不夠的,您需要告訴OpenGL您想如何使用它。嘗試添加下面幾行:

glEnable(GL_DEPTH_TEST); // Enable depth testing 
glDepthMask(GL_TRUE);  // Enable depth write 
glDepthFunc(GL_LEQUAL); // Choose the depth comparison function 

雖然我們在這裏,我建議GLKViewDrawableDepthFormat24超過GLKViewDrawableDepthFormat16大多數用例(更好的精度)。

我還建議您熟悉xcode的幀捕獲功能(doc),它確實是瞭解渲染不按預期工作時發生了什麼的非常寶貴的方法。

+1

「GL_TRUE」是深度遮罩的默認值,「GL_LESS」(至少和GL_LEQUAL一樣)是比較函數的默認值。所以只需要第一行。 –

+0

我增加了這些線條,但我仍然可以看穿近三角形。也許它與照明有關?點燃的碎片不完全不透明? –

+0

嘗試添加glDisable(GL_BLEND)和glDisable(GL_CULL_FACE)以查看是否有幫助。 – Columbo