Sign in to follow this  
MARS_999

FBO depth texture and glGetTexImage

Recommended Posts

I am trying to copy the depth texture from my FBO to a array and save the results to a .tga file. I can get the color textures to work but not my depth texture... Is this possible to save the depth results as a greyscale texture?
glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
unsigned char *temp = new unsigned char[1024 * 1024];
waterFBO.BindDepthTexture();
glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, temp);
CTexture t = CTexture();
t.SaveTGATextureGreyScale("temp.tga", 1024, 1024, temp, 1024);
delete []temp;

Share this post


Link to post
Share on other sites
GL_LUMINANCE (I'm 99% sure) returns one value per texel. You might want to use GL_INTENSITY (even if only because I am 100% sure that it uses 1 value per texel [wink]!)

Since true color textures work, just repeat the values for the RGB channels, like this:

glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, temp); // from your code

GLubyte bgr_data[1024 * 1024 * 3];
for (int i = 0; i < 1024 * 1024; ++i)
{
bgr_data[(3*i) + 0] = temp[i];
bgr_data[(3*i) + 1] = temp[i];
bgr_data[(3*i) + 2] = temp[i];
}

save_tga_true_color("depth.tga", 1024, 1024, bgr_data); // or something

Share this post


Link to post
Share on other sites
If you have a depth texture FBO, then sure, it will work.
Also, you will lose precision if you read as ubyte. Read as uint or ushort. Save it as a DDS.

Share this post


Link to post
Share on other sites
No dice on the loop idea deavik.... And V-man I am using FBO's and they work correctly and the glGetTexImage works for the color textures of my FBO... Thanks for the help so far.

Share this post


Link to post
Share on other sites
Quote:
Original post by MARS_999
No dice on the loop idea deavik....

I was pretty sure that would have worked ... anyway when you say the save tga is "not working" with the dpeth texture, what is happening? I can predict of 2 things: a) around 1/3 of the pixel area of your saved image is filled with garbled data, and the rest is black (uninitialised), and b) the whole image is white (or maybe black).

If it is a) then the solution would be in my previous post. If b), then hte depth texture is actually blank (maybe due to lack of depth buffer precision). Have you tried binding the depth texture as a GL_INTENSITY texture and drawing a textured quad in-game to check?

Share this post


Link to post
Share on other sites
Its all black and I bet you now that I think about it that the values are 0 to 1 and thats floating point and when I save it as a unsigned char its trimmed to 0 loss of data... :) I will print the data out to a text file and see what is going on...

Share this post


Link to post
Share on other sites
Quote:
Original post by MARS_999
Its all black and I bet you now that I think about it that the values are 0 to 1 and thats floating point and when I save it as a unsigned char its trimmed to 0 loss of data... :) I will print the data out to a text file and see what is going on...

If you read back as GL_UNSIGNED_BYTE, it will give you values in the range [0, 255]. The problem in this case, is that the depth buffer does not have enough precision.

What are the near and far plane values in the pass right after which you are capturing the depth buffer? Try moving the near plane as far out as possible (and the far plane as near as possible) to get higher depth buffer precision. All depth values are clamped in a non linear range (most probably floating point (0, 1)), so you want to reduce the range of the depth buffer to get maximum precision where it is required.

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
I'm assuming you've confirmed you do get databack into your array?


Yeah that's what I was doing with the printing the values to a text file and checking to see what I got... I set the array to zeros and then copied the data into the temp array from the glGetTexImage() and I get values like 40 and 60??? Got me whats wrong, like I said my color textures in my FBO work to print out but the depth texture is a no go...

Share this post


Link to post
Share on other sites
MARS_999 if the readback does change the array values try this.

Instead of reading the values to an array of unsigned chars, get it into an array of unsigned ints. This way you can get better resolution of the depth values. Now, loop over all pixels, find the minimum and the maximum (40 and 60 in your example) and scale the rest so the minimum maps to 0 and the maximum to 1.0 (or 255).

The "problem" i think appears because you specify a near-far combination which isn't filled when you are rendering the scene. In other words, only some depth values are visible. If you don't want to scale the depth buffer, try making the view frustum fit exactly to the scene. This way you will get better distribution of depth values.

HellRaiZer

Share this post


Link to post
Share on other sites
Sorry for repeating something I already said (you could have missed it!):

Did you try drawing the depth texture on a quad and checking what it looks like in-game? What this will do (if identical to the tga) is rule out any possible errors in the tga writing code.

Share this post


Link to post
Share on other sites
Quote:
Original post by deavik
Sorry for repeating something I already said (you could have missed it!):

Did you try drawing the depth texture on a quad and checking what it looks like in-game? What this will do (if identical to the tga) is rule out any possible errors in the tga writing code.


Yeah it shows up on my quad I render to, to see the depths, as long as its not using


glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);


if I use those texture parameters I can't see the results. Now if I omit them I can see the depth buffer... Not sure why that is. The .tga saving code works, due to my rgba images save just fine. :)

You are probably onto something Hellraizer. I just decided to forget about it and move on... Lost enough hair (pulling it out) working with this headache. :) Now onto the next headache...

Share this post


Link to post
Share on other sites
Quote:

if I use those texture parameters I can't see the results. Now if I omit them I can see the depth buffer... Not sure why that is.

I think this is perfectly normal. When you specify a compare mode other than NONE, OpenGL tries to do a depth compare every time you try to read from the depth map (comparing R texcoord to depth value). When you are rendering the depth map on a 2D quad for debugging, depth values aren't correct and the compare function is returning invalid results. That's why you have to set it to NONE every time you want to visualize the depth buffer.

HellRaiZer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this