Jump to content
  • Advertisement
Sign in to follow this  
Khaos Dragon

OpenGL Having trouble getting stencil shadows to work in d3d9

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have implemented stencil shadows before in OpenGL and am doing pretty much the same thing in d3d9, however I am not seeing my algorithm work at all. I have verified that I am correctly rendering the front and back faces of my extruded shadow volume so I believe the problem is the way I am configuring the stencil buffer. Below is a snippet of the relevant code... if anyone can spot anything, I would greatly appreciate it.
//ZFILL PASS

//disable color buffer writing
direct3D_device->SetRenderState(D3DRS_COLORWRITEENABLE, 0x00000000);
//disable stencil buffer writing
direct3D_device->SetRenderState( D3DRS_STENCILENABLE, FALSE );
//enable depth buffer writing and testing
direct3D_device->SetRenderState( D3DRS_ZENABLE, TRUE );
direct3D_device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
	
for( int i = 0; i < objects.size(); i++ )
        objects->Render(0);

/*
    This following would be iterated over for each light in the scene, but for testing purposes we are just doing it once with a fixed light position
*/

//clear the stencil buffer
direct3D_device->Clear( 0, NULL, D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
//disable depth buffer writing but enable testing
direct3D_device->SetRenderState( D3DRS_ZENABLE, TRUE );
direct3D_device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
//disable color buffer writing
direct3D_device->SetRenderState(D3DRS_COLORWRITEENABLE, 0x00000000);
//enable stencil buffer
direct3D_device->SetRenderState( D3DRS_STENCILENABLE, TRUE );
//set stencil test to always pass
direct3D_device->SetRenderState( D3DRS_STENCILFUNC,  D3DCMP_ALWAYS );
		
//increment stencil buffer whenever stencil test passes (always) and z test passes
//since the stencil test is set to always pass, essentially it increments only when z test passes
direct3D_device->SetRenderState(   D3DRS_STENCILPASS, D3DSTENCILOP_INCR );
//cull clockwise faces (back faces in my situation)
direct3D_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

for( int j = 0; j < objects.size(); j++ ) 
       objects[j]->RenderShadowVolume();

//decrement stencil buffer whenever z test passes
direct3D_device->SetRenderState(   D3DRS_STENCILPASS, D3DSTENCILOP_DECR );
//cull counterclockwise faces (front faces in my case)
direct3D_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );

for( int j = 0; j < objects.size(); j++ ) 
        objects[j]->RenderShadowVolume();


//enable color buffer writing with additive blending
direct3D_device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
direct3D_device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
direct3D_device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE);
direct3D_device->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0000000f );
		
//Set the stencil function to pass only when it equals the ref value equals 0
direct3D_device->SetRenderState( D3DRS_STENCILFUNC, D3DCMP_EQUAL );
direct3D_device->SetRenderState( D3DRS_STENCILPASS, D3DSTENCILOP_KEEP );
direct3D_device->SetRenderState(D3DRS_STENCILREF, 0);


//cull clockwise faces for normal rendering
direct3D_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

//ACTUAL LIGHTING PASS FOR THIS LIGHT SOURCE
for( int j = 0; j < objects.size(); j++ ) 
      objects[j]->Render();



Share this post


Link to post
Share on other sites
Advertisement
Erm... I figured this out. My code is fine, its just that I am stupid. My depth stencil format was D3DFMT_D24S8 and of course its not going to work with no stencil buffer!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!