我是OpenGL/GLKit的新手,在深度测试方面遇到了麻烦。下面的图片显示了一根柱子,它有五根光束相交。它们都在中间相交(在柱子内),但正如您所看到的,原本要遮挡的光束部分仍然可见。它们疯狂地闪烁,当我改变视图,使光束在屏幕上重叠时,也会发生同样的事情。如果我不glEnable(GL_CULL_FACE),那么它发生在每个形状中,即后多边形的背面被绘制并干扰了前多边形的正面。

以下是我认为相关的代码片段,我省略了有关设置顶点和视角的内容。希望有人会认识到这个问题,并且有一个银弹在那里。
在我的视图控制器中
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self setupGL];
}
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];
self.effect.lightModelAmbientColor = GLKVector4Make(0.5, 0.5, 0.5, 1);
self.effect.colorMaterialEnabled = GL_TRUE;
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.position = GLKVector4Make(2, 4, -5, 1);
self.effect.material.shininess = 10.0;
self.effect.material.specularColor = GLKVector4Make(0.5, 0.5, 1, 1);
self.effect.lightingType = GLKLightingTypePerPixel;
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.2, 0.2, 0.2, 0.5);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.effect prepareToDraw];
[beams makeObjectsPerformSelector:@selector(draw)];
[post draw];
}在Beam.m中
-(void) draw
{
self.effect.material.shininess = 15.0;
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, triangleColours);
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, triangleNormals);
glDrawArrays(GL_TRIANGLES, 0, 48);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribColor);
glDisableVertexAttribArray(GLKVertexAttribNormal);
}我完全承认我是一个新手,在这方面经验不足,所以如果我的应用程序结构很糟糕,我会非常开放地接受建设性的批评。
干杯,克雷格
发布于 2011-12-11 10:18:53
解决方案来自HannesSAK (http://www.iphonedevsdk.com/forum/iphone-sdk-development/5239-opengl-es-problem-depthbuffer-culling.html),似乎将zNear设置为0.0f是我的问题。我将其更改为1.0f,图像渲染正确。
self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.125*RADIANS, 2.0/3.0, 1.0f, 50.0f);https://stackoverflow.com/questions/8370998
复制相似问题