I'm trying to replace OpenGL's gl_FragDepth feature which is missing in OpenGL ES 2.0.
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 depth texture 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.
Since iOS >= 4.1 supports GL_OES_depth_texture, I'm trying to use GL_DEPTH_COMPONENT24 or GL_DEPTH_COMPONENT16 for the depth texture. I'm using the following calls to create the texture:
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);
The framebuffer creation succeeds, but I don't know how to proceed. I'm lacking some fundamental understanding of depth textures attached to framebuffers.
- What values should I output in the fragment shader? I mean,
gl_FragColoris still an RGBA value, even though the texture is a depth texture. I cannot set the depth in the fragment shader, sincegl_FragDepthis missing in OpenGL ES 2.0 - How can I read from the depth texture in the final rendering pass, where the depth texture is attached as a
sampler2D? - Why do I get an incomplete framebuffer if I set the third argument of
glTexImage2DtoGL_DEPTH_COMPONENT16,GL_DEPTH_COMPONENT16_OESorGL_DEPTH_COMPONENT24_OES? - Is it right to attach the texture to the
GL_DEPTH_ATTACHMENT? If I'm changing that toGL_COLOR_ATTACHMENT0, I'm getting an incomplete framebuffer.