title: Fighting Z-fighting in OpenGL-ES
body: <p>I have a 3D iPhone game done with OpenGL ES.</p>
  
  <p>It's a big world but with some tiny, first-person-view bits I need to paint up close, so I can't reduce the depth range (zNear vs zFar) that glFrustumf() takes any further.</p>
  
  <p>When surfaces meet for a Z-fight, I paint them slightly apart to stop them flickering. I'm also making the camera's distance determine how far apart I adjust them, in cases where this is useful and needed.</p>
  
  <p>It's mostly OK, but there are some things whose perspective suffers by the separation, and making the separation smaller causes flicker. I'd love to paint surfaces closer together.</p>
  
  <p>Is there any way to increase the depth buffer precision, so surfaces can be closer together without a narrower depth range?</p>
  
  <p>If not, is there any other way around this?</p>
  
  <p>I'm still using OpenGL ES 1.1 in the app, but am willing to upgrade if it's worth it.</p>
  
  <p>Thanks for your help.</p>
  
  <hr>
  
  <p>Here's how I create the depth buffer...</p>
  
  <p>In init method:</p>
  
  <pre><code>// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
  glGenFramebuffersOES(1, &amp;defaultFramebuffer);
  glGenRenderbuffersOES(1, &amp;colorRenderbuffer);
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
  glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
  
  //Added depth buffer
  glGenRenderbuffersOES(1, &amp;depthRenderbuffer);
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  </code></pre>
  
  <p>In resizeFromLayer method:</p>
  
  <pre><code>// Allocate color buffer backing based on the current layer size
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
  [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
  glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &amp;backingWidth);
  glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &amp;backingHeight);
  
  //Added depth buffer
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  </code></pre>
  
  <p>Here's how I create the frustum...</p>
  
  <pre><code>const GLfloat  zNear = 2.2;
  const GLfloat  zFar = 30000;
  const GLfloat  fieldOfView = 60.0;
  GLfloat size = zNear * tanf(degreesToRadian(fieldOfView) / 2.0);
  
  if (LANDSCAPE) { //for landscape clip &amp; aspect ratio.
      //parameters are: left, right, bottom, top, near, far
      glFrustumf(-size/(backingWidth/backingHeight),
                 size/(backingWidth/backingHeight),
                 -size, size,
                 zNear, zFar);
  }
  </code></pre>