What is a Depth Buffer?
How to tell if the Current Device can Support
Depth Buffers
How to Enable Depth Buffers
A depth buffer is a buffer which tells which pixels of a triangle is to be displayed depending on which triangle is in front of which triangle. Withought a depth buffer, triangles would be drawn as they were drawn. As to say, the triangle at the back of the whole scene will be the first triangle in the first primitive that was drawn.
A depth buffer can only be used on untransformed vertex formats, wether lit or not.
In DirectX 8, there are only two types of depth buffers.
Z-Buffer and W-Buffer.
Z-Buffer is fast and not VERY precise (but precise enough for
normal games)
W-Buffer is precise and slow.
Now, if you read the tutorial on Initialization of Direct3D, you'd probably notice the lines:
If vDEPTHBUFFER > 0 Then .EnableAutoDepthStencil = 1 .AutoDepthStencilFormat = vDEPTHBUFFER End If |
Where you see the "vDEPTHBUFFER", this choses what bit depth we are using. Most people will pass D3DFMT_D16 (80). This means that we will be using a 16bit depth buffer. Now let's say the user has a very poor video card... why not see if that video card has support for 16bit depth buffer first?... Most cards DO support 16bit depth buffers. But, let's say the user has a very good video card, why not see if that video card can support a 24bit depth buffer?
Well, to avoid this conflict, we will simply learn how to check if the video card supports a certain depth buffer.
If D3D.CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DTypeDevice, vFORMAT, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) = 0 Then |
Now, this causes a little problem. Where to put it? Well, this is all up to you. But I am telling you, you might have to change the code in the initialization.
The easiest process is to take out the following lines and place them right in the code (make that piece of code in-line instead of in a sub)
'InitD3D Set D3D = DX.Direct3DCreate If Err.Number Then D3DInit = Err.Number Exit Function End If |
After you'd put this in-line, then you'd do the line above these lines. (CheckDeviceFormat)
After you've initiated Direct3D and the Depth Buffer, you need to enable it. This is where you chose Z-Buffer or W-Buffer.
Disable | D3DDEV.SetRenderState D3DRS_ZENABLE, D3DZB_FALSE |
Enable Z-Buffer | D3DDEV.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE |
Enable W-Buffer | D3DDEV.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE |