Sign in to follow this  
dawchiks

HLSL Shadows

Recommended Posts

Hi there. I've been learning HLSL for a while, and now, I think, it's time to learn to create shadows. what is the easiest way ( the easiest technique) to create shadows using HLSL? thx.

Share this post


Link to post
Share on other sites
When I was trying to learn shadowmapping, I had a really hard time getting my head around the concepts at first. All the articles I read seemed to talk to someone who'd already got their head around the concepts. The article which really helped me get my head around it ( and it is a pretty simple concept once you get it ) was this one :

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shadow_map.php

I found Anirudh.S Shastry's article ( linked above by TheObi ) very useful for adding a screenspace blur but I couldn't get the basics down until I read Riemers one. The sample code is C# and XNA but I wasn't using C# or XNA at the time, so I think it's understandable whatever language/api/engine you're using.

Share this post


Link to post
Share on other sites
Remember that using HLSL is only a part of doing the shadow mapping. You also need to set up the various render targets, textures and render the shadow map using D3D.

I don't know how advanced you are in D3D/D3DX, but the basic things are :

1) create a texture as a render target using a floating point format (like R32F) and get its surface.

2) create a depth buffer the same size.

3) define a light position. Compute view and projection matrices for the shadow camera..start with a directional light first because its very easy. Make the view matrix just face the object you want to cast shadows, and define the projection matrix as orthagonal with a size not too big, like 2000,2000 or something.

4) Render the scene using the shadow view and projection to your floating point surface (make sure you also set the new depth buffer and clear them both). Your shader just needs to output some form of the distance from the light position to the world space position (numerous optimizations exist but the easiest is to just output length(light_pos.xyz, worldspace_pos.xyz);

5) render the final scene and project the depth texture by using the light position, view and and projection matrices...there are many examples for texture projection. Then just compute the shadow by doing th same depth calculation you did in the shadow pass in this pass, calculating the distance form the light to the object vertex. Then just compare that distance to the projected depth texture. if the shadow depth is less than the calculated distance, then make it shadowed. Normally you also need to bias the depth map a bit, to prevent "shadow acne", just multiply the depth map value by a small amount before the comparison.

I hope this is at least somewhat comprehensible.. i was very confused by shadow mapping and such when I first started to mess with it. But its really very simple; sometimes the implementation details distract from the basic theory: render two views, one from the camera view, one from the light view. What's covered up the light's view is shadowed in your camera view (if the light can't "see" something it is in shadow).

Also, be warned that shadow quality will be very bad using just the above method--much work will have to be done later to get optimal shadows.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this