Public Group

# Depth Buffer

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

## Recommended Posts

Two topics, Depth Buffers (Z-Buffer) and Stencil Buffers are particularly confusing for me. To start, I don't know the difference between the two. All I know is that the Depth Buffer allows you to use the Z axis. As for stencil buffers, I'm absolutely clueless. Could someone explain both of these? Also, concerning the D3DPRESENT_PARAMETERS structure, you have two members called AutoDepthStencilFormat and EnableAutoDepthStencil. I have no idea what either of these do NOR do I understand what the formats mean, such as D3DFMT_D16. What is the difference between a 16-bit stencil buffer, and a 32 bit one? My text book as well as MSDN are failing to help me out very well. I hope someone can explain all of this to me. Thanks!

##### Share on other sites
Here is a simplified explanation.

There are 3 buffers that store values for each pixel on the screen. The frame buffer, the Z buffer, and the stencil buffer.

Note: A "fragment" is a pixel-sized piece of the triangle that is being drawn.

The frame buffer stores the colors of the fragments being drawn. This is what is eventually shown on the screen. Whether or not the color of a fragment is stored in the frame buffer depends on the Z buffer and/or the stencil buffer.

The Z buffer stores the distance to the fragments being drawn. If the distance to the fragment being drawn is less than the stored distance, the color is stored in the frame buffer and the distance is stored in the Z buffer. This is why farther triangles appear obscured even when they are drawn after nearer triangles.

The stencil buffer works like a mask. Fragments being drawn are stored in the frame buffer and Z buffer only if the corresponding value in the stencil buffer meets a certain criteria. You might use this if you want to restrict drawing to a non-rectangular portion of the screen.

##### Share on other sites
I don't know what stencil buffers are either, but I do know a little about depth buffers (theory, not fundamentals). Depth in this case refers to the order in which the objects are drawn, more specifically, the order in which the pixels are drawn. The normal depth range is 0.0f to 1.0f, where the pixels with less depths are drawn first (or last, I don't know for sure). How does this help at all? Let's take a cube for example, and with this cube, you can rotate it on both axis (don't know what they're called though). That's cool, right? What could be wrong with that? In most cases, cubes have points that are drawn in a particular order, creating your basic triangles, and then triangles to rectangles and so on and so on. Ex: A cube has points A, B, C, D, E, F, G, H, and this is how it is drawn:
Mesh:A______________D \              |\            | | \E__________|_\H | |           | |B|_|___________|C|  \ |           \ |  \|            \|  F\_____________\GSolid:A______________D \              |\              | \E____________\H | |             |B| |           .C|  \ |             |  \|             |  F\_____________|G

That should work out nicely like that, right? Well, what happens if when you start rotating/mirroring it and you get something like this:
Mesh:E______________H \              |\            | | \A__________|_\D | |           | |F|_|___________|G|  \ |           \ |  \|            \|  B\_____________\CSolid:E______________H |              |             | | .A          | \D |             | |F|_____________|G|  \ |           \ |  \|            \|  B\_____________\C (This representation may be off)

Assuming that no ordering is assigned to those points, you will get a wierd looking inverted cube. What depth buffering does is allow you to assign those points a dynamic drawing order with a simple value, which when rendered, assigns the pixels to its coinciding depth value (using some gradiential patterns). When those pixels have the proper depths, they will be drawn in the correct order, and will hopefully look more proper than the second figure.

Sorry if I started to make less sense in the end. I got a little distracted by the text figures (fun stuff).

EDIT: Well, crap. The post submission distorted the text figures. Well, I hope it still makes some sense.
PS: Crap, beaten to it. Well, probably for the better.

##### Share on other sites
Thanks for your help guys. I still need to know what the buffer formats represent. How do you know to make your buffer 16 bit or 32 bit? What do these specify?

Thanks!

##### Share on other sites
Quote:
 Original post by MrDoomMasterI still need to know what the buffer formats represent. How do you know to make your buffer 16 bit or 32 bit? What do these specify?

The buffer format for the depth and stencil buffers is similar to the pixel format for the frame buffer. It describes the sizes and types of values stored in each buffer. D3D acts as if the depth and stencil buffers share a single buffer, though that may not actually be the case. The available formats depend on the hardware. The typical formats you might use are D3DFMT_D24S8 and D3DFMT_D24X8, and the one you choose depends on if you are going to use the stencil buffer or not. Here is a list of the formats supported by DirectX: D3DFORMAT.

Setting EnableAutoDepthStencil to TRUE tells D3D to create and manage a depth buffer and possibly a stencil buffer. AutoDepthStencilFormat is the buffer format you want. After telling D3D that you want a depth buffer (and a stencil buffer), you still have to enable Z-test and Z-write (and stencil buffer operations) by setting the appropriate render states.

##### Share on other sites
Might there be any good tutorials or books that cover the details of what you're explaining?

Thanks for your help!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 9
• 9
• 34
• 16
• 11
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015651
×