Lighting and Rasterization – Creating Shadows
1
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
2
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
3
Z
Point light source
(Xs, Ys, Zs)
We are the viewer
3D object
Y
X
shadow
X-Y is the plane
4
Coordinate transformation such that the light source becomes the origin
Ms←WC=T(-Xs, -Ys, -Zs) Perspective projection
1 0 0 0
0 1 0 0
M = 0 0 1 0
0 0 1 0 −Zs
5
OpenGL code
GLfloat light1PosType [ ] = {Xs, Ys, Zs, 1.0};
GLfloat M[16]; :
for (i=0; i<16; i++) M[i]=0;
M[0]=M[5]=M[10]=1; M[11]=-1.0/Zs;
// OpenGL is in column major format // though C is in row major format
object ( );
glPushMatrix ( );
glMatrixMode (GL_MODELVIEW);
glTranslatef (Xs, Ys, Zs); glMultMatrixf (M); glTranslatef (-Xs, -Ys, -Zs);
glColor3fv (shadowcolour); object ( );
glPopMatrix ( );
Mwc←s
// set 𝑘𝑘𝑎𝑎 = 𝑘𝑘𝑑𝑑 = 𝑘𝑘𝑠𝑠 = 0 if you are using lighting model
// restore state
// draw the objects // save state
//
// perspective project // Ms←wc
6
Render the scene
Z
Point light source
Z= ε
with the light Y
shadow
X
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?)
7
paint with shadow colour
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
8
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
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
10
Ray casting
11
Consider the math.
P = P0 + s u (Pixel Ray Equation)
P0 Ppix
u=
may either be PPRP or PPIX
is the (X, Y, Z) coordinates of the pixel
PPIX −PPRP PPIX −PPRP
is a unit vector pointing out from PRP 12
PPIX
u
PRP
13
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
14
Ray – Sphere Intersection
Sphere is the simplest surface with analytical equation
(X−X )2 +(Y−Y )2 +(Z−Z )2 =r2 CCC
PC(XC, YC, ZC) r
P−P 2 −r2 =0 c
Center Radius
Vector Equation
15
16
Ray-Sphere Intersections (2)
Sub. P = P0 + su gives a quadratic equation
Solution:
s=u⋅∆P± r2 −∆P−(u⋅∆P)u2 ∆P=P −P C0
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
17
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
18
Shadow ray
To test whether P is in shadow w.r.t. a point light source So:
SendapixelrayfromPtoSo
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”
19
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
Figure from
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter09.html
21
Figure from
http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx
22
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.
23