Title: Shadows
1Shadows
2Motivation for Shadows
- Shadows make images more realistic
- Shadows give more depth perception
No shadow Where is the ball in relation to the
plane?
3Shadow position hints at object position
4How to Make Shadows?
- Can use ray-tracing methods (well cover ray
tracing later in the semester) but ray tracing is
slow - Arbitrary casting of shadows among different
objects is difficult to calculate. Need to search
for all objects and calculate projections for
each pair. - For this class, we only cover simple shadows A
triangle casts a shadow on the ground plane.
5Simple shadow on a ground plane
- Assume directional light (sun is at an infinite
distance away, so sunlight rays are parallel) - Then, the model is as follows
Sun
Object
Shadow
Ground
6Simple Shadow From Sun
- Suppose that the ground is on the x-z plane, that
is at y0. - Suppose that the light rays are in the direction
(xL,yL,zL) - Suppose that a point on the object is at
(x0,y0,z0) - Then, the projected (shadow) point on the ground
(xS,yS,zS) would be (x0,y0,z0) k(xL,yL,zL) - But yS 0. Therefore, y0 kyL 0.
- Re-arranging, k -y0/yL
- Then, substituting into xS x0 kxL, we get xS
x0 y0xL/yL - Similarly for zS.
- In this way, we get the following transformation
matrix
xS 1 -xL/yL 0 0 x0
yS 0 0 0 0
y0
zS 0 -zL/yL 1 0 z0
1 0 0 0 1
1
7OpenGL Code for Directional Light Source
void display( void ) float m16
1,0,0,0, 0,0,0,0, 0,0,1,0, 0,0.01,0,1
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0, GL_POSITION,
light_position) glEnable(GL_LIGHT0)
// Draw Actual Object
glMaterialfv() // make object color
glPushMatrix() glTranslatef(0.0,0.5,0.0)
glutSolidSphere(0.2,10.0,10.0)
glPopMatrix() // Draw the
Shadow
glMaterialfv() // make shadow color
glPushMatrix() m4 - light_position0/li
ght_position1 m6 - light_position2/l
ight_position1 glMultMatrixf(m)
glTranslatef(0.0,0.5,0.0)
glutSolidSphere(0.2,10.0,10.0)
glPopMatrix() glutSwapBuffers()
8Simple Shadow from a Point Light Source to the
Ground Plane
- If the light source is a point light source, all
we need to do is to change the projection from
parallel projection to something similar to
perspective projection. - We first translate everything so that the light
source is at the origin. - Then we perform the perspective-like projection.
- Then we translate back.
y
y
Light
Light (xL,yL,zL)
x
Object
Translate by (-xL,-yL,-zL)
Object (x0,y0,z0)
Ground Plane
Ground Plane
x
Projected Point
9Simple Shadow from a Point Light Source to the
Ground PlaneThe Mathematics behind the projection
y
Let the projected point be (xP,yP,zP)
Light
x
yP -yL xP x0(-yL)/y0
x0
yL
Object
zP z0(-yL)/y0
xp
Ground Plane
Therefore, transformation matrix is
Projected Point
1 0 0 0 x0
0 1 0 0 y0
How to derive this Using law of similar
triangles, xp/yp x0/y0 gt xp x0(-yL)/y0
0 0 1 0 z0
0 -1/yL 0 0 1
10Finally, we translate back
y
y
Light (xL,yL,zL)
Light
x
x0
yL
Object
Object (x0,y0,z0)
Translate by (xL,yL,zL)
xp
Ground Plane
Ground Plane
x
Projected Point
Projected Point
11OpenGL Code for Point Light Source
void sideGlutDisplay( void ) float m16
1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0.01,0,0
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0, GL_POSITION,
light_position) glEnable(GL_LIGHT0)
// the object
glMaterialfv() //
object color glPushMatrix()
glTranslatef(0.0,0.5,0.0)
glutSolidSphere(0.2,10.0,10.0)
glPopMatrix() // the
shadow
glMaterialfv() // shadow color
glPushMatrix() m7 -1.0/light_position1
glTranslatef(light_position0,light_posi
tion1,light_position2)
glMultMatrixf(m) glTranslatef(-light_positio
n0,-light_position1,-light_position2)
glTranslatef(0.0,0.5,0.0)
glutSolidSphere(0.2,10.0,10.0)
glPopMatrix() glutSwapBuffers()