1
votes

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?

2

2 Answers

2
votes

Sugu Lee,

I've run into the exact same problem, it turns out that the texture filtering was the problem. Try GL_NEAREST as shown below.

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT32F, texWidth, texHeight,
    0, GLES20.GL_DEPTH_COMPONENT, GLES20.GL_FLOAT, null);
GLES30.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES30.GL_TEXTURE_COMPARE_MODE, GLES20.GL_NONE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);

Note: I'm using GLES 3.0 but this fix works with 2.0 also. Device: HTC m8 t-mo Android 5.0 Lollipop

0
votes

It's hard to tell without more of the setup from your application, but here are some guesses:

*Set the GL_TEXTURE_COMPARE_MODE parameter for your depth texture to GL_NONE. IF this is GL_COMPARE_R_TO_TEXTURE, it may not yield what you expect.

*You really only need the first component from the texture2D lookup, since a single value is being stored. You can try this in your frag shader:

gl_FragColor = vec4(texture2D(DepthBuffer, tex_coord).rrr, 1.0);

I'd also consider these experiments.

*Manually inspect the contents of the texture, via glReadPixels

*Copy the contents of the depth texture into a color texture, and try rendering that (I'd recommend glBlitFramebuffer, but it is not available in ES 2.0). You can use glReadPixels and then glTexImage2D.