# Shadow Volume

[1] suggests the modified robust shadow volume algorithm to improve the existing shadow volume algorithms like Z-Pass and Z-Fail.

## Z-Pass & Z-Fail Algorithms

The existing algorithms use the stencil test to create shadow volume for each object. Both algorithms require to render the depth only of the scene at first. Based on the written depth values, each algorithm uses the stencil test while rendering each object to create shadow volume. Note that the stencil test happens before the depth test in the rendering pipeline. First, Z-Pass algorithm is as follows:

- if the depth test passes while rendering the
*back*facing polygons of the shadow volume,*decrement*the value in the stencil buffer. - if the depth test passes while rendering the
*front*facing polygons of the shadow volume,*increment*the value in the stencil buffer.

However, this algorithm suffers an issue when rendering the scene inside shadow. To resolve this problem, Z-Fail algorithm can be used as in [2]:

- if the depth test fails while rendering the
*back*facing polygons of the shadow volume,*increment*the value in the stencil buffer. - if the depth test fails while rendering the
*front*facing polygons of the shadow volume,*decrement*the value in the stencil buffer.

Importantly, the both caps of shadow volume must be rendered in Z-Fail algorithm while Z-Pass algorithm does not need to. To accomplish both algorithms, a geometry shader is usually used and the triangle adjacency drawing mode is required when drawing an object to find if the adjacent triangles are the silhouette edge of the object.

## Robust Shadow Volume Algorithm

The method [1] suggests is similar with the existing algorithms but has a small modification.

- extrude edges that do not have any neighbor polygons at all for non-closed meshes like a single triangle.
- consider not only polygons facing the light, but also ones not facing the light when creating shadow volume.

This robust modification is needed to render the open mesh correctly as the above figure shows. So the existing algorithms early returns for the non-light facing polygons, but this modification simply inverts this polygon and creates shadow volume. Consequently, this new approach is doing twice the work of the non-robust algorithms. The red points (virtually edges) in the above figure represent this.

## Reference

[2] https://ogldev.org/www/tutorial40/tutorial40.html