# Reading the Z buffer.

I want to read the z buffer into an array.
How do I know the dimensions x and y of the
z buffer?

you know it because you have to keep track of it when the window manager tells you.

if you’re using glut, then the function ptr you give to glutReshapeFunction() (or, whatever it’s exactly called; I can’t remember) has two parameters representing the dimension of the window that you have been given.

store it. don’t lose it. this info is important. wrap it in cotton wall and store them in a global variable, or something.

cheers
John

Thanks that makes perfect sence.

By the way how does one interpret the z value that is read. Assuming I use a
16 bit short read of the z buffer the value
returned will be 0 to 65535 so how does this map to the actual z value? Is this some scaled value of the minimum and maximum z in the current z buffer? and hence is that mapping going to be the same for every frame of the animation?

you need to convert your short into a float between 0.0 and 1.0 and then refer to the article posted by new…bie? newname, in fact, elsewhere. i just wrote an answer to it, so it can’t be too far away =)

er, to convert to a float, you divide your z value by the maximum range of the type. so, in the case of a 16 bit integer, then you’d say:

unsigned short int z;
float d;

d=(float)z/(1<<15);

cheers
John

Reading the ZBuffer is EVIL !
It’s slow, ugly and never a good idea.
You’d better avoid it as much as possible.

I wouldn’t call it evil. It’s just not a great thing for performance.

• Matt

Originally posted by bobrien:
I want to read the z buffer into an array.
How do I know the dimensions x and y of the
z buffer?

Dimensions are the same as the framebuffer dimensions (in pixels).

Evil??? I know its slow but I’m not trying to do anything real time, just a test.
an evil, evil test. slow? my lowly vanta can do depth buffer reads at 17 million pixels a second

Originally posted by john:
[b]er, to convert to a float, you divide your z value by the maximum range of the type. so, in the case of a 16 bit integer, then you’d say:

unsigned short int z;
float d;

d=(float)z/(1<<15);

cheers
John[/b]

Great nice and simple but then must I scale this by the current scale factor? How else would this be converted into the real co-ordinates?

check out the thread:
http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/002423.html

since you know v’ (and the corresponding z’ and w’, where w’==1.0) and P, then you can compute z and w.

cheers
John