3D Object Representation
1
Lighting and Rasterization –
Creating Shadows
2
Intended Learning Outcomes
Apply fast techniques to generate realistic shadow on
the ground plane and its programming implementation
Extend ray casting technique for general shadow
creation
Apply shadow mapping for general shadow creation
3
Creating Shadow on Plane
Works only when projecting objects onto a plane and point
light source
Idea : Given a point light source s and a plane P,
1. Render the objects normally.
2. Use a coordinate system transformation that transforms s to
the PRP and P to the image plane.
3. Set the object colour to the shadow colour
4. Perspective project the objects onto the image plane, creating
shadows.
5. Use the inverse coordinate system transformation to
transform the shadows to the normal coordinate system
4
X
Y
Z
shadow
X-Y is the plane
3D object
Point light source
(Xs, Ys, Zs) We are the viewer
5
Coordinate transformation such that the light source
becomes the origin
Ms←WC=T(-Xs, -Ys, -Zs)
Perspective projection
−
=
0
1
00
0100
0010
0001
sZ
M
6
OpenGL code
GLfloat light1PosType [ ] = {Xs, Ys, Zs, 1.0};
:
GLfloat M[16]; // OpenGL is in column major format
// though C is in row major format
for (i=0; i<16; i++)
M[i]=0;
M[0]=M[5]=M[10]=1;
M[11]=-1.0/Zs;
object ( ); // draw the objects
glPushMatrix ( ); // save state
glMatrixMode (GL_MODELVIEW);
glTranslatef (Xs, Ys, Zs); // Mwc←s
glMultMatrixf (M); // perspective project
glTranslatef (-Xs, -Ys, -Zs); // Ms←wc
glColor3fv (shadowcolour); // set 𝑘𝑘𝑎𝑎 = 𝑘𝑘𝑑𝑑 = 𝑘𝑘𝑠𝑠 = 0 if you are using lighting model
object ( );
glPopMatrix ( ); // restore state
7
X
Y
Z
shadow
Point light source
paint with shadow
colour
Render the scene
with the light
Implementation notes: in actual programming, cast the shadow on a plane
Z = ε after changing to the light source coordinate system, where ε is a very
small number (why?)
Z= ε
8
Extension to corners
It can be used to cast shadows on corners of the room
(treat it as three planes)
However, it cannot be used to cast shadows on general
non-plane objects
General Shadow creation
Limitations:
Up to now, shadows can only be casted on planes or
corners
No shade differences for overlapping shadows
The shadow boundary is too sharp
Below we introduce two techniques: ray casting using
shadow ray and shadow mapping, that overcome the
first two limitations.
One way to create soft shadows is radiosity, which is a
sophisticated model of ambient reflection
9
10
Ray Casting
retrace the light paths of the rays that arrive at the pixel
for each pixel, send a ray from PRP that goes through
the pixel
find all intersections of the ray with the surfaces
the nearest intersections is the visible part of the surface
for that pixel
11
Ray casting
12
Consider the math.
P = P0 + s u (Pixel Ray Equation)
P0 may either be PPRP or PPIX
Ppix is the (X, Y, Z) coordinates of the pixel
is a unit vector pointing out from PRP
PRPPIX
PRPPIX
PP
PP
u
−
−
=
13
PRP
PPIX
u
14
Ray – Surface Intersections
Suppose the CG scene consists of n surfaces or
polygons
Compute the intersection point(s) of the pixel ray with
each of the n surfaces/polygons
The surface/polygons whose intersection point has the
smallest s is the visible surface
since it is the nearest
15
Ray – Sphere Intersection
Sphere is the simplest surface with analytical equation
PC(XC, YC, ZC) Center
r Radius
02
2
=−− rcPP Vector Equation
2222 )()()( rZZYYXX CCC =−+−+−
16
17
Ray-Sphere Intersections (2)
Sub. P = P0 + su gives a quadratic equation
Solution:
If discriminant < 0, does not intersect
Otherwise choose the intersection with the smaller s
Solution is more difficult and time consuming for more
complicated surfaces
0
22 )( PPPuPuPPu −=∆∆⋅−∆−±∆⋅= Crs
18
Shadowing
The ray casting method above can be used to determine
the visible surface
For each visible surface point P, the question is how to
determine whether P is in shadow
Given a set of light sources, P can be in shadow to a
subset of light sources and illuminated by the rest
If in shadow with respect to source So, then the light
intensity due to So is set to zero
19
Shadow ray
To test whether P is in shadow w.r.t. a point light source
So:
Send a pixel ray from P to So
If the pixel ray intersects ANY surface /polygon on its
way, P is in shadow w.r.t. So
The pixel ray is called “shadow ray”
Shadow Mapping
Idea: A point is in shadow iff it is not visible to the light source
(a visibility determination problem)
Change the coordinate system such that the light position is
the PRP. We call this the lighting coordinate system
Perform a perspective projection. Keep the depth buffer. The
depth buffer holds, for each pixel, the nearest distance to the
light source
For each 3D point to be rendered, change to the lighting
coordinate system.
Project the point. Compare its depth to the value in the depth
buffer.
The point is in shadow if it is not the same value as that in the
depth buffer.
20
21
Figure from
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter09.html
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter09.html
22
Figure from
http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx
http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx
23
References
Text: Ch. 16-10 for ray casting method
Creating shadow on plane
E. Angel, Interactive Computer Graphics, A Top-Down
Approach Using OpenGL, 3rd Ed., 2003, pp. 261-264.
Lighting and Rasterization – Creating Shadows
Intended Learning Outcomes
Creating Shadow on Plane
Slide Number 4
Slide Number 5
OpenGL code
Slide Number 7
Extension to corners
General Shadow creation
Ray Casting
Slide Number 11
Slide Number 12
Slide Number 13
Ray – Surface Intersections
Ray – Sphere Intersection
Slide Number 16
Ray-Sphere Intersections (2)
Shadowing
Shadow ray
Shadow Mapping
Slide Number 21
Slide Number 22
References