Sign in to follow this  
leo77

ZBUFFER to bmp file

Recommended Posts

Hi I have a very simple problem. I want to save the ZBuffer of a scene into a bmp file. I already have a bmp_write function, so the main problem is put the float (0 to 1) information in a byte (0 to 255). I've already tryed to cast and multiply by 255, but the multiplication result was lots of 254.x numbers - and a "white screen" bmp file. Is there a way to save the ZBuffer information in a bmp grayscale file? thanks...

Share this post


Link to post
Share on other sites
It sounds like everything is going more or less right. The problem is, the Z-Buffer is not linear. It has much, much more resolution at the near plane than at the far plane.

You might try computing the reciprocal of each value and then scaling/clamping it. That could give a nicer result. What are you trying to do with the resultant grayscale image, though?

Share this post


Link to post
Share on other sites
I'll put the resultant image in a (school) persentation to ilustrate the depth of a scene to a group of grad. students...
thanks for the tip, I've solved the problem this way:
---
double cabal = 263168; //(2^18+2^10)
for (i = 0; i < SCREEN_SIZE; i++) {
for (j = 0; j < SCREEN_SIZE; ++j) {
int k = (int)(Zbuffer[i*SCREEN_SIZE+j]*cabal) - (cabal-255);
if (k < 0) { //to "adjust" the range
cabal -=1024; //2^10
i = 0;
j = 0;
continue;
}
k = 255 - k; //cause I want white for closer objects
pw[i*SCREEN_SIZE+(j+3)] = (unsigned char)k;
pw[i*SCREEN_SIZE+(j+2)] = (unsigned char)k;
pw[i*SCREEN_SIZE+(j+1)] = (unsigned char)k;
pw[i*SCREEN_SIZE+j] = (unsigned char)k;
}
}
i = bmp_write ( "ZB.bmp", SCREEN_SIZE, SCREEN_SIZE, pw, 1);
----

If someone else have a better solution, please tell me...

Share this post


Link to post
Share on other sites
leo77
If you need exact Z-Buffer info, as it is, but in RGB term, I recommend to use GL_NV_copy_depth_to_color. It is supported on almost every nVidia hardware. It represents depth information exactly as RGB absolutely lossless.

http://oss.sgi.com/projects/ogl-sample/registry/NV/copy_depth_to_color.txt

Unfortunately, for ATI cards I did the same way: I copied depth texture from screen via glCopyTexSubImage, and after I save this 24 bit depth, as it is RGB 3-bytes color. Result was the same.

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