title: Rendering to depth texture - unclarities about usage of GL_OES_depth_texture
body: <p><strong>I'm trying to replace OpenGL's <code>gl_FragDepth</code> feature which is missing in OpenGL ES 2.0.</strong></p>
  
  <p>I need a way to set the depth in the fragment shader, because setting it in the vertex shader is not accurate enough for my purpose. AFAIK the only way to do that is by having a render-to-texture framebuffer on which a first rendering pass is done. This <em>depth texture</em> stores the depth values for each pixel on the screen. Then, the depth texture is attached in the final rendering pass, so the final renderer knows the depth at each pixel.</p>
  
  <p>Since iOS >= 4.1 supports <code>GL_OES_depth_texture</code>, I'm trying to use <code>GL_DEPTH_COMPONENT24</code> or <code>GL_DEPTH_COMPONENT16</code> for the depth texture. I'm using the following calls to create the texture:</p>
  
  <pre><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureId, 0);
  </code></pre>
  
  <p>The framebuffer creation succeeds, but I don't know how to proceed. I'm lacking some fundamental understanding of depth textures attached to framebuffers.</p>
  
  <ul>
  <li><strong>What values should I output in the fragment shader?</strong> I mean, <code>gl_FragColor</code> is still an RGBA value, even though the texture is a depth texture. I cannot set the depth in the fragment shader, since <code>gl_FragDepth</code> is missing in OpenGL ES 2.0</li>
  <li><strong>How can I read from the depth texture in the final rendering pass, where the depth texture is attached as a <code>sampler2D</code>?</strong></li>
  <li><strong>Why do I get an incomplete framebuffer</strong> if I set the third argument of <code>glTexImage2D</code> to <code>GL_DEPTH_COMPONENT16</code>, <code>GL_DEPTH_COMPONENT16_OES</code> or <code>GL_DEPTH_COMPONENT24_OES</code>?</li>
  <li><strong>Is it right to attach the texture to the <code>GL_DEPTH_ATTACHMENT</code>?</strong> If I'm changing that to <code>GL_COLOR_ATTACHMENT0</code>, I'm getting an incomplete framebuffer.</li>
  </ul>