Archived

This topic is now archived and is closed to further replies.

Caesar

How to create a z-buffer

Recommended Posts

Hi, How do I turn on/setup/initialize a z-buffer (or depth buffer in other words). I can''t quite figure out. I''ve set some stuff in the D3DPRESENT_PARAMETERS namely: param.EnableAutoDepthStencil = TRUE; param.AutoDepthStencilFormat = D3DFMT_D16; I also clear the zbuffer like device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0, 0) not sure if I have done everything necessary. Have I?

Share this post


Link to post
Share on other sites
1) The setup looks ok assuming your hardware supports it - which it should. You can check with IDirect3D9::CheckDeviceFormat and IDirect3D9::CheckDepthStencilMatch.
(You can see an example of this checking in the SDK sample framework).


2) The parameters to Clear() are fine. However it's usual practice (and better for performance on many chips) to clear the render target at the same time with D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER.


3) Something you haven't done is specified what you want the device to DO with the Z buffer - there are a few render states to set:

a. Turn the Z buffer on: device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

b. Allow rendered pixels to write to the Z buffer: device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

c. Specify what logic you want for Z buffer comparisons: device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);


4) Your projection matrix also plays a large part in what values get written into the Z buffer and its effectiveness.
The near clip plane you specify should be as high as you can accept in your application (allows for more linear z performance).
The far clip plane should make sense for the scale of your world (for example if your indoor game uses metres as its scale and you set the far plane to 1000000 then you're not using the Z buffer range very well).

--
Simon O'Connor
3D Game Programmer &
Microsoft DirectX MVP


[edited by - s1ca on November 16, 2003 8:33:28 AM]

Share this post


Link to post
Share on other sites