   Strange Z-Buffer Errors in D3D
  Hi all,I''ve got a strange problem with my (future to be..) shadow mapping program. I''m trying to implement a system of multi-pass rendering to get shadows from multiple light sources. The theory is pretty sound I''m sure, and just to go over it this is it:1. Render entire scene to the Z buffer only2. For each light3. Render shadow volumes to stencil buffer4. where stencil==0 render scene with ONLY that light enabledThe key to all this is that when the scene is finally rendered its done additively - that is, light is additive, so the final colour will be the sum of all the lights affecting it.So my first problem was to get additive rendering working, which i did like so:   pDev->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );pDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );pDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );This seems to work, but I have a really bad problem with flickering/z-fighting  It only seems to happen from certain positions in the world, the camera automatically spins around an axis, but if you pause it it''ll either be perfectly rendered or have loads of artifacts. It almost appears as though from some angles the geometry isn''t getting rendered on subsequent passes - so all you see is the background  Now this made me think that something is up with the Z-buffer, and given that the first pass ONLY renders to the Z-Buffer (all subsequent passes to z-test, but no z-write). But I''ve tried almost every combination of Z-buffer options I can think of to no avail.Any chance someone here can spot something / has anyone else got multi-pass shadowing/additive rendering working in D3D??First part to JUST render the scene to the Z-Buffer   //clear any existing colour and depth information.//MUST CLEAR to BLACK (0,0,0) to stop it interfering with additive lightingpDev->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);//tell D3D we''re gonna start renderingpDev->BeginScene();//1. Turn off Colour Rendering, Stencil Rendering, LightingpDev->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );pDev->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ZERO );pDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );pDev->SetRenderState( D3DRS_LIGHTING, FALSE );//2. Render entire scene (Z-Fill step)mshCaster[0]->Render( pDev );mshCaster[1]->Render( pDev );mshCaster[2]->Render( pDev );//3. Turn off Z-writing, Turn on Stencil Buffer, change colour buffer to additive renderpDev->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );if I add ''D3DRS_ZENABLE, FALSE'' to the end of this code, then the flickering goes away, but all the geometry has a rather ghostly semi-transparent look! which as cool as it looks, isn''t really that useful!An example of how the point light is rendered, shadow volumes haven''t been plugged in yet, but you can see where they will eventually go:   if ( bLight_Point ) {//a. Clear Stencil BufferpDev->Clear( 0, NULL, D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0,0,0), 0.0f, 0 );//b. Render shadow volumespDev->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ZERO );pDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );//c. Turn on colour rendering, lightingpDev->SetRenderState( D3DRS_LIGHTING, TRUE );pDev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );pDev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );//d. Enable the point lightpDev->LightEnable( 0, TRUE ); //point lightpDev->LightEnable( 1, FALSE ); //directional lightpDev->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(0,0,0) );//e. Reconfigure stencil buffer (draw where stencil == 0)//f. Render entire scenemshCaster[0]->Render( pDev );mshCaster[1]->Render( pDev );mshCaster[2]->Render( pDev );}This is the little piece of code that moves the camera around, such that from certain points on the "loop" the geometry will randomly disappear as though it were failing the Z-Test   if ( bAnimateCamera ) {fCamAngle += (((float)( GetTickCount() - dwLastCameraChange )) / 1000.0f) * (float)iCameraSpeed;if ( fCamAngle > 360.0f )fCamAngle -= 360.0f;dwLastCameraChange = GetTickCount();D3DXMatrixLookAtLH( &MatView, new D3DXVECTOR3(15 + 55 * (float)sin( fCamAngle * (D3DX_PI / 180) ),50,55 * (float)cos( fCamAngle * (D3DX_PI / 180) )), new D3DXVECTOR3(15,0,0), new D3DXVECTOR3(0,1,0) );pDev->SetTransform( D3DTS_VIEW, &MatView );}Any ideas anyone?!  I can get quite far with this program - but this strange flickering really makes it look rubbish cheers,Jack   DirectX 4 VB: All you need for multimedia programming in Visual BasicFormula 1 Championship Manager, My Game Project. 
  A quick guess:When you''re doing subsequent passes, you got some z-fails for pixels due to floating point limited precision. Either try Z-bias (bad) or modify your projection matrix as described in Tom''s DX FAQ Peace,Muhammad Haggag 