On-Line Computer Graphics Notes

Shading


Calculating accurate values for the correct illumination of surfaces is possibly the most difficult task in computer graphics. In fact, the modeling of light reflection is so difficult to describe, and so computationally complex, that a great effort has gone into finding fast approximation methods to this problem.

Why is Shading a Hard Problem?

 

Consider the room in which you sit reading this document. This is a very complex scene, which is made up of several objects. Some of these objects may be considered light sources (or emitters of light energy) - perhaps your study lamp, or the fluorescent lights on the ceiling, or the window where the sun shines in, or the crack under the door if you are locked in a closet. In addition, you probably have many other objects in your room that are not usually considered to be light sources, but each of them reflects light - tables, chairs, your glasses, etc.

So, think about it! Think of rays of light or energy coming out of the light sources, bouncing around the room and finally reaching your eye. Your eye, being the remarkable instrument that it is, receives this light energy and passes it to your vision system which transmits the colors and illumination information in the scene to your brain.

Now, examine each thing a little more closely. Note that the light sources, your desk lamp for example, is emitting light from a filament inside a diffuser. Note that the table, even if it is darkly colored, reflects light in different ways (some parts of the table is brighter than other parts). Note that your hand is brighter when it is closer to the light source. We obviously have some complex things going on here.

To accurately construct a picture of this room via computer graphics, we have to simulate this illumination process and be able to calculate the shading at each point of each surface in our scene. Let's see if we can figure out how to do this.


One direct way to look at this problem is to consider an arbitrary point in space tex2html_wrap_inline388 and a point on a surface tex2html_wrap_inline390. Lets try to calculate, in a straightforward manner, the amount of illumination that arrives at tex2html_wrap_inline392 from tex2html_wrap_inline394. This quantity will be called tex2html_wrap_inline396 and is illustrated in the following figure.

tex2html_wrap488

This illumination is a combination of many things. In particular,

    [(1)]
  1. The surface on which tex2html_wrap_inline398 lies could be emitting light energy - i.e., its a light source - in which case, a certain intensity tex2html_wrap_inline400 from this source is transported from tex2html_wrap_inline402 to tex2html_wrap_inline404.

    tex2html_wrap490
  2. There may be other light sources in the scene, and other reflecting surfaces. The illumination from these objects arriving at tex2html_wrap_inline406 is scattered by the roughness of the surface. A certain amount tex2html_wrap_inline408 is scattered toward tex2html_wrap_inline410.

    tex2html_wrap492
  3. There can be other surfaces or atmospheric effects that block a portion of the illumination from reaching tex2html_wrap_inline412. This factor tex2html_wrap_inline414 (usually called the geometry factor) reduces the amount of illumination arriving at tex2html_wrap_inline416, and depends on the positions of other objects in the scene.

    tex2html_wrap494

So, in this simplified form, the illumination transported from tex2html_wrap_inline418 to tex2html_wrap_inline420 due to the above factors can be written as the sum of the emitted and scattered components, modified by the geometry factor, or
align38

This doesn't seem too bad at first glance, but we have already made several simplifications - or actually, we ignored several things. For example, we have not considered the fact that the medium between the two points could also scatter light (e.g. fog) and have not considered either wavelength (for color) or phase. We have also been somewhat lax on getting our units correct (this is physics after all). But in any case, this initial model of illumination does not seem too bad.

First glances can sometimes be decieving, and unfortunately this is the case here. Examining the scattered component further, we see that tex2html_wrap_inline422 can be calculated by considering all other sources of illumination in the scene. Consider any other point tex2html_wrap_inline424 in the scene and let's examine how light energy coming in from tex2html_wrap_inline426 could be scattered to tex2html_wrap_inline428.

tex2html_wrap496

Illumination from tex2html_wrap_inline430, incoming from tex2html_wrap_inline432, is scattered by the roughness of the surface at tex2html_wrap_inline434 and a fraction of the result is directed toward tex2html_wrap_inline436. We will write this contribution as
displaymath438
where tex2html_wrap_inline440 is the illumination transported from tex2html_wrap_inline442 to tex2html_wrap_inline444 and tex2html_wrap_inline446 is the fraction of this illumination that arrives at tex2html_wrap_inline448 and is scattered to tex2html_wrap_inline450.

If we consider all the possible points tex2html_wrap_inline452 in the scene tex2html_wrap_inline454 that transport light energy to tex2html_wrap_inline456, then tex2html_wrap_inline458 can be calculated by summing up the scattering contribution tex2html_wrap_inline460 from all possible points tex2html_wrap_inline462 in tex2html_wrap_inline464. Thus,
align41
where, of course, the sum is an integral in this case. Thus, the illumination equation takes the form
 align44
(We note that all of our illustrations are two dimensional, but in reality the picture is three dimensional. The possible directions from which illumination could impact the point tex2html_wrap_inline466 lie on a hemisphere above tex2html_wrap_inline468 with tex2html_wrap_inline470 as its center.)

Now this looks a little more difficult - but only one integral sign is not too bad. In looking at it a little further however, we note that to calculate tex2html_wrap_inline472 we must know how to calculate tex2html_wrap_inline474 for every point tex2html_wrap_inline476 in the scene. But, to calculate tex2html_wrap_inline478 we must construct another integral of this type, which depends on tex2html_wrap_inline480 for example (since tex2html_wrap_inline482 is a point in the scene). However, tex2html_wrap_inline484 depends on tex2html_wrap_inline486, which is exactly the value that we are trying to calculate!  

tex2html_wrap498

This interreflection, as is shown in the figure above, causes the quantity that we wish to calculate to appear on both sides of the equation (one is under the integral sign) - and thus makes it very difficult to solve. But, if think about it, this is exactly what is happening in the room around you - this interreflection is everywhere.

So it is a difficult task to try to solve such an equation. This hasn't however, kept the computer graphics community from trying to solve it - which in some circles is called the ``rendering equation''. But even though the speed of computer systems is increasing dramatically, the equation is still to difficult to solve without making substantial assumptions about the environment and the reflection properties of the surfaces of the scene. The biggest problem in computer graphics is that we have to do this calculation at least once for each pixel that that makes up the picture - and with potentially millions (billions?) of potential intersections, our illumination model must be able to be calculated very quickly. Of course, we also require that the picture still look realistic.


Now perhaps the reader can see that this problem is difficult and why we must address simplifications or approximations to the algorithm to be able to partially solve the illumination problem. The above model is conceptually straightforward, but is also quite difficult to solve - even though we didn't consider many quantities (phase, wavelength, scattering of light by the environment, etc.)

Simplifications of the Environment Model

If we wish to render complex scenes quickly and wish to be as realistic as possible - and we do - then we must address simplifications to, or compromises in, the illumination algorithm. But we must also be careful that even though we are creating an illumination algorithm that may be easy to calculate, we do not compromise the quality of the picture.

Thus, we must make some simplifications to either the illumination equation or the environment so that the illumination equation simplifies to where we can calculate the result in a reasonable amount of time. That can be (and has been) done in many ways - and they each lead to a different illumination algorithm, or allow the inclusion of some different features in the illumination algorithm.


Many comprimises or simplifications in the environment can be posed. Those most frequently used in computer graphics are based upon a single factor: We wish to be able to calculate the illumination equation which usually implies that the integral must be replaced by a sum, and the geometry factor must be eliminated. These simplifications can be described as follows:

  1.   Uniform Media - We assume that our media is such that no scattering occurs as light travels between objects.

    The scattering of light is virtually impossible to calculate directly and can only be simulated at a significant cost. The following figure illustrates the problems that are caused by scattering. As a light enters the media, it is scattered, frequently many times. Some of this energy will still be scattered toward the light source, but it is extremely difficult to tell through geometric means how much of the energy that leaves tex2html_wrap_inline500 actually reaches tex2html_wrap_inline502.

    tex2html_wrap518

    This simplification implies that our light rays travel directly between points and the only factors that will reduce the light energy is the inverse square law and the transparency of the objects that the light encounters.

  2.   Opaque Objects - We assume that our objects are opaque, that is light is not transmitted through the surface, nor is light refracted through the surface. Again, refraction is extremely difficult to calculate in the same way as scattering.

    This simplification implies that, if an object intercepts the light being transported from tex2html_wrap_inline504 to tex2html_wrap_inline506, then no light energy reaches the point tex2html_wrap_inline508. This, together with the first simplification implies that the geometry factor tex2html_wrap_inline510 is either zero or one.

  3.   Elimination of Interreflection - We assume that interreflection takes place in the scene, but its result is constant throughout the scene. In fact, we assume that the only reflected light that directly reaches the point tex2html_wrap_inline512 from tex2html_wrap_inline514 is reflected off the surface from the light sources of the scene.
  4.   Point Light Sources - Light sources are points, and there are only a finite number of them.

    This will simplify the task enormously and, for making simple pictures is not a significant change. It allows the replacement of the integral sign by a simple summation over the light sources.

  5.   Color Color can be accurately represented by utilizing three samples from the continuous color spectrum: red, green and blue.

These appear to be significant compromises, and in some sense they are. They imply that we cannot accurately do fog, or any technique that requires the scattering of light, and that we cannot accurately model transmission or refraction of light. However, they simplify our equation in that they imply that the geometry factor is either zero (i.e. if something is in the way, then none of the light energy reaches tex2html_wrap_inline516) or one, that our integrals become sums (as we only need to consider reflection from a finite set of point light sources), and that we do not need to consider reflection from all surfaces. We only need to calculate the illumination for the three primary colors, and find the resulting color from these results.

In reality, the computer graphics community has made inroads in all of these compromises: Interreflection is treated in the radiosity algorithms, single scattering fog models are available, surface light sources and multiple sample color models have been tried. Ray tracing methods address simple refraction. But these are still considered special cases.

The Basic Illumination Equation

The basic illumination equation utilized in computer graphics is due to Phong Bui-Tuong, and consists of three components:

The total illumination is then calculated as
displaymath83
We discuss each of these factors in the following sections.

Interreflection - Ambient Light

The difficulties with accurately calculating interreflection between objects cause a problem, and the solutions in the computer graphics community largely avoid these problems by approximating this interrefletion by a constant. That is, we assume that there is a constant amount of background light that illuminates each surface.

This factor, commonly called the ambient contribution can be modeled simply by specifying a constant for each of the red, green and blue spectra,
displaymath92
which affects all surfaces of the scene. To calculate how this light affects an individual surface, the surface color at the point is multiplied times the light values. That is, if tex2html_wrap_inline520 is the color of the object, then the ambient light reflected to the viewer from this point is
displaymath103

Diffuse Reflection - Lambert's Law

A Lambertian Surface (commonly called a perfectly diffusing surface) has the property (commonly called Lambert's Law) that the amount of light reflected from a small area toward the viewer is directly proportional to the cosine of the angle between the direction to the viewer and the normal vector.

If we have a small beam of light of width w, being reflected in the direction tex2html_wrap_inline524, and we have an intensity I per unit area on the small surface piece A, then for a Lambertian reflector we can calculate the total intensity of light that will be transported in the direction tex2html_wrap_inline530 by multiplying the intensity transported per unit surface area (tex2html_wrap_inline532 from Lambert's law), times the surface area of the small piece of surface that gives us a beam of width w (tex2html_wrap_inline536). Thus the total intensity is
displaymath122
Thus, for a Lambertian surface, the total intensity transported in a certain direction is constant, no matter what the direction (the cosines cancel).

tex2html_wrap560

This is a very convenient situation, and it means that we only need calculate the intensity of light reaching a point on the surface from the light source, and then, no matter where the point is viewed from, the intensity reflected is constant.

We can calculate the intensity of light reaching the surface by considering the following figure

tex2html_wrap562

We note that as tex2html_wrap_inline538 increases the cross section of the beam decreases - having a maximum when tex2html_wrap_inline540, and zero when tex2html_wrap_inline542. By a similar argument to the calculations above, we can conclude that if the light source has intensity tex2html_wrap_inline544, then the intensity falling on the surface must be
displaymath131
So, in the case of the lambertian surface, the diffuse reflection can be represented by
displaymath134
where tex2html_wrap_inline546 represents the fractional portion of the intensity received by the surface that is actualy reflected (we can assume that some of the light energy is absorbed and converted to heat).

A way to look at this contribution to the illumination equation is to note that the contribution along a certain direction is the length of the chord of a circle of radius tex2html_wrap_inline548, where the chord is oriented from the origin in the direction of the light source

tex2html_wrap564

Your can see from this that the full intensity of tex2html_wrap_inline550 is achieved only if tex2html_wrap_inline552 and tex2html_wrap_inline554 are identical, and that zero intensity is given when tex2html_wrap_inline556 and tex2html_wrap_inline558 are perpendicular. Thus, it is the light source that determine the contribution for the diffuse reflection.

tex2html_wrap566

and when the surface is viewed, we obtain the same value for the illumination, regardless of how we view the point.

tex2html_wrap568

Specular Reflection

Modeling surfaces only with ambient and diffuse factors in our illumination equation, gives very dull matte surfaces. Upon further examination, a direct reflection, or highlight, can be seen on the surface. This highlight is generated at those points tex2html_wrap_inline570, where the viewing direction is nearly in line with the perfect reflecting direction from the light source. The reason for this is clear if we consider our surface microscopically.

If we look closely a a surface, it can be considered to be made up from a number of ``microfacets'', each of which reflect the light: Very smooth surfaces will have microfacets whose normal vector is very close to the normal vector of the surface at tex2html_wrap_inline572, while rough surfaces will have microfacets with normals that substantially deviate from the normal at tex2html_wrap_inline574. If we consider the following figure

tex2html_wrap612

where the light approaches the point from the right, the light is scattered by these microfacets. For smooth surfaces this light is mostly scattered in the reflecting direction.

We can model this specular reflection in the same way as we modeled light reflection from Lambertian surfaces - however we cannot measure the contribution easily (recall that in the Lambertian case, we could use the chord length of a circle). In this case, the reflected intensity cannot be modeled by a circle but in reality is a surface extending along the direction of the perfect reflecting vector tex2html_wrap_inline576. If we view the point, the contribution is just the again chord length, but it is much more difficult to calculate this length.

tex2html_wrap614

Phong was the first to give a workable approximation to this ``surface''. His idea was to first measure the angle between the vector tex2html_wrap_inline578 (which is calculated as the vector tex2html_wrap_inline580 reflected from the surface) and the vector to the light source tex2html_wrap_inline582. This would give an idea of the deviation of tex2html_wrap_inline584 from the perfectly reflecting direction. If tex2html_wrap_inline586 was close to the perfectly reflecting direction, then the specular contribution should be high, while if far away, very little specular contribution should be made.

tex2html_wrap616

Thus, the quantity tex2html_wrap_inline588 would measure the variance of the vector tex2html_wrap_inline590 from the perfectly reflecting direction.

To simulate the scattering of the light by the surface, Phong realized that this dot-product measure could be focussed by raising the cosine to a power. This can be written as
displaymath144
where m is a constant that reflects the roughness of the surface. Below, we illustrate the graphs of the cosine function (m=1) and cosine raised to the 5th power (m=5) and 40th power (m=40). Note the focusing of the contribution about the value tex2html_wrap_inline600.

tex2html_wrap618

This approximation has become the defacto standard for calculation of the specular reflection. One simple change has been made to make the quantities easier to calculate. Instead of calculating tex2html_wrap_inline602, we calculate an ``ideal normal vector'' called tex2html_wrap_inline604, which is exactly halfway between the vectors tex2html_wrap_inline606 and tex2html_wrap_inline608. If the actual normal vector to the surface was equal to this ideal normal then the maximum amount of light would be reflected back to the camera. The idea then is to measure the difference between this ideal normal, and the actual normal and base the amount of reflection on this value.

tex2html_wrap620

So, the intensity specular reflection is calculated as
displaymath148
m is referred to as the glossiness constant for the surface.

Putting the Ambient, Diffuse and Specular Components Together

In a RGB color model, these values are typically put together as three equations: one for each of red, green, and blue.
displaymath152
where tex2html_wrap_inline622 are the RGB components of the color of the surface, tex2html_wrap_inline624 are the RGB components of the color of the ambient light, tex2html_wrap_inline626 is the color of the light source, and tex2html_wrap_inline628 is the resulting color displayed to the screen.

We note that the constant tex2html_wrap_inline630 from above now represents the color of the surface, and the constant tex2html_wrap_inline632 represents the color of the light source.

Below are illustrated several images of a red sphere illuminated with a light source coming in from the upper right. The first three images show the sphere under varying object colors of (0.2, 0.0, 0.0), (0.5, 0.0, 0.0) and (0.7, 0.0, 0.0) respectively.

The next three show the sphere under varying (dark to light) values of ambient or background light. This light is modeled as a shade of gray: (0.1, 0.0, 0.0), (0.3, 0.0, 0.0), and (0.5, 0.0, 0.0).

The final three illustrations show the sphere under varying specular values. All specular values shown are for ``white'' light ( red, green, and blue components are all equal).

Note the white spot that appears as a direct reflection from the light source. As these values decrease, the spot becomes larger, eventually being completely absorbed into the diffuse reflections.


This document maintained by Ken Joy

Comments to the Author

All contents copyright (c) 1996, 1997
Computer Science Department,
University of California, Davis
All rights reserved.



Ken Joy
Fri May 2 15:23:25 PDT 1997