Introduction
Forward lighting is a very common method to calculate the interaction between the various light sources and the other elements in the scene, such as meshes and particle systems. Forward lighting method has been around from the fixed pipeline days (when programmable shaders were just an insightful dream) till today, where it gets implemented using programmable shaders.
From a high-level view, this method works by drawing every mesh once for each light source in the scene. Each one of these draw calls adds the color contribution of the light to the final lit image shown on the screen. Performance wise, this is very expensive—for a scene with N lights and M meshes, we would need N times M draw calls. The performance can be improved in various ways. The following list contains the top four commonly used optimizations:
Warming the depth buffer with all the fully opaque meshes (that way, we don't waste resources on rendering pixels that get overwritten by other pixels closer to the camera).
Skip light sources and scene elements that are not visible to the camera used for rendering the scene.
Do bounding tests to figure which light affects which mesh. Based on the results, skip light/mesh draw calls if they don't intersect.
Combine multiple light sources that affect the same mesh together in a single draw call. This approach reduces the amount of draw calls as well as the overhead of preparing the mesh information for lighting.
Rendering the scene depths, as mentioned in the first method, is very easy to implement and only requires shaders that output depth values. The second and third methods are implemented on the CPU, so they won't be covered in this book. The fourth method is going to be explained at the end of this chapter. Since each one of these methods is independent from the others, it is recommended to use all of them together and gain the combined performance benefit.
Although this method lost its popularity in recent years to deferred lighting/shading solutions (which will be covered in the next chapter) and tiled lighting due to their performance improvement, it's still important to know how forward lighting works for the following reasons:
Forward lighting is perfect for lighting scene elements that are not fully opaque. In fact, both deferred methods only handle opaque elements. This means that forward lighting is still needed for scenes containing translucent elements.
Forward lighting can perform well when used for low-quality rendering tasks, such as low-resolution reflection maps.
Forward lighting is the easiest way to light a scene, which makes it very useful for prototyping and in cases where real-time performance is not important.
All the following recipes are going to cover the HLSL side of the rendering. This means that you, the reader, will need to know how to do the following things:
Compile and load the shaders
Prepare a system that will load and manage the scene
Prepare a framework that supports Direct3D draw calls with shaders that will render the scene
All vertex buffers used with this technique must contain both positions and normals. In order to achieve smooth results, use smooth vertex normals (face normals should be avoided).
In addition, the pixel shader has to come up with a per-pixel color value for the rendered meshes. The color value may be a constant per mesh color or can be sampled from a texture.