【发布时间】:2016-08-01 02:16:38
【问题描述】:
我正在开发一个简单的 iOS 应用程序来了解 OpenGLES 2.0。在项目中,我渲染了 4 个金字塔形状的三角形,并带有一些滑块来调整金字塔顶点的高度,并围绕 y 轴旋转 modalViewMatrix。我试图找出原因..在逆时针旋转这个物体到三角形出现在其他三角形前面的点之后,我可以看到近处的三角形。但是,当顺时针方向旋转到同一点时,近处的三角形是不透明的,并且会遮挡最远的三角形。
我认为原因是缺少深度渲染缓冲区,但在设置属性 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);
}
【问题讨论】:
-
您是否启用深度测试和深度写入并在任何地方设置深度测试?另外,您能否尝试禁用背面剔除,看看是否有所不同(glDisable(GL_CULL_FACE))
-
我写的关于深度缓冲区的唯一一行是设置 drawableDepthFormat 属性。在 GLKView 上是否也有一个布尔值可以设置?
标签: opengl-es glkit depth-buffer