The best known effect that uses stencil buffering is real-time shadowing. How does that work? Assume that we're using a game like Quake. Once the whole screen is rendered (scenery, monsters, objects, etc.), the Z-buffer contains the depth values for all the visible pixels. Now we need to figure out how to use the stencil buffer to make objects cast shadows. This is done by starting at the light source and going around the side of the object/monster casting the shadow (check the pictures above). Just like in real life, this forms a conical region of eclipse with the peak at the object casting the shadow and the sides going away from the light source and expanding until they reach a wall. Now we want to find out which are within the area of shadow. To do this we use a simple process. If a pixel's Z-value is located between the Z-value of the front side and the Z-value of the back side of the region then it is inside the conical region. Using this comparison, the system can flag pixels as being either inside or outside the region. Let's say that pixels inside the region get a value of one in the stencil buffer. Once they are flagged, the system can render a translucent gray texture to these pixels that have a stencil value of one. In the final frame, pixels located in the region and thus in the shadow get a grayish, shadowy look. Well I hope this article has made it clear just how Z-buffering and stencil buffering works. If not, please let us know and I will have to write a more in-depth article in the future. If you have any questions feel free to ask them in our forum.