Illumination and Shading
Illumination and Shading
Illumination and Shading
Illumination (Lighting)
Model the interaction of light with surface
points to determine their final color and
brightness
OpenGL computes illumination at vertices
illumination
Shading
Apply the lighting model at a set of points
across the entire surface
Shading
Illumination Model
The governing principles for computing the
illumination
A illumination model usually considers:
Light attributes (light intensity, color, position,
direction, shape)
Object surface attributes (color, reflectivity,
transparency, etc)
Interaction among lights and objects (object
orientation)
Interaction between objects and eye (viewing dir.)
Illumination Calculation
Example: OpenGL
Illumination Models
object 4
object 3 object 2
object 1
Spot light
Simple local illumination
The model used by OpenGL – consider three
types of light contribution to compute the
final illumination of an object
Ambient
Diffuse
Specular
Final illumination of a point (vertex) =
ambient + diffuse + specular
Ambient light contribution
Ambient light (background light): the light that is
scattered by the environment
A very simple approximation of global illumination
object 4
object 3 object 2
object 1
Independent of the light position,object orientation,
observer’s position or orientation – ambient light has
no direction
Ambient lighting example
Ambient light calculation
Each light source has an ambient light contribution
(Ia)
Different objects can reflect different amounts of
ambient (different ambient reflection coefficient Ka,
0 <= Ka <= 1)
So the amount of ambient light that can be seen
from an object is:
Ambient = Ia x Ka
Diffuse light contribution
Diffuse light: The illumination that a surface receives
from a light source and reflects equally in all direction
See nothing!
Specular light example
Specular light calculation
How much reflection you can see depends on
where you are
The only position the eye can see specular from P
if the object has an ideal reflection surface
n = 10 n = 90
n = 30 n = 270
Put it all together
Illumination from a light:
Illum = ambient + diffuse + specular n
= Ka x I + Kd x I x (N.L) + Ks x I x (R.V)
If there are N lights or
1 2 3
(1) constant: specify which light you want to set the property
example: GL_LIGHT0, GL_LIGHT1, GL_LIGHT2 … you can
create multiple lights (OpenGL allows at least 8 lights)
(2) constant: specify which light property you want to set the value
example: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_POSITION
(check the red book for more)
(3) The value you want to set to the property
Property Example
Define colors and position a light
glDisable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
mat_amb_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_speacular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
Global light properties
glLightModelfv(property, value)
Enable two sided lighting
property = GL_LIGHT_MODEL_TWO_SIDE
value = GL_TRUE (GL_FALSE if you don’t want two sided
lighting)
Global ambient color
Property = GL_LIGHT_MODEL_AMBIENT
Value = (red, green, blue, 1.0);
Check the red book for others
Surface Normals
Correct normals are essential for correct lighting
Associate a normal to each vertex
glBegin(…)
glNormal3f(x,y,z)
glVertex3f(x,y,z)
…
glEnd()
The normals you provide need to have a unit length
You can use glEnable(GL_NORMALIZE) to have OpenGL
normalize all the normals
Lighting revisit
Where is lighting performed in the
graphics pipeline?
v1, m1
Display
Polygon shading model
Flat shading – compute lighting once and
assign the color to the whole polygon
Flat shading
Only use one vertex (usually the first one)
normal and material property to compute the
color for the polygon
Benefit: fast to compute
It is used when:
The polygon is small enough
The light source is far away (why?)
The eye is very far away (why?)
OpenGL command: glShadeModel(GL_FLAT)
Mach Band Effect
n3 n4
n = (n1 + n2 + n3 + n4) / 4.0
Gouraud Shading (3)
Compute vertex illumination (color) before the
projection transformation
Shade interior pixels: color interpolation (normals
are not needed)
C1
for all scanlines
Ca = lerp(C1, C2) Cb = lerp(C1, C3)
C2 C3
* lerp: linear interpolation
Lerp(Ca, Cb)
Gouraud Shading (4)
Linear interpolation
x = a / (a+b) * v2 + b/(a+b) * v1
a b
v1 x v2
Interpolate triangle color: use y distance to
interpolate the two end points in the scanline, and
use x distance to interpolate interior
pixel colors
Gouraud Shading Problem
Lighting in the polygon interior can be
inaccurate
Gouraud Shading Problem
Lighting in the polygon interior can be
inaccurate
Phong Shading
Instead of interpolation, we calculate lighting
for each pixel inside the polygon (per pixel
lighting)
We need to have normals for all the pixels –
not provided by the user
Phong shading algorithm interpolates the
normals and compute lighting during
rasterization (need to map the normal back to
world or eye space though)
Phong Shading (2)
Normal interpolation
n1
lerp(na, nb)
n2
n3