Jump to content
  • Advertisement
Sign in to follow this  
MrDoomMaster

Depth Buffer

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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\_____________\G
Solid:
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\_____________\C
Solid:
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by MrDoomMaster
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?

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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!