Computer Graphics (CS 543) Lecture 9b: Shadows and Shadow Maps Prof Emmanue Agu Computer Science Dept. Worcester Poytechnic Institute (WPI)
Introduction to Shadows Shadows give information on reative positions of objects Use ambient + diffuse + specuar components Use just ambient component
Image courtesy of BioWare Why shadows? More reaism and atmosphere Neverwinter Nights
Types of Shadow Agorithms Project shadows as separate objects (ike Peter Pan's shadow) Projective shadows As voumes of space that are dark Shadow voumes [Frankin Crow 77] As paces not seen from a ight source ooking at the scene Shadow maps [Lance Wiiams 78] Fourth method used in ray tracing
Projective Shadows Odest method: Used in eary fight simuators Projection of poygon is poygon caed shadow poygon Actua poygon Shadow poygon
Projective Shadows Works for fat surfaces iuminated by point ight For each face, project vertices V to find V of shadow poygon Object shadow = union of projections of faces
Projective Shadow Agorithm Project ight-object edges onto pane Agorithm: First, draw ground pane/scene using specuar+diffuse+ambient components Then, draw shadow projections (face by face) using ony ambient component
Projective Shadows for Poygon. If ight is at (x, y, z ) 2. Vertex at (x, y, z) 3. Woud ike to cacuate shadow poygon vertex V projected onto ground at (x p,, z p ) (x,y,z) (x p,,z p ) Ground pane: y =
Projective Shadows for Poygon If we move origina poygon so that ight source is at origin Matrix M projects a vertex V to give its projection V in shadow poygon y m
Buiding Shadow Projection Matrix. Transate source to origin with T(-x, -y, -z ) 2. Perspective projection 3. Transate back by T(x, y, z ) z y x z y x M y Fina matrix that projects Vertex V onto V in shadow poygon
Code snippets? Set up projection matrix in OpenGL appication foat ight[3]; // ocation of ight mat4 m; // shadow projection matrix initiay identity M[3][] = -./ight[]; y M
Projective Shadow Code Set up object (e.g a square) to be drawn point4 square[4] = {vec4(-.5,.5, -.5,.} {vec4(-.5,.5, -.5,.} {vec4(-.5,.5, -.5,.} {vec4(-.5,.5, -.5,.} Copy square to VBO Pass modeview, projection matrices to vertex shader
What next? Next, we oad mode_view as usua then draw origina poygon Then oad shadow projection matrix, change coor to back, re-render poygon. Load modeview draw poygon as usua 2. Modify modeview with Shadow projection matrix Re-render as back (or ambient)
Shadow projection Dispay( ) Function void dispay( ) { mat4 mm; // cear the window gcear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // render red square (origina square) using modeview // matrix as usua (previousy set up) guniform4fv(coor_oc,, red); gdrawarrays(gl_triangle_strip,, 4);
Shadow projection Dispay( ) Function // modify modeview matrix to project square // and send modified mode_view matrix to shader mm = mode_view * Transate(ight[], ight[], ight[2] *m * Transate(-ight[], -ight[], -ight[2]); guniformmatrix4fv(matrix_oc,, GL_TRUE, mm); //and re-render square as // back square (or using ony ambient component) guniform4fv(coor_oc,, back); gdrawarrays(gl_triangle_strip,, 4); gutswapbuffers( ); } z y x z y x M y
Shadow Buffer Theory Aong each path from ight Ony cosest object is it Other objects on that path in shadow Shadow buffer stores cosest object on each path Lit In shadow
Shadow Map Iustrated Second dept buffer caed the shadow map is used Point v a stored in eement a of shadow map: it! Point v b NOT in eement b of shadow map: In shadow Not imited to panes
Shadow Map: Depth Comparison
OpenGL Depth Buffer (Z Buffer) Depth: Whie drawing objects, depth buffer stores distance of each poygon from viewer Why? If mutipe poygons overap a pixe, ony cosest one poygon is drawn Depth......3.3. Z =.5 Z =.3.5.3.3..5.5.. eye
Setting up OpenGL Depth Buffer Note: You did this in order to draw soid cube, meshes. gutinitdispaymode(glut_depth GLUT_RGB) instructs opengl to create depth buffer 2. genabe(gl_depth_test) enabes depth testing 3. gcear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) Initiaizes depth buffer every time we draw a new picture
Shadow Map Approach Rendering in two stages: Loading shadow Map Render the scene
Loading Shadow Map Initiaize each eement to. Position a camera at ight source Rasterize each face in scene updating cosest object Shadow map (buffer) tracks smaest depth on each path
Shadow Map (Rendering Scene) Render scene using camera as usua Whie rendering a pixe find: pseudo-depth D from ight source to P Index ocation [i][j] in shadow buffer, to be tested Vaue d[i][j] stored in shadow buffer If d[i][j] < D (other object on this path coser to ight) point P is in shadow ighting = ambient Otherwise, not in shadow Lighting = amb + diffuse + specuar D[i][j] D In shadow
Loading Shadow Map Shadow map cacuation is independent of eye position In animations, shadow map oaded once If eye moves, no need for recacuation If objects move, recacuation required
References Interactive Computer Graphics (6 th edition), Ange and Shreiner Computer Graphics using OpenGL (3 rd edition), Hi and Keey Rea Time Rendering by Akenine-Moer, Haines and Hoffman