Jump to content


Depth buffer works bad


6 replies to this topic

#1 wahbi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 27 April 2005 - 02:53 PM

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).

#2 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 27 April 2005 - 09:23 PM

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.

#3 TheColonial

    New Member

  • Members
  • Pip
  • 9 posts

Posted 29 April 2005 - 06:39 AM

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.
In order to understand recursion, one must first understand recursion.

#4 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 29 April 2005 - 02:19 PM

wahbi said:

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.

#5 wahbi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 30 April 2005 - 07:31 AM

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

#6 wahbi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 30 April 2005 - 07:48 AM

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

#7 wahbi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 30 April 2005 - 07:52 AM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users