Skip to main content
GameDev.net gamedev.net
🔒 Locked

How does a z-buffer work?

Started by DvDmanDT Aug 14, 2006 at 11:46 PM 10 replies 1.9k views
Original Post
DvDmanDT
DvDmanDT
I know what a z-buffer is, but what I would like to know is how it's scaled (don't know the right word for it). A ZB is usually a 16 or 24 bit int, right? So, is the number on the pixel multiplied by a constant (say, 0.1) or is it dynamic? That is, if I have the number 1645 on a pixel in the ZB, how does it know if a point on a triangle that is 234.0f l.u. away from me should be rendered there instead? The optimal way it seems, would be to take the far clipping plane - near clipping plane and divide it into 65536 pieces (for a 16-bit ZB), and use that.. Is that how it's done? Or does it keep some average of the closest and farest points in the scene over the last frames?
DvDmanDT
DvDmanDT
That would indeed be better.. I suppose now that I think of it, there could be 16 bit floats as well.. Stupid me.. :p
DvDmanDT
DvDmanDT
Any ideas on the name of the method or function for setting that in D3D?
Mastaba
Mastaba
The z value that gets stored in the z buffer is usually linearly scaled to be between 0 and 1. Typically with 0 at the near plane and 1 at the far plane. Then since the range of z is very limited, it becomes very easy to convert it to a fixed point format. It is the fixed point value that gets stored. Some z buffers store 1/z instead of or in addition to z. Some hardware may actually store z values in a proprietary floating point format.

The method you suggest of evenly dividing space into 65536 partitions, is what would be called a 16 bit w buffer.

[Edited by - Mastaba on August 15, 2006 12:00:04 AM]
DvDmanDT
DvDmanDT
Ok, thanks, that seemed somewhat closer to what I expected..

One recommended online documentation on W-buffers?

Also, what is a stencil buffer? It seems to be releated to the ZB?
Digitalfragment
Digitalfragment
A stencil buffer is another buffer to which you can peform more specific bit-logic operations (ORs, ANDs etc) to do masking and other techniques. The values are just bitplanes, they have no meaning except for what the application asks. You can specifiy rules for test, keeping and overriding (much like alpha testing and blending operations).
AndyTX
AndyTX
This is a good overview: Z-buffering.

Note that with most projection matrices, the z-buffer is *non-linearly* distributed: you get much more precision close to the "near" plane.

Also note that the values that go into the z-buffer are up to you: you control the projection matrix. OpenGL usually uses [-1, 1] and D3D [0, 1] IIRC.
Mastaba
Mastaba
Quote:
Original post by AndyTX

Note that with most projection matrices, the z-buffer is *non-linearly* distributed: you get much more precision close to the "near" plane.



What I meant by linearly was monotonically, but was having a problem picking the right word to simplify the notion.
AndyTX
AndyTX
Quote:
Original post by Mastaba
What I meant by linearly was monotonically, but was having a problem picking the right word to simplify the notion.

Ah right. Sorry for misunderstanding...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.