Lighting and Rasterization – Shading
1
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
2
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
3
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
4
Point Source
Distributed Source
5
Realistic lighting is higher order and complicated
2nd order light source
2nd order reflection
1st order reflection + 2nd order reflection
6
Shading
When light is incident on an object
part is reflected part is absorbed part is refracted
7
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
8
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
9
Ambient reflection
Iambdiff =kaIa
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
10
Diffuse Reflection
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
incident light
Diffuse (Lambertian) Surface (Rough, dull e.g. wood)
11
Amount of incident light received by the surface is proportional to the projected area of the surface in the lighting direction
12
Il,diff =kdIl(N⋅L)
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
13
Unit normal vector N
The total amount of light received by the surface with area A is proportional to Acosθ = A (N•L)
Surface with area A
Incident
light from light source with intensity
Il
Acosθ
θ
Unit lighting direction vector L
θ
14
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
Incident light
Specular
Surface (Shiny e.g. mirror, gold silver, glass)
L Incident light direction
15
Il,spec =W(θ)Il cosns φ
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
16
17
R =(2N·L)N–L
18
19
General Model with n light sources with ambient, diffuse and specular terms
I =kaIa +
∑ i=1
I [k (N⋅L )+W(θ )(V⋅R )ns ] lid i i i
n
20
Colour model
Each light source is a vector with Red, Green, Blue component (IlR, IlG, IlB)
Calculates each component separately: n
∑ i=1
IR=kaRIaR+ I [k (N⋅L)+W(θ)(V⋅R)nsR]
lRidR i Ri i IG=kaGIaG+ I [k (N⋅L)+W(θ)(V⋅R)nsG]
n
∑ i=1
lGidG i Gi i IB=kaBIaB+ I [k (N⋅L)+W(θ)(V⋅R)nsB]
n
∑ i=1
lBidB i Bi i
21
G
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
B
R
22
LG-32UD59-B
23
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
24
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
25
26
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.)
27
Step 1 : Determine the average unit normal vector at each polygon vertex
(each Nk is a unit vector, N isaunitvectorbydef.)
v k=1
N = ∑N
n
k
n
∑N k=1
k
v
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
28
29
Linear Interpolation
Points lying on an edge of the polygon : linearly interpolate between two endpoints
I4 = y4 − y2 I1 + y1 − y4 I2 y1 − y2 y1 − y2
interior points of the polygon : linearly interpolate across the scan line
Ip =x5 −xp I4 +xp −x4 I5 x5 −x4 x5 −x4
30
Phong shading
Similar to Gouraud shading, but interpolates normal vectors instead.
Captures specular highlights Highest realism
Slowest (disadv.)
31
Step 1 : determine the average unit normal vector at each polygon vertex
N=y−y N+y−yN 2112
y−y y−y 12 12
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
32
Incremental form
Linear interpolation equation is expressed in incremental form to save computation:
I(y)=I1 + I2 −I1 y1 − y2
one scan line down
I2 −I1 y1 − y2
I ( y − 1) = I ( y ) +
33
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);
34
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);
35
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
36
Surface Rendering
FLAT and Gouraud Shading glShadeModel (surfRenderingMethod);
surfRenderingMethod = GL_FLAT
= GL_SMOOTH
Calculating normals glNormal3* (Nx, Ny, Nz);
Flat shading Gouraud
37
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
glVertex3fv (vertex1); glNormal3fv (normalVector2); glVertex3fv (vertex2); glNormal3fv (normalVector3); glVertex3fv (vertex3);
glEnd ( );
// by average unit normal vector
38
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
39