我对OpenGL/GLKit不熟悉,遇到了深度测试的问题。下面的图片显示了一个帖子,有五个光束穿过它。它们都在中间汇聚(在帖子里),但是你可以看到应该被遮挡的光束部分仍然可见。它们会闪烁,当我改变视角使光束在屏幕上重叠时也会出现同样的问题。如果我不启用glEnable(GL_CULL_FACE),那么它会在每个形状内发生,即后面多边形的背面会被绘制并干扰前面多边形的正面。
以下是我认为相关的代码片段,我省略了设置顶点和视角的内容。希望有人能识别出问题,并找到解决方法。
在我的视图控制器中:
在 Beam.m 文件中:
以下是我认为相关的代码片段,我省略了设置顶点和视角的内容。希望有人能识别出问题,并找到解决方法。
在我的视图控制器中:
- (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);
}
我完全承认我在这方面非常新手和没有经验,所以如果我的应用程序结构不好,我非常乐意接受建设性的批评。
谢谢, Craig