An Open Graphics Library® (OpenGL®) shadow is a visual approximation in a three-dimensional (3D) OpenGL® scene of how a shadow would be cast in real life under similar lighting conditions. Shadows are a complex visual feature to implement in OpenGL®, largely because there is no innate support for casting shadows in a scene. There are instead a variety of techniques that can be used to create shadows from the objects and geometry within a scene using various tools, such as the various available buffers, shaders and, in some cases, extensions that give access to special features on certain graphics cards. In most cases, the quality, accuracy and speed of OpenGL® shadow rendering is directly related to the processing power and memory of the graphics hardware present in a computer or device. Some of the more popular methods of implementing an OpenGL® shadow include shadow volumes, shadow mapping and ray tracing.
Creating an application that has OpenGL® shadow support requires mathematical knowledge and a deep understanding of how OpenGL® works. OpenGL® is largely used to create 3D scenes that are dynamic and rendered in real time, so the algorithms that are used to create shadows must not only be optimized for speed, but also must be carefully measured for their consumption of resources and any disruptions that could be caused in the graphics pipeline used for rendering. For these reasons, the most basic method of creating a shadow, called ray tracing, often is impractical for full 3D scenes, because it can take a very long time.
Ray tracing basically follows an imaginary line, or ray, from the viewing plane or position of the camera through a scene to an object’s surface and then calculates the color of the surface based on the position of one or more light sources and other factors. During the process of rasterization, where the 3D scene is drawn to a two-dimensional (2D) surface so it can be displayed, the process of ray tracing has to be performed on every pixel that is being drawn and, especially with textures or other surface properties, might need to assess the same pixels several times. This means real-time ray tracing, although it would accurately render shadows, usually is not practical.
Another method that can be used to create an OpenGL® shadow is known as shadow mapping. This method uses the depth or stencil buffer to render a scene from the angle of the light source as a series of distances from the light source to the surfaces of different objects within the scene. The scene is then rendered a second time from the correct viewing angle, checking to see whether each pixel falls within a certain distance range within the buffer, indicating whether the pixel is lit or shadowed. Using shadow mapping can be memory intensive and, with scenes that have more than one light source, can involve making multiple maps from multiple angles.
Shadow volumes are a way to create an OpenGL® shadow that is more accurate than some other methods, but it presents its own problems. A shadow volume essentially is an invisible piece of geometry in a scene that expands from the lit surface of an object away from the light source. This volume can be checked against other objects to see if they are falling into the shadow of the surface from which the volume originated. This procedure can be very complex and ultimately create visual artifacts when optimized for speed.