title: Depth testing in GLKit
body: <p>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.</p>
  
  <p><img src="https://i.stack.imgur.com/kb6qQ.png" alt="enter image description here"></p>
  
  <p>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.</p>
  
  <p>In my view controller</p>
  
  <pre><code>- (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];
  
  }
  </code></pre>
  
  <p>In Beam.m</p>
  
  <pre><code>-(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);
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Cheers,
     Craig</p>