FBO depth texture and glGetTexImage

Started by
12 comments, last by HellRaiZer 17 years, 11 months ago
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;

Advertisement
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 codeGLubyte bgr_data[1024 * 1024 * 3];for (int i = 0; i < 1024 * 1024; ++i){  bgr_data[(3*i) + 0] = temp;  bgr_data[(3*i) + 1] = temp;  bgr_data[(3*i) + 2] = temp;}save_tga_true_color("depth.tga", 1024, 1024, bgr_data); // or something
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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
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?
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...
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.
I am already ahead of you! :) near/far 3.0 and 75.0 :) Still no dice, all zeros. Oh well about to give up... Thanks for the help.
I'm assuming you've confirmed you do get databack into your array?
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...

This topic is closed to new replies.

Advertisement