PDA

View Full Version : Rendering the depth buffer to a texture in a FBO



RoiiG
05-28-2011, 01:40 PM
Hello I'm trying without succes to render the depth buffer to a texture.

Here is how i set up the Texture and the FBO:




//CREATE FB TEXTURE
glGenTextures(1, &m_FBOdepth_textura);
glBindTexture(GL_TEXTURE_2D, m_FBOdepth_textura);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 1280, 720, 0,GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

//CREATE FBO AND ATTACH TEXTURE TO IT
glGenFramebuffers(1, &m_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, m_FBOdepth_textura,0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
// check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
cout<<"FBO NO OK!!"<<endl<<endl;

// switch back to window-system-provided framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);





Then my render loop looks like this:




void Test10::Render()
{


glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


m_GL_program->Use();
m_textura->Bind();
m_textura_normalmap->Bind();

//SET UNIFORMS
m_GL_program->SetUniform("textura",0);
m_GL_program->SetUniform("normal_map",1);


vec3 pos_llum = vec3(m_llum->GetPosition());
m_GL_program->SetUniform("pLight",value_ptr(pos_llum),3,1);


m_stack.LoadIdentity();
mat4 identitat = m_stack.Top();
m_GL_program->SetUniformMatrix("ModelToWorld",value_ptr(identitat),4,4,false,1);


//RENDER MESH
m_mesh->Render(m_GL_program);


//FINISH
m_GL_program->UnUse();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}



But when I check with gDEBugger the contents of the FBO texture it's completely white... anyone can see wrong things there? (If I use the default framebuffer i can see the whole mesh correctly, and the FBO completeness is OK)

Oh and another question, have I to change the fragment shader in order to render to the depth buffer? I think it's only with color_atachments.. no?

Thank you :)

BionicBytes
05-28-2011, 05:11 PM
I think this is right as the texture contains depth which is non linear and a great deal of the values will be close to 1.0 (white).
Perhaps you need to do as I did and write a shader to visualise the depth buffer by linearising the values from it.

Dark Photon
05-29-2011, 01:19 PM
But when I check with gDEBugger the contents of the FBO texture it's completely white... anyone can see wrong things there? (If I use the default framebuffer i can see the whole mesh correctly, and the FBO completeness is OK)
As BionicBytes said, looks like your code is good. Though without a complete standalone test pgm, you can never be sure.

The one thing that looked odd is your FBO only has a depth buffer, but yet you're clearing the color buffer too (?)


Oh and another question, have I to change the fragment shader in order to render to the depth buffer?
Nope. Shader doesn't change.

RoiiG
05-29-2011, 02:54 PM
I will try to render the texture in a plane but in gDEBugger it's all white.. :S

Thank you both! I will try other things and answer in a few days if I can get it to work.

RoiiG
05-30-2011, 11:45 AM
Well still i can't get it to work... >_< the depth texture is completely white.. and the scene has two cubes at diferent positions in the Z axis.

Now I tried to attach two textures one for output color and one for the depth.


glGenFramebuffers(1, &amp;m_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, m_FBOdepth_textura,0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, m_FBOcolor_textura,0);

You can see in the attached what every texture contains


I add some code to show the creation of the textures just to be sure the problem is not there:


//FRAMEBUFFERS
//CREATE FB TEXTURE

glGenTextures(1, &amp;m_FBOcolor_textura);
glBindTexture(GL_TEXTURE_2D, m_FBOcolor_textura);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1280, 720, 0,GL_RGBA,GL_UNSIGNED_BYTE , 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures(1, &amp;m_FBOdepth_textura);
glBindTexture(GL_TEXTURE_2D, m_FBOdepth_textura);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1280, 720, 0,GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);



glGenFramebuffers(1, &amp;m_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, m_FBOdepth_textura,0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, m_FBOcolor_textura,0);

//glDrawBuffer(GL_COLOR_ATTACHMENT0);
//glReadBuffer(GL_NONE);
// check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
cout<<"FBO NO OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!"<<status<<endl;



// switch back to window-system-provided framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);





Help please! :p


P.D. If all this fails, can I render the depth using a Color_attachment output and a fragment shader?

Alfonse Reinheart
05-30-2011, 12:07 PM
Besides the fact that the texture looks white in gDEbugger, what proof do you have that the depth is not being rendered correctly? As BionicBytes pointed out, it is the nature of depth textures to look white under standard visualization (ie: chopping off the lowest 16 bits and rendering it as a luminance texture). What are you doing to compensate for this fact?

RoiiG
05-30-2011, 12:19 PM
Well, I attached a screenshot showing the depth buffer texture rendered to a plane, and it's pure white. I checked in gDEBugger the numerical data of the depth buffer texture and all the pixel depth values are 255.

I don't know what i have to do to compensate that :S. It's my first time :eek: with the depth buffer :)

Alfonse Reinheart
05-30-2011, 12:46 PM
If you're concerned that the depth data may not be correct, use glReadPixels to read the depth data manually. Use GL_DEPTH_COMPONENT and GL_UNSIGNED_INT_24_8, with each pixel being an unsigned int (so allocate enough space).

RoiiG
05-30-2011, 01:45 PM
ok I will try that :) Thank you Alfonse.