Title: Part IX Lighting
1 Part IX Lighting Shading
2Lighting and its Diffuse Component
- Rasterization stage has to compute a color for an
object point through lighting or illumination
which describes the interaction between light
sources and objects. - We need a model for lighting, and a lighting
model is usually divided into diffuse, specular
and ambient components. - The diffuse component is based on Lamberts law,
which states that reflections from the ideally
diffuse (totally matte) surface (e.g. chalk) are
scattered with equal intensity in all directions,
independently of the viewing direction. The
reflected light is determined by the cosine of
the angle ? between the surface normal n and the
light vector l.
E
n
l
(n and l are unit vectors.)
3Diffuse Component
- If ? gt90o, cos? nl lt 0.
- In reality, however, the reflected light should
become 0, not a negative value, because the
object point does not receive light at all. - So, instead of nl, use max(0, nl).
n
n
l
l
4Interaction between Colored Lights and Surfaces
- A colored light has RGB components.
- Example 1 Suppose a white light sdiff (sdiff-R,
sdiff-G, sdiff-B)(1,1,1). If an object lit by
the light appears yellow, it means that the
object reflects R and G and absorbs B. We can
easily implement this kind of filtering through
material parameter mdiff, i.e. if (mdiff-R,
mdiff-G, mdiff-B)(1,1,0), then
(1,1,1)?(1,1,0)(1,1,0) where ? is component-wise
multiplication. - Example 2 If (sdiff-R sdiff-G, sdiff-B)(
0.5,0.5,0.5) and (mdiff-R, mdiff-G, mdiff-B) (
0.5,1,0.5), then (0.5,0.5,0.5)?(0.5,1,0.5)
(0.25,0.5,0.25). -
G
R
B
G
R
B
intensity .5
.5
.5
intensity 1
1
1
.5x.5.25
.5x1.5
1x11
1x00
.5x.5.25
1x11
(.5,1,.5)
(1,1,0)
where ? is component-wise multiplication.
5Specular Component
- In order to make a surface look shiny by making
highlight appear, we need specular component. - For a perfectly shiny surface, the highlight is
only where ?0. - For non-perfect objects, the highlight occurs at
?0 but falls off sharply as ? increases. Such
rapid fall-off is approximated by cosm?, where m
describes the shininess of the surface - For a perfectly shiny surface, m?. (For example,
a perfect mirror.) - Objects have both diffuse and specular
properties. The diffuse component is
view-independent while the specular component is
highly view-dependent. - Wrt the light sources, sdiff and sspec should be
the same.
cos2?
cos?
cos64?
n
r
l
?
v
6Ambient Component
- The diffuse and specular components deal with
direct rays from the light source. - In real world, however, some rays emanating from
a light source may bounce off a wall and then
reach an object when the light is turned on. This
bounced light (called ambient light) is assumed
to be so scattered that there is no way to tell
its origin direction, but it disappears if a
particular light source is turned off. - The ambient component catches such indirect rays.
- The amount of ambient light incident on each
object is a constant for all surfaces and over
all directions, and reflections from the surfaces
are scattered with equal intensity in all
directions.
7.
Colored Lights and Surfaces Summary
- Every light source has three parameters ambient
color samb, diffuse color sdiff and specular
color sspec. - Some systems such as OpenGL allow different sdiff
and sspec, which might look unnatural but
provides us with flexibility. For a realistic
effect, however, sdiff and sspec should be
identical. - samb can be different. Imagine a white light in a
room with red walls. The scattered light tends to
be red. - A material consists of ambient, diffuse,
specular, shininess and emissive parameters
mamb, mdiff, mspec, m (shininess) and memi. - memi describes how much light a surface itself
(e.g. headlight) emits. - mamb and mdiff define the materials color, and
are typically similar if not identical. - In contrast, mspec is in the gray level (e.g.
white) so that highlights end up being the color
of the light source. Imagine a while light
shining on a shiny red plastic sphere. Most of
the sphere appears red, but the shiny highlight
is white.
8Lighting Equation
- The lighting is the sum of ambient, diffuse and
specular components. - The intensity of light decreases as distance from
the light increases.
9Light Sources
- So far, weve implicitly assumed a point light.
However, we may have various types of light
sources. - A directional light is positioned infinitely far
away (e.g. sun) and so all rays from the source
are parallel when arriving at an object. - A point light is at a certain position, and
radiates in all directions. - A spot light is similar to a point light, but
emits light only within the volume of a cone,
defined by a cut-off angle and a light direction.
(We may say that both the point and spot lights
are positional.)
10Directional Light vs. Positional Light
- Directional lights are described by their
direction vectors, and positional lights (point
spot lights) are by their positions. - Recall that, for attenuation, we introduced spos
(the light source position), which is used for
point spot lights. - Lets also store the directional lights
direction vector into spos. - We are in homogeneous coordinates. The 4th
component of spos is often used to distinguish
between the two cases (e.g. OpenGL). Note that
w0 implies a vector and w?0 implies a point. - Also note that attenuation is disabled for a
directional light. - So far, parameters for defining light sources are
samb, sdiff, sspec and spos.
11Spot Light
- The spot light is another positional light, and
spos is used to define its position. - In addition to samb, sdiff, sspec and spos, a
spot light needs more parameters. - a direction vector sdir
- a cut-off angle scut
- a spot exponent sexp for fall-off control
- A factor denoted cspot can describe the spot
light effect. - If the point to be lit is inside the spot light
cone defined by scut, - If outside,
- cspot 0
12Phong Lighting Equation
- The lighting equation is extended as follows.
- itot cspot( d( iamb idiff ispec ) )
where cspot1 if not a spot light source - The parameter memi describes how much light a
surface emits. - itot memi cspot( d( iamb idiff ispec ) )
- There is also a global ambient light source
parameter aglob. - There can be multiple light sources
- After lighting computations, resulting color
values outside the range 0,1 are clamped to the
range 0,1. - The above is called Phong lighting model.
13Example Model of A Scene
14Example of Ambient Reflection Only
15Application of Lighting Model for Shading
- Shading of a polygon mesh
- The polygon mesh could be shaded by applying the
Phong lighting model for every (pixel)
position. - Two problems
- expensive
- may not be able to avoid faceted appearance
- Before talking about the faceted appearance,
lets recall that a polygon mesh is an
approximation of a smooth surface.
16Why Faceted Appearance?
- Without loss of generality, suppose only the
diffuse reflection of a directional light source
where a uniform light vector l is used for
lighting all polygons. - Lets consider 3 adjacent polygons which have 3
points p1, p2 and p3, respectively. - Because the intensity is proportional to cos?
nl, p1 is brighter than p2, and p2 is brighter
than p3. - All points in a polygon have the same normal n,
and so same intensity. - Therefore, faceted appearance is inevitable.
n2
n3
n1
l
l
l
medium
light
dark
17Flat Shading
- If we use a single normal for a face (as in the
previous example), its called flat/constant
shading. - Flat shading is fast, and in fact many of old
games such as Virtual Fighter I used flat shading
for rendering speedup.
polygon mesh faceted image
smooth image
current state what we
want
18Example of Flat Shading with Diffuse Reflection
19Normals of the Polygon Mesh
- Noting again that a polygon mesh is an
approximation of a smooth surface, consider the
triangle marked by bold lines. - The triangle corresponds to a patch of the sphere
surface, and so all points of the triangle should
have different normals, which would lead to
different intensities/colors. However, the flat
shading assumes a uniform normal for the entire
triangle and leads to a faceted image. - On the other hand, consider a vertex shared by
multiple triangles. It also corresponds to a
point of the sphere, and so its normal should be
set accordingly. - Interpolation shading techniques take the points
into account.
20Gouraud Shading
- Gouraud Shading is an color interpolation
shading. - Determine the normal of each vertex.
- Its typically given at the modeling stage.
- Otherwise, it can be obtained by averaging the
surface normals of all polygons sharing the
vertex. - Compute colors of all vertices (with Phong
lighting model). - In WdC, linearly interpolate vertex colors along
each edge of polygon. - Then, linearly interpolate pixel colors along
each scan line. - This is a magic to get smooth images from polygon
meshes.
n1
average of n1, n2, n3, n4 and n5
n5
n2
n4
n3
21Linear Interpolation of Colors
- RGB colors I1, I2 and I3 (computed at vertices)
are interpolated to determine the colors Ia at
xleft and Ib at xright for a scan line. - Then, Ia and Ib are interpolated to determine a
color Ip for each pixel for the scan line.
22Why Smooth Appearance?
- Color interpolation results in smooth transition
of lightness.
n2
n3
n1
becoming lighter along this direction
polygon mesh flat shading
Gouraud shading
23Example of Gouraud Shading with Diffuse Reflection
24X-intersection Computation (revisited)
- Recall x-intersection interpolation for polygon
filling. - Pre-computed delta value per a unit move is
repeatedly added. - Initial/starting value is computed (using similar
triangles).
i1
1
m
i
ymxB
1
1/m
new-x old-x 1/m
old-x
8 7 6 5 4 3 2 1
y
(5.4,7.3)
(1.5?,2)
2 1
0.4
(1.5,1.6)
1
?
(1.5,1.6)
1/m
x 1 2 3 4 5 6 7
25Color Interpolation
- Color interpolation is achieved basically through
the same mechanism. - Z-value interpolation for z-buffering is also
done in the same manner.
(r,g,b)(245,87,32) at (5.4,7.3)
8 7 6 5 4 3 2 1
y
(r,g,b)(1880.410,,)(192,,) at (,2)
r20210
2 1
r19210
0.4
(r,g,b)(188,23,99)at (1.5,1.6)
x 1 2 3 4 5 6 7
(r,g,b)(188,23,99) at (1.5,1.6)
?y 7.3-1.6 5.7 ?r 245-18857 ?r/?y 57/5.7
10
26Z-buffer Algorithm with Gouraud Shading
- for all x, y
- z-bufferx,y background depth
- frame-bufferx,y background color
- for each polygon T
- if T is a front face, i.e. neither a back face
nor a silhouette - obtain vertex colors (I1, I2 I3 for ?) and
z-values (z1, z2 z3 for ?) - compute Ts y-range ylow,yhigh
- ymin ?? ylow? ?
- ymax ? yhigh ? ? -1
- for y ymin to ymax
- compute xleft,xright x-intersections
of T along y - xmin ?? xleft? ?
- xmax ? ?xright ? ? -1
- compute ltIa, Ibgt and ltza, zbgt by interpolation
- for x xmin to xmax
- compute Ip and zp at (x,y) by interpolation
- if zp gt z-bufferx,y
27Problems in Gouraud Shading
- Recall cosm?(rv)m for highlighting in specular
reflection. - Suppose that A and B are two vertices of a
triangle, and m is a big value. Because m is big,
highlight falls sharply as ? increases. - Then, we should have the triangle shown at the
right.
vertex A
r1
l
l
v2
r2
v1
n1
n2
vertex B
B small ?
A large ?
28Problems in Gouraud Shading (contd)
- However, Gouraud shading is color
interpolation, and therefore the colors at
vertices are linearly/evenly interpolated along
the edges and then along scan lines. - Worse still, highlights inside a triangle cannot
be caught.
Gouraud shading what we should have
small ?
scan line
large ?
To avoid the second problem, break up the (large)
polygon into smaller ones.
29Phong Shading
- The problems of Gouraud shading can be overcome
by Phong shading, which is a normal interpolation
shading. - Determine the normal of each vertex. (Same as in
Gouraud shading.) - Linearly interpolate vertex normals along each
edge. - Linearly interpolate normals along each scan
line. - Compute colors at each pixel (with Phong lighting
model).
interpolation along edges
interpolation along a scan line
30Example of Gouraud Shading with Specular
Reflection
31Example of Phong Shading with Specular Reflection
32Example with Curved Surfaces, not Polygon Meshes
33Shading Summary
- Three types of shading
- Flat shading Lighting computation per polygon
- Gouraud shading Lighting computation per vertex
- Phong shading Lighting computation per pixel
- Most graphics hardware implementations have
adopted Gouraud shading. - It is as fast as flat shading and produces
improved quality. - It is highly dependent on the LOD of the mesh.
For a coarse mesh, highlights are often missing,
the spot-light effect cannot be caught, etc. - Thanks to the advance of hardware technology, the
trends start to move from Gouraud shading to
Phong shading.
Gouraud shading
Phong shading
Flat shading
34Discussions Lighting in Eye Coordinates
- Recall that, in the geometry stage, projection
transformation incurs distortion, which will
distorts the configuration of l, n, r and v. So,
lighting computation should be completed before
the projection transformation. - Lighting is computed at EC.
- Think about why not in LC or WC.
modeling viewing transformation
projection transformation
in EC
in EC
in CC
lighting
in LC
perspective division
viewport transformation
in CC
clipping
in WdC
in NDC
35Discussions Normal
- When a series of transformations is applied to a
model, the models vertex coordinates are
transformed. How about the normals? Can we apply
the same transformations to the normals? - With 2D cross-section of a polygon, consider
applying a scaling to both the polygons vertices
and normal. - As discussed earlier, we need vertex normals at
EC for lighting, and we just found that the
modelview transformation Mmodelview which
transforms the model from LC to EC should not be
directly applied to normals.
sx0.5 sy1
36Discussions Normal (contd)
- Suppose the normal n which should be
perpendicular to the edge connecting p and q. - Apply the transpose of the inverse of Mmodelview
to normal vectors.
n
q
p
to vertices
to normals
37Discussions Normal (contd)
- We have to apply (Mmodelview-1)t to normals, but
. - What if Mmodelview is simply a rotation?
- Recall that every rotation is an orthogonal
matrix, whose inverse is equal to its transpose. - So, we can simply use Mmodelview instead of
(Mmodelview-1)t. - How about translations?
- Translations do never affect vector direction.
- So, if Mmodelview is a combination of rotations
and translations, i.e. a rigid-body motion, we
can use Mmodelview instead of (Mmodelview-1)t. - The scaling we took as an example was a
non-uniform scaling. - How about uniform scaling where sxsysz?
- It affects only the length of the transformed
normal, not its direction. - We just need the normalization step to make a
unit vector. - Also note that, for (Mmodelview-1)t, we can
compute only the transpose of the adjoint of the
upper-left 3x3 portion of Mmodelview.
38Discussions Reflection Halfway Vectors
- In the Phong lighting model,
- l is either computed by connecting the vertex
with the light source position (for positional
light) or given as a constant (for directional
light), - n is either computed or given, and
- v is either computed by connecting the vertex
with COP (for perspective projection) or given as
(0,0,1) (for orthographic projection). - The only remaining is r, and it has to be
computed as follows.
n
r
l
?
v
s
s
39Discussions Reflection Halfway Vectors (contd)
- Note that r2n(nl)-l requires 7
multiplications and 5 additions or subtractions
for every vertex. - Suppose that we use parallel projection and
directional light, where - l is a constant, and
- v is also a constant (0,0,1).
- Now, lets take halfway vector h(lv)/lv,
which is also a constant for every vertex. - If l, n, r, and v are in a plane, b?/2.
Otherwise, bgt?/2. - cos?nh and cos?rv are essentially
proportional nh is maximized when rv is
maximized. Approximately, (rv)m (nh)4m. - Lets replace (cosm?) by (cosm?) and (rv)m by
(nh)m in the specular term ispec(cosm?)mspec?ss
pec(rv)mmspec?sspec.
?
?
40Discussions Reflection Halfway Vectors (contd)
- ispec (cosm?)mspec?sspec (nh)mmspec?sspec
has been proposed. - Is it safe? Yes, it is even though (rv)m and
(nh)m are different. - Such a difference does not matter because m is
set to a specific value through trial and error.
So is m. - In fact, many systems such as OpenGL recommend
infinite viewer even though the perspective
projection is chosen. - With the infinite viewer, we can avoid computing
v for every vertex. - Combined with the directional light which makes l
constant, the halfway vector h is also made
constant. - Pretty efficient computation is possible with
infinite viewer only for lighting, but the
rendered image is less realistic.
?
?