title: How to render to a depth texture in OpenGL ES on Android
body: <p>I need to render to a depth texture in Android, and I'm having trouble figuring it out. I've searched the web for several days and pieced together the following code:</p>
  
  <pre><code>  public void loadFrameBuffer(Context context, int resourceId) {
      final Bitmap bitmap = createBitmap(context, resourceId);
  
      // generate the framebuffer and texture object names
      glGenFramebuffers(1, frameBuffers, 0);
      glGenTextures(2, textures, 0);
  
      // bind color texture and load the texture mip level 0 texels are RGB565
      // no texels need to specified as we are going to draw into the texture
      glBindTexture(GL_TEXTURE_2D, textures[COLOR_TEXTURE]);
      texImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap, 0);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  
      // bind depth texture and load the texture mip level 0
      // no texels need to specified as we are going to draw into
      // the texture
      glBindTexture(GL_TEXTURE_2D, textures[DEPTH_TEXTURE]);
      texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, bitmap, 0);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  
      // bind the framebuffer
      glBindFramebuffer(GL_FRAMEBUFFER, frameBuffers[0]);
  
      // specify texture as color attachment
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
        GL_TEXTURE_2D, textures[COLOR_TEXTURE],
        0);
  
      checkStatus();
  
      // specify texture as depth attachment
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
        GL_TEXTURE_2D, textures[DEPTH_TEXTURE],
        0);
  
      checkStatus();
    }
  </code></pre>
  
  <p>checkStatus() for the color attachment is a success: GL_FRAMEBUFFER_COMPLETE
  however checkStatus() for the depth attachment is: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT</p>
  
  <p>The documentation for the error states:</p>
  
  <blockquote>
    <p>Not all framebuffer attachment points are framebuffer attachment
    complete. This means that at least one attachment point with a
    renderbuffer or texture attached has its attached object no longer in
    existence or has an attached image with a width or height of zero, or
    the color attachment point has a non-color-renderable image attached,
    or the depth attachment point has a non-depth-renderable image
    attached, or the stencil attachment point has a non-stencil-renderable
    image attached.</p>
    
    <p>Color-renderable formats include GL_RGBA4, GL_RGB5_A1, and GL_RGB565.
    GL_DEPTH_COMPONENT16 is the only depth-renderable format.
    GL_STENCIL_INDEX8 is the only stencil-renderable format.</p>
  </blockquote>
  
  <p>I'm at a loss, even with this description as to what is wrong. The color attachment works just fine.</p>
  
  <h1>EDIT:</h1>
  
  <p>I'm seeing this in the log just before the previously mentioned status error:</p>
  
  <p><code>&lt;core_glTexImage2D:501&gt;: GL_INVALID_OPERATION</code></p>
  
  <p>It appears to be because of this line of code:</p>
  
  <p><code>texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, bitmap, 0);</code></p>
  
  <h1>EDIT:</h1>
  
  <p>I got past the errors. I switched from the call to:</p>
  
  <p><code>texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, bitmap, 0);</code></p>
  
  <p>which is actually wrong, and now I'm calling:</p>
  
  <p><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, texWidth, texHeight, 0,
        GL_DEPTH_COMPONENT, GLES30.GL_FLOAT, null);</code></p>
  
  <p>which works. I don't see any more errors. However, I'm still having a problem. </p>
  
  <p>Here's the model rendered straight to the default buffer (straight to the window):</p>
  
  <p><img src="https://i.stack.imgur.com/HB2HP.jpg" alt="enter image description here"></p>
  
  <p>But when I try to render the texture attached to my FrameBuffer mapped to the following:</p>
  
  <pre><code>// x, y, z, S, T
        new float[]{
          // left triangle
          -1f, -1f, 0f, 0f, 0f, //
          1f, 1f, 0f, 1f, 1f, //
          -1f, 1f, 0f, 0f, 1f, //
  
          // right triangle
          -1f, -1f, 0f, 0f, 0f, //
          1f, -1f, 0f, 1f, 0f, //
          1f, 1f, 0f, 1f, 1f //
        }
      );
  </code></pre>
  
  <p>It stretches the texture:</p>
  
  <p><img src="https://i.stack.imgur.com/uImjY.jpg" alt="enter image description here"></p>
  
  <p>If I remove the left triangle and render I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/4epwR.jpg" alt="enter image description here"></p>
  
  <p>And lastly, rendering the depth texture gives me all red:</p>
  
  <p><img src="https://i.stack.imgur.com/rmLQC.jpg" alt="enter image description here"></p>