Shading & Lighting Models
Lecture: 9
Fall 2016
Computer Graphics (CS3388) Department of Computer Science
University of Western Ontario
Shading & Lighting Models
Outline
Basics of shading & lighting Light sources
Reflections
Lambertian shading Gouraud shading
Phong Shading
materials from Foley & Van Dam, Nancy Pollard (CMU), T. M ̈oller (SFU), T. Komura (Edinburgh), E. Tosun (NYU), S. Jamin (umich)
1
Shading
Why shade?
Human vision uses shading as a cue to form, position, & depth Total handling of light is very expensive
Shading models can give us a good approximation of what would “really” happen, much less expensively
2
Light Simulation
Effective simulation requires:
object representation (geometry)
object illumination (lighting)
camera model (vision)
Rendering: converting graphical data into image
3
Illumination & Shading
Illumination Model: determine the color of a surface (data) point by simulating some light attributes
Local IM: deals only with isolated surface (data) point and direct light sources
Global IM: takes into account the relationships between all surfaces (points) in the environment.
Shading Model: applies the illumination models at a set of points and colors the whole scene
4
Illumination model
Simple Illumination
point light source
Models only the direct illumination from the source (no bounce back light)
Illumination models evaluated only at the ‘vertices’
Complex Illumination
The incoming light can come from a source, or bouncing off another object, or after multiple bounces
Sources can be extended
Multiple interactions between light and surface
5
Illumination vs Rendering
An illumination model is used to calculate the intensity of the light that is reflected at a given point on a surface
A rendering method uses intensity calculations from the illumination model to determine the light intensity at all pixels in the image
6
Light source models
Point Source: All light rays originate at a point and radially diverge Parallel source: Light rays are all parallel. May be modeled as a point
source at infinite distance (the sun)
Distributed source: All light rays originate at a finite area in space. It models a nearby source, such as a fluorescent light
7
Light interaction with surface
Ambient: ‘background’ illumination
Diffuse: non-shiny illumination and shadows Specular: bright, shiny reflections
8
Assumptions on light
Light travels in a straight line
Ignore its frequency composition and care about its component-wise
intensity (energy): R, G, B
general light sources are difficult to work with because we must integrate light coming from all points on the source
9
Light Effects
Light = reflected + absorbed + transmitted
Light = ambient + diffuse + specular
I =KaIa +KdId +KsIs
10
Ambient Lighting
non-directional light in the environment (background light) amount of ambient light incident on each object is constant for all
surfaces and over all directions Precise simulation of this is very hard!
11
Ambient Lighting
The reflected intensity Iamb of any point on the surface is: Iamb = KaIa
Iamb: ambient light intensity
K1 ∈ [0, 1]: surface ambient reflectivity (or percentage of the light
reflected by the object)
In principle Ia and Ka are functions of color, so we have Iamb, Iamb,
I amb B
RG
12
Ambient Lighting
13
Diffuse Lighting
Also known as Lambertian reflection
Lambert’s cosine law: Colour intensity is proportional to the cosine of
the angle between surface normal and direction to light source
Ld ∝ cos(θ) Ld ∝N.L
Does not depend on view direction = scatters the same way in all directions
14
Diffuse Lighting
Diffuse (Lambertian) surfaces are rough or grainy, like clay, soil, fabric The surface appears equally bright from all viewing directions
The brightness at each point is proportional to cos(θ)
15
Diffuse Lighting
I = IpKd cos(θ) = IpKd(N.L)
Ip: Light intensity
Kd ∈ [0, 1]: Surface diffuse reflectivity
θ: Angle between normal vector & direction to light source N: Surface normal
L: Light direction
No dependence on camera angle! 16
Diffuse Lighting: Example
17
Diffuse Lighting: Example
diffuse reflection from different light directions
18
Ambient + Diffuse
Commonly, there are two types of light sources: A background ambient
A point light source
Combining two types of light:
I =Idiff +Iamb =KdIpN.L+KaIa (should be replicated for each colour channel)
19
Ambient + Diffuse
What’s missing?
Shininess
20
Specular Reflection
Ideal Specular or Mirror reflection
Models shiny and glossy surfaces (like metal, plastic, etc.) with
highlights
Can model objects with smooth, shiny surface (metal, polished stone, plastics, apples, skin)
No dependence on object colour
21
Specular Reflection
An ideal specular surface (mirror) reflects light exclusively in one direction: R
Glossy objects are not ideal mirrors and reflect in the immediate vicinity of R
Reflectance intensity changes with reflected angle (intensity depends on viewing direction)
22
Specular Reflection
The Phong model
reflected specular intensity falls off as some power of cosn(φ) Ispec = KsIp cosn(φ) = KsIp(R.V)n
Ks: the surface specular reflectivity
n: specular reflection parameter (for a perfect mirror, n = ∞)
23
Specular Reflection
The Phong model: plots of cosn(φ) for three values of the specular parameter n:
24
Phong Specular Reflection
How to compute reflection vector R?
R = −L + 2(N.L)N
How?
(Remember Lambert’s cosine law)
L + R = (2 cos(θ))N
25
Specular Reflection: efficiency boosting
Halfway Vector:
computing reflection vector r can be expensive Find a vector halfway between l and v: h = i + v Specular reflection is modelled as,
Isp = KsIpmax(0,(N.h)n)
26
Combined Lighting Model
Combining ambient, diffuse and specular highlights gives the Phong Illumination model:
I = Iamb + Idiff + Ispec
I=KaIa +Ip(KdN.L+Ks(R.V)n)
For K light sources in the scene,
I = Iamb + (IKdiff + IKspec ) K
27
Specular Reflection: Example
28
Specular Reflection
Effects of the specular parameter n
29
Examples
30
Examples
Ambient
31
Examples
Ambient + Diffuse
32
Examples
Ambient + Diffuse + Specular
33
Shading
How do we use these models to shade the triangles in the graphics pipeline?
Are we supposed to do the computation of lighting at all the points over the surface?
Local shading models
Flat Shading (once per polygon)
Gouraud shading (for all the vertex of the polygon) Phong Shading (all the points)
34
Flat Shading
A single intensity is calculated for each surface polygon All pixels in a polygon are set to the same colour calculate intensity from the reflection model
use the surface normal (for triangles, average for others)
35
Flat Shading
Assumptions:
The object is a polyhedron
Light source is far away from the surface so that N.L is constant over
each polygon
Viewing position is far away from the surface so that V .R is constant over each polygon
polygons represent actual surface, not an approximation
36
Flat Shading: problems
Creates discontinuities in colour easily visible
hence we can see the distinct surface patches easily Suffers from an effect called Mach banding
caused by “lateral inhibition” of the receptors in the eye (optical illusion)
37
Gouraud Shading
Also called Smooth Shading
Renders the polygon surface by linearly interpolating intensity values
across the surface
Gouraud Shading algorithm
1 Determine the normal at each polygon vertex
2 Apply an illumination model to each vertex to calculate the vertex
intensity
3 Linearly interpolate the vertex intensities over the surface polygon
38
Gouraud Shading
Normal computation:
The normal Nv of a vertex is an average of all neighboring normals k Nk
average all adjacent face normals (Requires knowledge about which faces share a vertexadjacency info)
n= n1+n2+n3+n4 |n1 +n2 +n3 +n4|
Nv = |k Nk|
39
Gouraud Shading
Colour interpolation of the vertex intensities:
I4 = y4 − y2 I1 + y1 − y2
I5 = y5 − y2 I3 + y3 − y2
Ip = x5 − xp I4 + x5 − x4
y1 − y4 I2 y1−y2
y3 − y5 I2 y3−y2
xp − x4 I5 x5−x4
40
Gouraud Shading: Example
41
Flat vs Gouraud Shading
42
Gouraud Shading: problems
Wrong highlights: Highlights depend on polygonal shape
Does not capture highlight in the middle of polygon c1 = 0 because N.h < 0 (remember halfway vector)
c2 =0becauseN.L<0
any interpolation of c1 and c2 will be zero!
43
Gouraud Shading: problems
Gouraud Shading: Wrong highlights
color at vertex may not be (highlight) color inside the polygon
What would give the most accurate simulation of highlights? compute normal of actual surface (not polygon-approximated surface)
at each pixel and color accordingly: very expensive!
Solution: interpolate normal of vertices across polygon surface & color each pixel according to interpolated normals: Phong Shading
44
Phong Shading
Phong shading algorithm:
determine average normal at each vertex
linearly interpolate per-pixel normal across surface
compute color for each pixel using the value of the approximated per-pixel normal
45
Phong Shading: Example
46
Polygon Rendering Examples
47
Polygon Rendering Examples
48
Polygon Rendering Examples
49