Moving along with Chapter 1 of the HLSL Development Cookbook,
we're on to handling directional lighting. I've covered most of the theory behind directional lighting previously,
so this is going to be quite brief.
To recap, in case anyone is unfamiliar with the term, a directional light is a light which illuminates the entire scene equally from a given
direction. Typically this means a light source which is so large and far away from the scene being rendered, such as the sun or moon, that any attenuation
in light intensity, via the inverse-square law or
variations in the direction from any point in the scene to the light source location are neglible. Thus, we can model a directional light simply by using a directional
vector, representing the direction of incoming light from the source, and the color of the light emitted by the light source.
Generally, we model the light hitting a surface by breaking it into two components, diffuse and specular, according to an empirical lighting equation called
the Phong reflection model. Diffuse light is the light that reflects from
a surface equally in all directions which is calculated from the angle between the surface normal vector and the vector from the surface to the light source.
Specular light is light that is reflected off of glossy surfaces in a view-dependant direction. This direction of this specular reflection is controlled by the surface normal,
the vector from the surface to the light, and the vector from the surface to the viewer of the scene, while the size and color of the specular highlights
are controlled by properties of the material being lit, the specular exponent, approximating the "smoothness" of the object, and the specular color of the material.
Many objects will reflect specular reflections in all color frequencies, while others, mostly metals, will absorb some frequencies more than others. For now,
we're only going to consider the first class of materials.
Below you can see a model being lit by directional light, in addition to the ambient lighting we used in the last example. Here, the directional light is coming downward and and from the
lower-left to the upper-right. Surfaces with normals facing more directly towards the light source are lit more brightly than surfaces which are angled partly away from the light,
while surfaces facing away from the light are lit only by ambient lighting. In addition, you can see a series of brighter spots along the back of the rabbit model, where there are specular highlights.
Full code for this example can be downloaded from my GitHub repository.