dev in the making

game development, maya and code by brainzizi

Posts Tagged ‘real time shadows

Abandoned – Shadow mapping

with 2 comments

I always wanted to implement a shadow mapping technique, ever since my first big game project called Black Sun. Black Sun was going to be a space-simulation (similar to the X series or more like Freelancer), so a lot of open spaces were involved. The most logical shadowing technique to implement in that sort of game was stencil shadows (to me than). Turns out that was a bit too complicated for my skills then (and now). I kept reading about shadow mapping and how easy was to implement it, but I just ignored that then.

Now I managed to implement my first shadow mapping technique. The official name of the procedure I implemented would be Perspective Shadow Mapping (PSM) with Percentage Closer Filter (PCF) and a screen image Gaussian Blur. The scene setup is rather simple. One directional light and the only one that casts shadows. Large ground terrain (Scale about 200 x 200 x 40) with a few models on top (Scale about 1 x 2 x 1 or smaller). The procedure goes a bit like this:

1) Render a depth map of the scene from the lights position.

This sounds very simple but is a key difference in most shadow mapping techniques. The shadow map largely depends on the type of camera you’re using to render the depth map from the light’s view. Depending on the type of projection the camera can be orthographic or perspective. Based on the amount of area it captures it can be trapezoidal, or just plain “capture all”. A lot of optimizing can be done here with the projection settings, field of view settings and the distance from the camera to your focus point. The key thing you’re looking for is depth map precision. If the focus point is too far from your camera’s position, you’ll use only a small part of the limited 32bit precision (or 64bit if your GPU supports 64bit render targets).

2) Render the depth map from your normal camera.

Not a lot you can do for optimization here.

3) Check if the distance to the light is smaller than the depth value for that pixel in the first step. If it is than light that pixel.

Here is where the PCF filter is implemented. PCF means that you take multiple depth samples from the light’s depth map, and average them out. Based on the number of samples the PCF filter can be 3×3, 5×5, 7×7, and based on the sample offsets it can be a grid PCF, rotated PCF or an irregular PCF. I used an irregular 3×3 PCF, which gave me the best results. Do be careful when comparing the depth values that they’re in the same range. Always scale down the distance with your FarClip (or FarClip – NearClip if your near clip is big enough). The reason why I used PCF 3×3 is because you can use Pixel Shader version 2.0 to implement it, while 5×5 or even 4×4 doesn’t fit in a PS2.0 ( hate the 64 instruction limit ). Or at least I didn’t manage to put them in 64 instructions.

4) Take only the lighting texture and blur it by some factor.

And you’re done with the light!

5) Render the normal scene from your normal camera and apply the blurred lighting texture on top of that.

You can optimize the algorithm in many ways. I found out that increasing the shadow depth resolution is costly, and so is blurring a 1024 x 768 texture using Gaussian blur. So I render the lighting map at half size, blur that and resize is back to 1024 x 768 giving it more blur. Also I tried to avoid complicated frustum math, so I just used a PSM with 90 FOV. More on the details and the specific shaders in the next post. Also I’m not at home now, so screen shots a bit later.


Written by brainzizizi

09.07.2009 at 15:42

Posted in xna

Tagged with , , ,