3D Object Representation
1
Lighting and Rasterization –
Shading
2
Intended Learning Outcomes
Classify different types of light sources
Understand the image formation process
Mathematically model three types of reflection and
understand their properties
Understand three rendering methods and compare their
pros and cons
Able to program lighting and shading using OpenGL
3
Lighting and Shading Models
Calculate intensity and colour of light that we should see
at a given point of a scene
Ultimate aim : Photorealism
Lighting /Illumination models
models lighting from light sources and the
environment
Shading models
models how lights are processed (reflected, absorbed,
refracted etc) by the objects and the atmosphere
4
Light sources
Ambient source
models background light
Point source
for small nearby light sources
Distributed source
for large nearby light sources
models by a collection of point sources
Lighting direction
(e.g. sun) – for distant light sources
5
Point Source Distributed Source
6
Realistic lighting is higher order and complicated
2nd order reflection
2nd order light source
1st order reflection + 2nd order reflection
7
Shading
When light is incident on an object
part is reflected
part is absorbed
part is refracted
8
Object properties
Opaque object only reflect and absorb light
Transparent object only refract and absorb light
Semi-transparent object reflect, refract and absorb light
The amount of light reflected depends on material.
Shiny material : reflect most of the light
Dull material : absorb most of the light
Let restrict discussion to opaque object at present
9
Types of Reflection
Ambient reflection
Average signal from the background
Non-directional
Diffuse reflection
Rough, dull, matte surfaces
scatter light equally in all directions
Specular reflection
Smooth, shiny, mirror like surfaces
reflect light more in one direction
10
Ambient reflection
ka ambient reflection coefficent, 0 ≤ ka ≤ 1
Ia incident ambient light
Can be interpreted as the average value of diffuse
reflection from numerous light sources in the
background
aaambdiff IkI =
11
Consider a point light source or lighting direction
Lambertian surfaces : Reflections from the surface are
scattered with equal intensity in all directions,
independent of the viewing direction
Diffuse Reflection
incident light
Diffuse (Lambertian)
Surface (Rough, dull
e.g. wood)
12
Amount of incident light received by the surface is
proportional to the projected area of the surface in the
lighting direction
13
kd diffuse reflection coefficient, 0 ≤ kd ≤ 1
Il Incident light intensity
N unit normal of the surface
L unit light direction vector
N·L models the projected area
)(, LN ⋅= lddiffl IkI
14
Incident
light from
light source
with intensity
Il
Unit normal vector N
Surface with
area A
θ
Acosθθ
Unit
lighting
direction
vector L
The total amount of light received
by the surface with area A is
proportional to Acosθ = A (N•L)
15
Specular reflection
Consider a point light source or lighting direction.
Ideal specular surface = perfect mirror: light is only
reflected in the direction of R
Non-ideal reflector: some light are scattered around R
Specular
Surface (Shiny e.g. mirror, gold
silver, glass)
Incident light
L Incident light direction
16
W(θ) specular reflection coefficent, 0 ≤ W(θ) ≤ 1
sometimes W(θ) is assumed to be a constant ks
N bisects L and R (incident angle = reflection angle in a
perfect mirror)
R unit specular reflection direction vector
R = (2N·L)N – L
V unit viewing direction vector
cos(φ) = R·V 0 ≤ φ ≤ π/2
ns specular reflection exponent, ns = ∞ for perfect mirror
φθ snlspecl IWI cos)(, =
17
18
R = (2N·L)N – L
19
20
General Model with n light sources with
ambient, diffuse and specular terms
]))(()([
1
sn
iii
n
i
dliaa WkIIkI RVLN ⋅+⋅+= ∑
=
θ
21
Colour model
Each light source is a vector with Red, Green, Blue
component (IlR, IlG, IlB)
Calculates each component separately:
]))(()([
]))(()([
]))(()([
1
1
1
sB
sG
sR
n
iiBi
n
i
dBlBiaBaBB
n
iiGi
n
i
dGlGiaGaGG
n
iiRi
n
i
dRlRiaRaRR
WkIIkI
WkIIkI
WkIIkI
RVLN
RVLN
RVLN
⋅+⋅+=
⋅+⋅+=
⋅+⋅+=
∑
∑
∑
=
=
=
θ
θ
θ
22
B
G
R
Note:
Only colours in the
triangle is displayable.
Some naturally occurring
colours outside the triangle
cannot be displayed!
Quattron technology uses
4 primary
Colours RYGB that
extends the displayble
colours
CIE chromaticity diagram
-Represent all possible colours
seeable by humans
23
LG-32UD59-B
https://www.forbes.com/sites/forbes-personal-shopper/2018/05/28/lg-32ud59-b-review-a-decent-4k-monitor-with-excellent-color-reproduction/#4d0a7b3240d3
24
Shading Models /Rendering Models
Input : Object tessellated into polygons (standard
graphics object)
Three common ways to shade the polygons:
Flat Shading
Gouraud Shading
Phong Shading
Increasing realism
Increasing computational cost
25
Flat shading
A single intensity is calculated for the polygon. All points
of the polygon are then displayed with the same intensity
value
Fast (Adv.)
Faceted look – ugly!
Human vision is subject to “Mach band effect” – intensity
discontinuities are accentuated. This amplifies the
edges of the polygons, which is undesirable
26
27
Gouraud shading
Linearly interpolate intensity values across each
polygon
Intensities for each polygon are matched with the values
of adjacent polygons along the common edges
Interpolation eliminates the intensity discontinuities that
occur in flat shading
Slower (disadv.)
Smooth out specular highlights (disadv.)
28
Step 1 : Determine the average unit normal vector at
each polygon vertex
(each Nk is a unit vector,
Nv is a unit vector by def.)
Step 2 : Apply an illumination model to each vertex to
calculate the vertex intensity
Step 3 : linearly interpolate the vertex intensities over the
surface of the polygon
∑
∑
=
==
n
k
k
n
k
k
v
1
1
N
N
N
29
30
Linear Interpolation
Points lying on an edge of the polygon : linearly
interpolate between two endpoints
interior points of the polygon : linearly interpolate across
the scan line
2
21
41
1
21
24
4 Iyy
yy
I
yy
yy
I
−
−
+
−
−
=
5
45
4
4
45
5 I
xx
xx
I
xx
xx
I ppp −
−
+
−
−
=
31
Phong shading
Similar to Gouraud shading, but interpolates normal
vectors instead.
Captures specular highlights
Highest realism
Slowest (disadv.)
32
Step 1 : determine the average unit normal vector at
each polygon vertex
Step 2 : linearly interpolate the vertex normals over the
surface of the polygon
Step 3 : apply an illumination model to calculate pixel
intensities of each surface point
2
21
1
1
21
2 NNN
yy
yy
yy
yy
−
−
+
−
−
=
33
Incremental form
Linear interpolation equation is expressed in incremental
form to save computation:
one scan line down
21
12
1)( yy
II
IyI
−
−
+=
21
12)()1(
yy
II
yIyI
−
−
+=−
34
OpenGL Functions : Lighting
glEnable (GL_LIGHTING); // activate lighting routines
glLight* (lightName, lightProperty, propertyValue);
GLfloat light1PosType [ ] = {2.0, 0.0, 3.0, 1.0}; // point
// source; the last entry is 1.0
GLfloat light2PosType [ ] = {0.0, 1.0, 0.0, 0.0}; // light
// direction; the last entry is 0.0
glLightfv (GL_LIGHT1, GL_POSITION, light1PosType); // v
for vector
glEnable (GL_LIGHT1);
glLightfv (GL_LIGHT2, GL_POSITION, light2PosType);
glEnable (GL_LIGHT2);
35
Light source colour
(R, G, B, A) A stands for alpha value
GLfloat blackColor [ ] = {0.0, 0.0, 0.0, 1.0};
GLfloat whiteColor [ ] = {1.0, 1.0, 1.0, 1.0};
glLightfv (GL_LIGHT3, GL_AMBIENT, blackColor);
glLightfv (GL_LIGHT3, GL_DIFFUSE, whiteColor);
glLightfv (GL_LIGHT3, GL_SPECULAR, whiteColor);
36
Surface Property
glMaterial* (surfFace, surfProperty, propertyValue);
diffuseCoeff [ ] = {0.2, 0.4, 0.9, 1.0}; // kdR = 0.2, kdG = 0.4, kdB = 0.9
specularCoeff [ ] = {1.0, 1.0, 1.0, 1.0}; // WR(θ) = 1.0, …
glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
diffuseCoeff );
glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, specularCoeff);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 25.0 ); // ns = 25
37
Surface Rendering
FLAT and Gouraud Shading
glShadeModel (surfRenderingMethod);
surfRenderingMethod = GL_FLAT Flat shading
= GL_SMOOTH Gouraud
Calculating normals
glNormal3* (Nx, Ny, Nz);
38
Gouraud shade a triangle
glEnable (GL_NORMALIZE); // convert all normal vectors to unit vector
glLightModeli (GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
// set correct V for specular calculations
glBegin (GL_TRIANGLES);
glNormal3fv (normalVector1); // normal vector at vertex1 calculated
// by average unit normal vector
glVertex3fv (vertex1);
glNormal3fv (normalVector2);
glVertex3fv (vertex2);
glNormal3fv (normalVector3);
glVertex3fv (vertex3);
glEnd ( );
39
References
Text: Ch. 17.1-17.3 for lighting and shading equations
Text: Ch. 19.3 – 19.4 for CIE chromaticity diagram and
RGB model
Text: Ch. 17.10 for different shading method
Text: Ch. 17.11 for OpenGL commands
Demo: Run lightposition.exe and lightmaterial.exe in
TUTORS program
Quattron technology:
http://en.wikipedia.org/wiki/Quattron
http://en.wikipedia.org/wiki/Quattron
Lighting and Rasterization – Shading
Intended Learning Outcomes
Lighting and Shading Models
Light sources
Slide Number 5
Slide Number 6
Shading
Object properties
Types of Reflection
Ambient reflection
Diffuse Reflection
Slide Number 12
Slide Number 13
Slide Number 14
Specular reflection
Slide Number 16
Slide Number 17
Slide Number 18
Slide Number 19
General Model with n light sources with ambient, diffuse and specular terms
Colour model
Slide Number 22
Slide Number 23
Shading Models /Rendering Models
Flat shading
Slide Number 26
Gouraud shading
Slide Number 28
Slide Number 29
Slide Number 30
Phong shading
Slide Number 32
Incremental form
OpenGL Functions : Lighting
Light source colour
Surface Property
Surface Rendering
Slide Number 38
References
]
)
)(
(
)
(
[
]
)
)(
(
)
(
[
]
)
)(
(
)
(
[
1
1
1
sB
sG
sR
n
i
i
B
i
n
i
dB
lBi
aB
aB
B
n
i
i
G
i
n
i
dG
lGi
aG
aG
G
n
i
i
R
i
n
i
dR
lRi
aR
aR
R
W
k
I
I
k
I
W
k
I
I
k
I
W
k
I
I
k
I
R
V
L
N
R
V
L
N
R
V
L
N
×
+
×
+
=
×
+
×
+
=
×
+
×
+
=
å
å
å
=
=
=
q
q
q
_1380970568.unknown