back to table of contents

Specular Highlights, Directional and Spot Lights
download mac os x application and source code (76k)
Released tuesday february 12, 2002. Developed with apple's free developer tools on mac os 10.1
This document, images, source and application are the intellectual property of forrest briggs. You may freely use the source code i provide. Email me with questions or suggestions.

Specular Highlights
Specular highlights are what make objects look shiny. Unlike diffuse illumination, where light scatters equally when it hits a surface, specular illumination goes in a particular direction. When it goes directly into the eye, it makes a highlight. We must therefore also take the viewpoint into consideration when calculating the contribution of specular highlights. The amount of light reflected directly into the eye is proportional to The angle between the reflection of the light and the incoming ray. This is the same as the angle between the reflection off the surface of the incomming ray and the ray to the light, both Of which have already been calculated for reflections and lighting. So, s = L · R, where s = specular, L = the ray from the intersection to the light and R = the reflected ray. Negative Values of s indicate that no light is reflected. Now we raise the specular component to an exponent that represents how shiny the object is and add this value to the total contribution of the light. A large exponent will yield a small sharply focused highlight. In code:

Specular = VectorDot(reflectRayDirection, rayToLightDirection);
If(specular>0) 
 Specular = pow(specular, surface.Shininess) * surface.SpecularCoef;
Else
 Specular = 0;
 

...
ReturnColor.R += lightSources[i].Color.R * (primitive.Surface.BaseColor.R * diffuse + specular); ReturnColor.G += lightSources[i].Color.G * (primitive.Surface.BaseColor.G * diffuse + specular); ReturnColor.B += lightSources[i].Color.B * (primitive.Surface.BaseColor.B * diffuse + specular);

Directional and Spot Lights
Directional and spot lights do not differ much in implementation from point lights. The diffuse and specular components are calculated exactly the same. A directional light has no location, so we specify the direction of rays to it in advance, rather than calculating them. This eliminates a square root. Directional lights accurately model light sources like the sun that are so far away that for all intensive purposes, all rays to them are parallel. A spot light has both a location and a direction, as well as a focus, which defines how light falls off as it gets farther from the direction of the light. It is possible to make a spotlight fade gradually, but in my engine, a point will either be in the spotlight or it wont. To determine this, we get the dot product of The direction of the ray to the light and the opposite of the direction of the light (because it is facing in the wrong direction.) This is the cosine of the angle Between them. Now we compare that value to the focus. If the cosine of the angle is greater than than the focus, then the point is in the light. Focus values range from 0 to 1, with Values close to 1 indicating a sharply focused light.