Jump to content
  • Advertisement
Sign in to follow this  
SteveIDS

Shadow volumes

This topic is 4140 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

For some reason, I can't get shadow volumes to work properly. I'm testing it with a polygon with four vertices. I created the shadow volume properly - rendering it in wireframe proves it - but the light doesn't appear where it should. Here's a screenshot of the wireframe for the shadow volume: Here's the code for rendering everything:
// Depth fill rendering pass comes first
m_platform.getDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
m_platform.getDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
m_platform.getDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
m_platform.getDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);

m_platform.toggleLights(false);
m_platform.getDevice()->SetRenderState(D3DRS_ZENABLE, true);
m_platform.getDevice()->SetRenderState(D3DRS_ZWRITEENABLE, true);
m_platform.getDevice()->SetRenderState(D3DRS_STENCILENABLE, false);

// Draw polygons
/* not shown */

// Ambient lighting pass next
m_platform.getDevice()->SetRenderState(D3DRS_ZWRITEENABLE, false);
m_platform.getDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
m_platform.getDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
m_platform.getDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
m_platform.toggleLights(true);
m_platform.getDevice()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));

// Turn off all lights
for (unsigned int i = 0; i < m_lights.size(); ++i)
	m_lights.toggle(m_platform, false);

// Draw polygons
/* not shown */

// Each light gets its own iteration
for (unsigned int i = 0; i < m_lights.size(); ++i)
{
	m_platform.getDevice()->Clear(0, NULL, D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
	m_platform.getDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	m_platform.getDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
	m_platform.getDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	m_platform.toggleLights(false);
	m_platform.getDevice()->SetRenderState(D3DRS_STENCILENABLE, true);
	m_platform.getDevice()->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);

	// Render front faces
	m_platform.getDevice()->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_INCR);
	m_platform.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
	
	// Render shadow volumes, NOT regular polygons
	/* not shown */

	// Render back faces
	m_platform.getDevice()->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_DECR);
	m_platform.getDevice()->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW);

	// Render shadow volumes, NOT regular polygons
	/* not shown */

	m_platform.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	m_platform.getDevice()->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATER);
	m_platform.getDevice()->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);

	m_platform.toggleLights(true);
	m_platform.getDevice()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(0,0,0));
	for (unsigned int j = 0; j < m_lights.size(); ++j)
		m_platform.getDevice()->LightEnable(m_lights[j].getIndex(), (j == i ? true : false));

	m_platform.getDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	m_platform.getDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
	m_platform.getDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	// Draw polygons now
	/* not shown */

	m_platform.getDevice()->SetRenderState(D3DRS_STENCILENABLE, false);
}
I'm using an 8-bit stencil buffer. I followed the algorithm from this article somewhat closely (adapting it for my own needs of course). Does anyone see a potential problem with this code? Or some tips for getting shadow volumes to work? I'd appreciate any help.

Share this post


Link to post
Share on other sites
Advertisement
Found the solution. It turns out that somewhere between initializing the light and creating the shadow volume, the light's diffuse got set to 0, 0, 0. So after fixing that it works fine. [grin]

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!