Jump to content
  • Advertisement

Archived

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

poly-gone

Problem with Stencil Shadows

This topic is 5216 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''ve written my first shadow volume app, and it doesn''t seem to work. The problem is that the stencil buffer and back buffer don''t seem to get cleared at all. All I see is "spots" of white and black pixels on the models. Here''s the rendering code :-
// Clear the viewport

m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL | D3DCLEAR_TARGET, 0x000000ff, 1.0f, 0L );

// Begin the scene

if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
	// Set the technique

	m_pEffect->SetTechnique( "Scene" );

	// Setup the technique variables

	m_pEffect->SetMatrix( "matWorldViewProj", &m_matWorldViewProj );
	m_pEffect->SetMatrix( "matWorldIT", &m_matWorldIT );
	m_pEffect->SetMatrix( "matProj", &m_matProj );

	m_pEffect->SetValue( "light", ((Light*)m_pLightList->GetPtr(0)), sizeof(Light) );

	// Begin the effect

	UINT uPasses = 0;
	m_pEffect->Begin( &uPasses, 0 );

	for( UINT uPass = 0; uPass < uPasses; ++uPass )
	{
		// Set the current pass

		m_pEffect->Pass( uPass );

		// Draw the geometry

		m_pMesh->DrawSubset(0);
		m_pRoom->DrawSubset(0);
	}

	// End the effect

	m_pEffect->End();

	// Setup the render states

	m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE,			FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE,		FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE,				D3DSHADE_FLAT );
	m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,				D3DCULL_NONE );

	m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,			TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_TWOSIDEDSTENCILMODE,	TRUE );

	m_pd3dDevice->SetRenderState( D3DRS_STENCILFUNC,			D3DCMP_ALWAYS );
	m_pd3dDevice->SetRenderState( D3DRS_CCW_STENCILFUNC,		D3DCMP_ALWAYS );
	
	m_pd3dDevice->SetRenderState( D3DRS_STENCILREF,				0x1 );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILMASK,			0xffffffff );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILWRITEMASK,		0xffffffff );

	m_pd3dDevice->SetRenderState( D3DRS_STENCILPASS,			D3DSTENCILOP_INCR );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILZFAIL,			D3DSTENCILOP_KEEP );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILFAIL,			D3DSTENCILOP_KEEP );

	m_pd3dDevice->SetRenderState( D3DRS_CCW_STENCILPASS,		D3DSTENCILOP_DECR );
	m_pd3dDevice->SetRenderState( D3DRS_CCW_STENCILZFAIL,		D3DSTENCILOP_KEEP );
	m_pd3dDevice->SetRenderState( D3DRS_CCW_STENCILFAIL,		D3DSTENCILOP_KEEP );
	
	// Set the technique

	m_pEffect->SetTechnique( "ShadowVolume" );

	// Setup the technique variables

	m_pEffect->SetMatrix( "matWorldViewProj", &m_matWorldViewProj );
	m_pEffect->SetMatrix( "matWorldIT", &m_matWorldIT );
	m_pEffect->SetMatrix( "matProj", &m_matProj );

	m_pEffect->SetValue( "light", ((Light*)m_pLightList->GetPtr(0)), sizeof(Light) );

	// Begin the effect

	UINT uPasses = 0;
	m_pEffect->Begin( &uPasses, 0 );

	for( UINT uPass = 0; uPass < uPasses; ++uPass )
	{
		// Set the current pass

		m_pEffect->Pass( uPass );

		// Draw the shadow volume

		m_pMeshVolume->Render( m_pd3dDevice );
	}

	// End the effect

	m_pEffect->End();
	
	// Render the fullscreen quad

	m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE,				D3DSHADE_GOURAUD );
	m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,				D3DCULL_CCW );
	m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE,			TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE,		0xF );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,			FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,		FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_TWOSIDEDSTENCILMODE,	FALSE );

	// Setup the render states

	m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,				FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,			TRUE );
	
	m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,		TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,				D3DBLEND_SRCALPHA );
	m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND,				D3DBLEND_INVSRCALPHA );
	
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1,	D3DTA_TEXTURE );
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2,	D3DTA_DIFFUSE );
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,		D3DTOP_MODULATE );
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1,	D3DTA_TEXTURE );
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2,	D3DTA_DIFFUSE );
	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,		D3DTOP_MODULATE );

	m_pd3dDevice->SetRenderState( D3DRS_STENCILREF,				0x1 );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILFUNC,			D3DCMP_LESSEQUAL );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILPASS,			D3DSTENCILOP_KEEP );
	
	m_pBigSquare->Render( m_pd3dDevice );

	// Setup the render states for rendering the mesh and room

	m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,				TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,			FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,		FALSE );
}
Any help is very much appreciated. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

  • 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!