Depth buffer works bad

28af53ea00d0df1724c8caae7a177350
0
wahbi 101 Apr 27, 2005 at 14:53

I used right handed Cartesian coordinates in my Direct3D9 application.
It may be useful to show you some code for better understanding:

// attaching a Z buffer to the target surface
D3DPRESENT_PARAMETERS pp;
pp.EnableAutoDepthStencil=TRUE;
pp.AutoDepthStencilFormat=D3DFMT_D16;

// initialising states

lpD3D9Device->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE ) ;
lpD3D9Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATEREQUAL ) ;

// Calculating view and projection matrices

GetBoundingSphere(&vCenter,&fRadius); // Center and radius of the object to show.

D3DXVECTOR3 vEye=D3DXVECTOR3(0.0f,0.0f,fRadius*3.0f)+vCenter;
D3DXVECTOR3 Up=D3DXVECTOR3(0.0f,1.0f,0.0f);

D3DXMatrixLookAtRH(&matView,&vEye,
&vCenter,
&Up);

D3DXMatrixPerspectiveFovRH(&matProj,D3DX_PI/4,1.0f ,0.0f, vCenter.z-fRadius*5);

// Clearing render surface

lpD3D9Device->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

Light source is directional (towards negatif Z)

The issue is that hidden pixels are not eliminated, so the object seems to be transparent.
It works successfully if I use W buffer (D3DRS_ZENABLE, D3DZB_USEW) with some graphic cards (TNT2, RADEON 7000), but I couldn’t use it because some cards didn’t support W buffering (bad results with GEFORCE MX 4000 and FX 5200).

6 Replies

Please log in or register to post a reply.

22b3033832c5c699c856814b0cf80cb1
0
bladder 101 Apr 27, 2005 at 21:23

In your call to D3DXMatrixPerspectiveFovRH. you’re setting the z-near paraameter to 0.0. That value should be anything above 0.0. Using 0.0 causes a divide by zero in the transformation phase and that can cause problems like the one you’re having. Try changing 0.0 to 1.0 or 0.1 or something and see if that works.

5e1d89e267ccbd40fed3dff8a3de9085
0
TheColonial 101 Apr 29, 2005 at 06:39

If bladder’s point doesn’t resolve the issue for you, you should make sure that your Z buffer is “big” enough. How many polys are you rendering? You may need to use D3DFMT_D32, or D3DFMT_D24S8 in your presentation parameters instead of D3DFMT_D16 when creating your device.

99f6aeec9715bb034bba93ba2a7eb360
0
Nick 102 Apr 29, 2005 at 14:19

@wahbi

lpD3D9Device->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATEREQUAL ) ;

That should be D3DCMP_LESSEQUAL really. You only want to render pixels that are in front of each other. Direct3D’s z-axis always goes into the screen.

28af53ea00d0df1724c8caae7a177350
0
wahbi 101 Apr 30, 2005 at 07:31

Thanks Bladder,
But how do you explain that such code works using W buffer?
:blink:

28af53ea00d0df1724c8caae7a177350
0
wahbi 101 Apr 30, 2005 at 07:48

Thanks Colonial,
The real code written detects the best format (enumeration for available modes and than selection for the greater one)
:sleep:

28af53ea00d0df1724c8caae7a177350
0
wahbi 101 Apr 30, 2005 at 07:52

You are right Nick, but that didn’t resolve the problem (I just tried D3DCMP_GREATEREQUAL )
:happy: