Requirements: Direct3D 11, Visual Studio 2010
Source Code: Shadow Volumes
This is another algorithm [1], [2] which allows to cast shadows on arbitrary surfaces and handle self-shadowing of the occluding objects as well. Suppose you have a light source located at some point in space and a triangle. Now, project a ray through that point and each vertex of the triangle at the infinity. You will have an infinity pyramid. All the points within the volume of the pyramid below the triangle will be shadowed. That is why, it is often called "shadow volume".
Let us assume the camera is located outside the shadow volume and looks along the ray at some view sample. Each time the ray goes through the front-face of the shadow volume, we increment a counter. The moment it goes through the back-face of the shadow volume, that is exits it, we decrement the counter. We can claim, in the end, that view sample is illuminated if the counter is zero; otherwise, it is shadowed. To imitate ray intersection with shadow volumes, stencil buffer could be used.
The steps of the algorithm are the following:
1. Clear stencil buffer with zero values.
2. Render the whole scene writing only ambient lighting components to the color buffer and updating depth buffer.
3. Disable writing to color and depth buffer. Depth test itself still should be done. Render font and back faces of the shadow volumes incrementing and decrementing, correspondingly, values in stencil buffer.
4. Render the whole scene again with alpha blending enabled writing only diffuse and specular material-light interaction components if stencil test passes equality to zero.
The approach above demands us to generate shadow volumes for each triangle in the mesh
(three quadrilaterals per pyramid)
for all the objects in the scene and, after, render them to stencil buffer. Usually, this is very resource and time-consuming and can hardly be applied in practice.
In fact, only silhouette edges of the mesh should produce shadow volume quadrilaterals [1]. A silhouette edge is an edge which separates front-facing and back-facing triangles of the mesh toward the light source. To efficiently identify them, the mesh should have a triangle adjacency list topology. Then, geometry shader can be used to calculate normals of two neighboring triangles and direction from the edge to the light source [3]. If it gets out, indeed, to be a silhouette edge, than two vertices defining the opposite edge of the quadrilateral are generated, and the primitive is passed further to the rasterizer.
Interaction:
Press "space" button to launch/stop torus animation.
References:
1. Tomas Akenine-Möller, Eric Haines, Nathaniel Hoffman. 2008. "Real-Time Rendering"
2. Elmar Eisemann, Michael Schwarz, Ulf Assarsson, Michael Wimmer. 2012. "Real-Time Shadows"
3. Jason Zink, Matt Pettineo, Jack Hoxley. 2011. "Practical Rendering and Computation with Direct3D 11"
No comments:
Post a Comment