1
votes

I'm having trouble with rendering depth texture using frame buffer in opengl and I can not find the problem by myself.

Here are the setup:

//initialize color texture
glGenTextures(1, &color_buffer);
glBindTexture(GL_TEXTURE_2D, color_buffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_FLOAT, NULL); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); 
glBindTexture(GL_TEXTURE_2D, 0);

//initialize depth texture
glGenTextures(1, &depth_buffer);  
glBindTexture(GL_TEXTURE_2D, depth_buffer); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
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_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//bind both textures to a frame buffer
glGenFramebuffers(1, &frame_buffer); 
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_buffer, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_buffer, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

After rendering the scene to the framebuffer, I used the following codes to render the texture. When the color_buffer is used, the scene is correctly drawn. But when I use depth_buffer, the screen is all white. I'm not sure what is wrong in here. My fragment shader just use gl_FragColor = texture2D(texture_ID,texture_coord); to render. What is wrong with my codes? How can I render a depth texture?

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ***color_buffer***);
glUseProgramObjectARB( program );
glUniform1i(glGetUniformLocation(program,"img"),0);
//codes that attach the texture to a quad
1
What do you mean by "when the color buffer is used"? You put both of them in the FBO. Is that what you call "using" it? Did you also check the framebuffer completeness status? Also, stop using ARB_shader_objects. Never use glUseProgramObjectARB; that's an extension function.Nicol Bolas
When I call "glBindTexture(GL_TEXTURE_2D, ID);" If ID is color_buffer, I can render the scene. If ID is depth_buffer, I got a white screen. I expected the depth_buffer can also be displayed.user2390930
I checked the completeness of the frame buffer and no r error returns. And what should I use instead of glUseProgramARB? This is a class project and this command is there before I starts.user2390930
"I got a white screen." Fair enough. What makes you think that's not your depth buffer ;) Or, more to the point, what exactly are you rendering into it? Are you trying to linearize the depth you get from the buffer, so that it's not in the usual non-linear space?Nicol Bolas

1 Answers

4
votes

But when I use depth_buffer, the screen is all white.

My fragment shader just use gl_FragColor = texture2D(texture_ID,texture_coord); to render.

That is your depth buffer. It's not possible to be sure without knowing anything about what you have actually rendered (or about the projection matrix you use). But generally speaking, perspective projections tend to be a very skewed transform. It skews the Z to farther values, on the [0, 1] range, so most numbers will be closer to 1 than to 0.

If you want depth values in some kind of linear space, then you'll need to linearize them. That's going to be somewhat difficult without the clip-space W to multiply with. It's doable, but you'll need values from your perspective matrix to do it.