back to table of contents

Diffuse Shading
download mac os x application and source code (64k)
Written friday october 19, 2001. 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.

Theory

The method of ray tracing commonly used is called backward ray tracing because it follows the path of vision. In the real world, photons originate from a light source, Bounce off of objects and some of them bounce into our eyes. This can be modeled by forward ray tracing, but that is orders of magnitude slower. Instead of keeping track of Every photon, backward ray tracing employs mathematical models of the behavior of light that can approximate how much light travels from one place to another. Diffuse shading, Which is a very simple model upon which we will later add, models how much light would be reflected by a diffuse (matte) surface from a light source into the view plane. To calculate how much light is being diffused by an object, from a light, into the view plane in a certain ray, we need the location of intersection of that ray with an Object, the surface normal at that point and the location of the light. Diffuse surfaces reflect photons in all directions equally, so it doesnt matter what direction the ray That instigates the calculation comes from.
A surface normal is a vector that is perpendicular to a plane that is tangent to a surface at a point (fig 1.) Surface normals are usually normalized to a length of 1. It is simple to calculate the surface normal at a point on a sphere. N = i - sc, where n is the normal, I is the location of the intersection and sc is the center of the sphere. In a general case, this vector would need to be normalized by a slow square root, However, for a sphere, the normalized vector can be found more quickly: n = (i - sc) / sr, where sr is the radius of the sphere. Notice that the Length of vector n before being normalized must be the same as the radius of the sphere, so dividing it by the radius of the sphere makes its length 1. Let the location of the intersection be i. I = ro + rd * t, where ro is the origin of the ray, rd is the direction of the ray and t is the distance From the ray's origin to the intersection, which has already been calculated by the ray-object intersection function.
Diffuse shading is based on the principal that more light hits the surface of an object where the angle between the surface normal and the vector from the intersection to The light is least. The dot product of two vectors is equal to the scalar product of the cosine of the angle between those vectors and the magnitudes of the those vectors, Or a.B = cos θ * ||a|| * ||b||, where a and b are vectors, θ is the angle between them, ||a|| is the magnitude of a, etc. F(θ) = cos θ models how much Light is diffused by a surface at a point if θ is the angle between the normal at that point and the vector from that point to the light source. Basic trigonometry tells us that This function will return 1 when θ is 0 and 0 when θ is pi/2, with a smooth gradient between those values. What this means in terms of lighting is that the most light reaches a Surface when the light source is directly striking the surface and the amount decreases until it is 0 when the vector from the surface to the light and the surface normal are perpendicular. You may wish to confirm this by examining a piece of paper, which is a reasonable approximation of a perfect diffuse surface, while rotating it near a light. It is not completely black when It is perpendicular to the light because of other light sources and indirect lighting, which i will not address. The vector from the surface to the light can be calculated by subtracting the Location of the intersection from the location of the light. Dl = l - i, where dl is the direction from the intersection to the light, l is the location of the light and I is the location of the intersection. This vector must then be normalized using a square root. Dl · n = cos θ * ||dl|| * ||n||, where θ is the angle between The surface normal and the direction to the light. However, since the length of the direction to the light has been normalized to 1 and the length of the surface normal is 1, Dl · n = cos θ, or the ammount of light that is diffused from the surface at that point (a.K.A. The lighting coefficient.) The color of objects is modeled using red, green and blue components. Multiply these components by the lighting coefficient to get the diffuse shaded color of the object at a point. If the dot product calculation of the lighting coefficient comes to less than 0, the actual coefficient is 0.