I'm working on a simple 3D application for OpenGL ES 2.0, iOS.
What I want to do is that I wanna get depth values of the scene in the depth buffer in a fragment shader.
So, I created a depth buffer as a 2D texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
self.Dimension = IVVec3I_Make(size.x, size.y, 0);
self.BufferObjID = texture;
and attached it to the frame buffer as a depth attachment.
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, pRD.BufferObjID,0);
When I render my scene, the depth buffer is working correctly(depth test and depth write all working perfectly).
But, when I assign the depth buffer to the sampler in the shader and I render the depth buffer to the screen using full screen quad plane, it's all black. And, the shader itself doesn't have any problem because when I assign another normal texture to the sampler, it renders the texture correctly.
This is the fragment shader.
uniform sampler2D DepthBuffer;
void main()
{
highp vec2 tex_coord = vec2(gl_FragCoord.x/1024.0,gl_FragCoord.y/768.0);
tex_coord.y = 1.0-tex_coord.y;
gl_FragColor = texture2D(DepthBuffer, tex_coord);
}
When I inspected the depth buffer by capturing OpenGL ES Frame, the debugger shows the depth buffer all in white (which is correct because almost all depth values are pretty close to 1.0).
So, what could be problem here?