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

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

## Recommended Posts

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

[quote]

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.Parameters["AlphaTestDirection"].SetValue(-1f);
effect.Parameters["View"].SetValue(FreeCamera.ActiveCamera.View);
effect.Parameters["Projection"].SetValue(FreeCamera.ActiveCamera.Projection);
}
mesh.Draw();
}

dsstate.StencilEnable = false;

}

[/quote]

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 on other sites
there is shadow on the screenshot

##### Share on other sites
[quote name='Geri' timestamp='1306196019' post='4814814']
there is shadow on the screenshot
[/quote]

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 on other sites
I am unable to see the problem. please point it.

##### Share on other sites
[url="http://www.iimmgg.com/image/25f69bb123fe9534e7d6e791518fac40"]http://www.iimmgg.co...7d6e791518fac40[/url]

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 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 on other sites
[quote name='andreilng' timestamp='1306200407' post='4814837']
[url="http://www.iimmgg.com/image/25f69bb123fe9534e7d6e791518fac40"]http://www.iimmgg.co...7d6e791518fac40[/url]

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.
[/quote]

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 on other sites
[quote name='stromchin' timestamp='1306216647' post='4814901']
[quote name='andreilng' timestamp='1306200407' post='4814837']
[url="http://www.iimmgg.com/image/25f69bb123fe9534e7d6e791518fac40"]http://www.iimmgg.co...7d6e791518fac40[/url]

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.
[/quote]

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 on other sites
btw did you enabled zbuffer write for the shadows?

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

[code] dsstate.DepthBufferWriteEnable = true;[/code]
right?

##### Share on other sites
And also cmp to less.

may try this:

If not works, try to sort the polygons based on the distance from the camera. I suggest shell sort or quicksort.

##### Share on other sites
Thank you, Geri, i will look into it! Thanks.

##### Share on other sites

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