# Lecture 25 Mathematics of Lighting and Shading

Document Sample

```					Virtual University                                                     Computer Graphics

Introduction to Computer Graphics
Lecture 25

Part I

25.1 LIGHTS AND MATERIALS

In order to understand how an object's color is determined, we'll need to
understand the parts that come into play to create the final color. First, we need a
source of illumination, typically in the form of a light source in our scene. A light
has the properties of color (an rgb value) and intensity. Typically, these are
multiplied to give scaled rgb values. Lights can also have attenuation, which
means that their intensity is a function of the distance from the light to the
surface. Lights can additionally be given other properties such as a shape (e.g.,
spotlights) and position (local or directional), but that's more in the
implementation rather than the math of lighting effects. Given a source of
illumination, we'll need a surface on which the light will shine. Here's where we
get interesting effects.
Two types of phenomena are important lighting calculations.

The first is the interaction of light with the surface boundary, and the second is
the effect of light as it gets absorbed, transmitted, and scattered by interacting
with the actual material itself. Since we really only have tools for describing
surfaces of objects and not the internal material properties, light—surface
boundary interactions are the most common type of calculation we'll see used,
though we can do some interesting simulations of the interaction of light with
material internals.
Materials are typically richer in their descriptions in an effort to mimic the effects
seen in real light—material surface interactions. Materials are typically described
using two to four separate colors in an effort to catch the nuances of real-world
light—material surface interactions. These colors are the ambient, diffuse,
specular, and emissive colors, with ambient and specular frequently grouped
together, and emissive specified only for objects that generate light themselves.
The reason there are different colors is to give different effects arising from
different environmental causes. The most common lights are as follows:

a) Ambient lighting:
It is the overall color of the object due to the global ambient light level. This is the
color of the object when there's no particular light, just the general environmental
illumination. That is, the ambient light is an approximation for the global

CS602                                                                               280
Virtual University                                                       Computer Graphics

illumination in the environment, and relies upon no light in the scene. It's usually
a global value that's added to every object in a scene.

b) Diffuse lighting:
It is the color of the object due to the effect of a particular light. The diffuse light is
the light of the surface if the surface were perfectly matte. The diffuse light is
reflected in all directions from the surface and depends only on the angle of the
light to the surface normal.

c) Specular lighting:
It is the color of the highlights on the surface. The specular light mimics the
shininess of a surface, and its intensity is a function of the light's reflection angle
off the surface.

d) Emissive lighting:
When we need an object to "glow" in a scene, we can do this with an emissive
light. This is just an additional color source added to the final light of the object.
Because we're simulating an object giving off its own light; we'd still have to add
a real "light" to get an effect on objects in a scene.

Before we get into exactly what these types of lighting are, let's put it in
perspective for our purpose of writing shader code. Shading is simply calculating
the color reflected off a surface (which is pretty much what shaders do). When a
light reflects off a surface, the light colors are modulated by the surface color
(typically, the diffuse or ambient surface color). Modulation means multiplication,
and for colors, since we are using rgb values, this means component-by-
component multiplication. So for light source l with color (r1,g1,b1 shining on
surface s with color (rs,gs,bs, the resulting color r would be:

or, multiplying it out, we get

Where the resulting rgb values of the light and surface are multiplied out to get
the final color's rgb values

The final step after calculating all the lighting contributions is to add together all
the lights to get the final color. So a shader might typically do the following:

CS602                                                                                  281
Virtual University                                                    Computer Graphics

1. Calculate the overall ambient light on a surface.
2. For each light in a scene, calculate the diffuse and specular contribution
for each light.
3. Calculate any emissive light for a surface.
4. Add all these lights together to calculate the final color value.

In the real world, we get some sort of interaction (reflection, etc.) when a photon
interacts with a surface boundary. Thus we see the effects not only when we
have a transparent—opaque boundary (like airplastic), but also a transparent—
transparent boundary (like air-water). The key feature here is that we get some
visual effect when a photon interacts with some boundary between two different
materials. The conductivity of the materials directly affects how the photon is
reflected. At the surface of a conductor (metals, etc.), the light is mostly reflected.
For dielectrics (nonconductors), there is usually more penetration and
transmittance of the light. For both kinds of materials, the dispersion of the light is
a function of the roughness of the surface (Figure 1 and 2).

Figure 1: Light reflecting from a rough and smooth surface of a conductor.

CS602                                                                              282
Virtual University                                                  Computer Graphics

Figure 2: Light reflecting from a rough and smooth surface of a dielectric
showing some penetration.

The simplest model assumes that the roughness of the surface is so fine that
light is dispersed equally in all directions as shown in Figure 1, though later we'll
look at fixing this assumption. A generalization is that conductors are opaque and
dielectrics are transparent. This gets confusing since most of the dielectric
surfaces that we are interested in modeling are mixtures and don't fall into the
simple models we've described so far. Consider a thick colored lacquer surface.
The lacquer itself is transparent, but suspended in the lacquer are reflective
pigment off of which light gets reflected, bounced, split, shifted or altered before
perhaps reemerging from the surface. This can be seen in Figure 3, where the
light rays are not just reflected but bounced around a bit inside the medium
before getting retransmitted to the outside.

Figure 3: Subsurface scattering typical of pigment-saturated translucent
coatings.

Metallic paint, brushed metal, velvet, etc. are all materials for which we need to
examine better models to try to represent these surfaces. But with a little
creativity in the modeling, it's possible to mimic the effect. Figure 4 shows what
you get when you use multiple broad specular terms for multiple base colors
combined with a more traditional shiny specular term. There's also a high-
frequency normal perturbation that simulates the sparkle from a metallic flake
pigment. As we can see, we can get something that looks particularly striking
with a fairly simple model.

CS602                                                                            283
Virtual University                                                  Computer Graphics

Figure 4: A simple shader to simulate metallic paint: (a) shows the two-tone
paint shading pass; (b) shows the specular sparkle shading pass; (c) shows the
environment mapping pass; (d) shows the final composite image

The traditional model gives us a specular term and a diffuse term. We have been
able to add in texture maps to give our scenes some uniqueness, but the lighting
effects have been very simple. Shaders allow us to be much more creative with
lighting effects. As Figure 4 shows, with just a few additional specular terms, we
can bring forth a very interesting look. But before we go off writing shaders, we'll
need to take a look at how it all fits together in the graphics pipeline. And a good
place to start is by examining the traditional lighting model.

CS602                                                                            284

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 10 posted: 5/26/2010 language: English pages: 5