Friday, May 20, 2011

Learning shadow mapping

I read through the section in RTR on shadow mapping and I think I know enough about the basics to get some kind of shadow system into the test project.  Shadow mapping involves rendering the scene's depth information from the light's perspective.  You then render the scene from the camera's perspective, detecting if a pixel is in shadow by transforming the pixel's world coordinates (passed to the pixel shader as an interpolated value from the vertex shader) to the light's clip space, projecting onto the depth map and comparing the depth of the pixel with the depth value in the depth map.  If the pixel you are drawing has a greater depth value (assuming z is directed into the screen) than the value in the depth map, it is in shadow since it is behind whatever caused that value to be placed in the depth map.

Ambient light is probably achieved by making a normal render pass, only including ambient lighting, before you do the light shadowing passes.  Now that I think about it, multiple lights are likely handled this same way; perform a set of 2 passes per light, one for creating the depth map and one for filling the color buffer.  Each lighting pass would add to the color in the color buffer.

I should note that the above is a mixture of things I have read and "filling in the blanks".  I don't have any actual experience with shadow mapping yet, so I may find that it's not quite what I am expecting.  A perfect example of this is with creating the depth map.  I assumed that an actual depth buffer was used, which would be set as a texture for the camera rendering pass.  However, it appears as though Direct3D9 does not let you access the depth buffer like that.  Instead, you need to create a texture and set it as the render target, and output depth values rather than colors from the pixel shader.

These are some resources which I found while researching shadow mapping.
Soft-Edged Shadows
Cubic Shadow Mapping in Direct3D

No comments:

Post a Comment