Jump to content
• Advertisement

# drawing Shadows with the DepthStencilState. Need just a bit of help. [XNA 4.0]

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

Im trying to render shadows using the stencilBuffer. And... doesnt seem to work. Have a look at my code.

public void DrawShadow(Model model, Matrix world)
{

device.Clear(ClearOptions.Stencil, Color.Black, 0, 0);

dsstate.StencilEnable = true;
dsstate.DepthBufferEnable = true;
dsstate.ReferenceStencil = 0;
dsstate.StencilFunction = CompareFunction.Equal;
dsstate.StencilPass = StencilOperation.Increment;

foreach (ModelMesh mesh in model.Meshes) {
foreach (Effect effect in mesh.Effects)
{
effect.CurrentTechnique = effect.Techniques["Shadow"];
effect.Parameters["World"].SetValue(world*shadow);
effect.Parameters["AlphaTestDirection"].SetValue(-1f);
effect.Parameters["View"].SetValue(FreeCamera.ActiveCamera.View);
effect.Parameters["Projection"].SetValue(FreeCamera.ActiveCamera.Projection);
}
mesh.Draw();
}

dsstate.StencilEnable = false;

}

[/quote]

http://imageshack.us...07/shadowa.jpg/

A few more questions. How can i render the shadow of the leafs that i rendered using a 2 pass approach? I mean, using the correct alpha values.
And is this method efficient for rendering the shadows? I mean, ill set the DepthStencilState only once per draw, not for every model, of course, but the DrawShadow() code above was just for testing 1-2 objects.

#### Share this post

##### Share on other sites
Advertisement
there is shadow on the screenshot

#### Share this post

##### Share on other sites

there is shadow on the screenshot

Yes, actually... there are more than 1 shadows. And thats the problem. Zoom in the picture.
Every poly, i think, has a shadow. And they are overlapping. I want just 1 shadow, having the contour/shape of the tree.

#### Share this post

##### Share on other sites
I am unable to see the problem. please point it.

#### Share this post

##### Share on other sites
http://www.iimmgg.co...7d6e791518fac40

See the 1st half of the picture? theres only 1 shape, 1 silhouette, 1 contour.
On the second one, there are many. And they are overlapping. You can easily see that.

The 1st half is actually just like the 2nd, except i use 1 for alpha, not 0.3 or whatever. The 2nd has many semi-transparent parts, one over the other. Thing is , i want just 1 shadow, like in the 1st part, that i can make semi-transparent.

#### Share this post

##### Share on other sites
i see it now. you may need shaders to manage this. i dont know if there is any fixed function tricks to resolve it.

#### Share this post

##### Share on other sites

http://www.iimmgg.co...7d6e791518fac40

See the 1st half of the picture? theres only 1 shape, 1 silhouette, 1 contour.
On the second one, there are many. And they are overlapping. You can easily see that.

The 1st half is actually just like the 2nd, except i use 1 for alpha, not 0.3 or whatever. The 2nd has many semi-transparent parts, one over the other. Thing is , i want just 1 shadow, like in the 1st part, that i can make semi-transparent.

I'm just a noob, but can't you just make it like the first one, render it to a texture for example, and then apply that texture with alpha on the ground?
I think the idea would be to make it completely black, and then put alpha on the final result.

#### Share this post

##### Share on other sites

[quote name='andreilng' timestamp='1306200407' post='4814837']
http://www.iimmgg.co...7d6e791518fac40

See the 1st half of the picture? theres only 1 shape, 1 silhouette, 1 contour.
On the second one, there are many. And they are overlapping. You can easily see that.

The 1st half is actually just like the 2nd, except i use 1 for alpha, not 0.3 or whatever. The 2nd has many semi-transparent parts, one over the other. Thing is , i want just 1 shadow, like in the 1st part, that i can make semi-transparent.

I'm just a noob, but can't you just make it like the first one, render it to a texture for example, and then apply that texture with alpha on the ground?
I think the idea would be to make it completely black, and then put alpha on the final result.
[/quote]

Thats what is was trying, except not rendering to a texture, but using the stencil. My pixel shader for the shadow simply returns black.

#### Share this post

##### Share on other sites
btw did you enabled zbuffer write for the shadows?

#### Share this post

##### Share on other sites
yes...

 dsstate.DepthBufferWriteEnable = true;
right?

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5
• Advertisement

• 36
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631703
• Total Posts
3001814
×

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