# ZBUFFER to bmp file

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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.

1. 1
Rutin
63
2. 2
3. 3
4. 4
5. 5

• 15
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633413
• Total Posts
3011757
• ### Who's Online (See full list)

There are no registered users currently online

×