title: GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth
body: <p>So, I've got an imposter (the real geometry is a cube, possibly clipped, and the imposter geometry is a Menger sponge) and I need to calculate its depth.</p>
  
  <p>I can calculate the amount to offset in world space fairly easily.  Unfortunately, I've spent hours failing to perturb the depth with it.</p>
  
  <p>The only correct results I can get are when I go:</p>
  
  <pre><code>gl_FragDepth = gl_FragCoord.z
  </code></pre>
  
  <p>Basically, I need to know how gl_FragCoord.z is calculated so that I can:</p>
  
  <ul>
  <li>Take the inverse transformation from gl_FragCoord.z to eye space</li>
  <li>Add the depth perturbation</li>
  <li>Transform this perturbed depth back into the same space as the original gl_FragCoord.z.</li>
  </ul>
  
  <p>I apologize if this seems like a duplicate question; there's a number of other posts here that address similar things.  However, after implementing all of them, none work correctly.  Rather than trying to pick one to get help with, at this point, I'm asking for complete code that does it.  It should just be a few lines.</p>