debugger
debugger

Reputation: 11

Why can't I get values from depth texture using FBO?

I can't get depth values from FBO's depth texture.. Program's workflow is like below :

Setup
1. Make pass1 FBO (color texture + depth texture)
2. Make pass2 RBO (color renderbuffer + depth renderbuffer)

Rendering
1. only vertex processing (pass1)
2. get depth values from pass0 FBO's depth texture
3. vertex and fragment processing using a previous depth values

Setup.1 code (create pass0 fbo)

glActiveTexture(GL_TEXTURE0);
glGenTextures(2, &pass1_tex);
glBindTexture(GL_TEXTURE_2D, pass1_tex[0]);
glTexImage2D(GL_TEXTURE_2D, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pass1_tex[1]);
glTexImage2D(GL_TEXTURE_2D, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glGenFramebuffers(1, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMEMNT0, GL_TEXTURE_2D< pass1_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMEMNT, GL_TEXTURE_2D< pass1_tex[0], 0);

And to get depth values in rendering, I did like below :

glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
// vertex processing..
glActiveTexture(GL_TEXTURE1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, &debug_buf[0]);

But, there are only 1.0 or 0.0 in debug_buf array. Of course, all objects are between 1.0 and -1.0 distance (NDC), a near and far value is 1.0 and 2.0 in gluPerspective().

Why can't I get proper depth values from FBO's depth texture?

Upvotes: 1

Views: 3775

Answers (2)

Jivi
Jivi

Reputation: 11

glBindFramebuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMEMNT0, GL_TEXTURE_2D< pass1_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMEMNT, GL_TEXTURE_2D< pass1_tex[0], 0);

you used the color texture, pass1_tex[0] for both the color and depth buffers

Upvotes: 1

Razzupaltuff
Razzupaltuff

Reputation: 2311

You can find the answer to your problem in this article. When using GL_COMPARE_R_TO_TEXTURE, sampling the depth texture returns a comparison result of the depth texture values against a reference value (which is the interpolated r texture coordinate). What you want to do is to specify the following texture parameters instead:

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);   

Upvotes: 2

Related Questions