【问题标题】:seeing through triangles in GLKit在 GLKit 中透视三角形
【发布时间】: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


【解决方案1】:

仅仅拥有一个深度缓冲区是不够的,你需要告诉 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),它确实是一种非常宝贵的方法,可以在渲染未按预期工作时弄清楚发生了什么。

【讨论】:

  • GL_TRUE 是深度掩码的默认值,GL_LESS(至少与GL_LEQUAL 一样好)是比较函数的默认值。所以只需要第一行。
  • 我添加了这些线条,但我仍然可以看穿近三角形。也许它与照明有关?并且被点亮的片段不是完全不透明的?
  • 尝试添加 glDisable(GL_BLEND) 和 glDisable(GL_CULL_FACE) 看看是否有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
相关资源
最近更新 更多