문제

I'm new to OpenGL/GLKit and having trouble with depth testing. The picture below shows a post with five beams intersecting it. They all meet in the middle (inside the post) but as you can see the parts of the beams that are meant to be obscured are still visible. They flicker like crazy and the same thing happens when I change the view so that the beams overlap on screen. If I don't glEnable(GL_CULL_FACE) then it happens within each shape i.e. the backface of the rear polygons gets drawn and interferes with the front face of the front polygon.

enter image description here

Here are the code snippets that I think are relevant, I've left out the stuff about setting up the vertexes and the viewing angle. Hopefully someone will recognise the problem and there's a silver bullet out there.

In my view controller

- (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];

}

In 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);

}

I fully admit to being very new and inexperienced at this, so I'm quite open to constructive criticism if I've structured the app poorly.

Cheers, Craig

도움이 되었습니까?

해결책

The solution came from HannesSAK (http://www.iphonedevsdk.com/forum/iphone-sdk-development/5239-opengl-es-problem-depthbuffer-culling.html) it seems that setting zNear as 0.0f was my problem. I changed it to 1.0f and the image rendered correctly.

self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.125*RADIANS, 2.0/3.0, 1.0f, 50.0f);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top