GLART week 9: Shadows

Examples: -- Use a custom matrix to make shadows. week4/ -- Demonstrate how a matrix can project geometry onto a plane also needed: spotlight_200.jpg -- spotlight image Flag_of_the_United_States.png -- flag image Resources: Wikipedia Shadow Mapping Page NOTES: ================================================================= A simple shadow technique Project a 3D shape onto a flat surface things we need: a light position an object a plane a matrix to draw the object projected onto the plane How to define a plane We need to define what plane the shadow will be cast on The Equation for a plane is four numbers: the normal of the plane (XYZ) and the distance to the orgin (at the closest point to the plane) normal of plane: an XYZ vector perpendicular to the plane pointing towards origin the length of the normal distance of the plane from the origin at it's closest point Example: A floor plane faces up the Y axis and is at the origin: 0,1,0,0 A wall that is five units to the right of the origin: -1,0,0,5 the plane faces towards negative X, so it's facing left A wall that is five units to the left of the origin: 1,0,0,5 the plane faces towards positive X How to define the shadow matrix make a matrix that projects the geometry onto a plane as if viewed from the light position the fourth column in matrix is the xyz direction of the plane see: change a number in the fourth column (try changing the first number to .4f) this column defines how the scene is projected change these numbers to project the scene onto a plane with a little math it's possible to create a matrix that renders geometry as if it is squashed against a plane (projected onto the plane) from the lights point of view. see: - makeShadowMatrix() Make a shadow see: Make a matrix that projects the model onto the floor plane from the light position (makeShadowMatrix()) In drawShadow() multiply the Modelview matrix by the shadow matrix, then draw the model. The matrix makes the model appear to be squashed against the floor plane. The "shadow" is actually the transformed model. In drawShadow() if you comment out the glDisable() commands you can see that the shadow is actually the model rendered with texturing and lighting. We disable lighting and texture to make the shadow look completely flat. We disable depth test to insure that the shadow draws on top of the floor itself (in this case we're not drawing a floor, but in other cases where a floor exists it is more important). Make shadow transparent turn on transparency with glcolor(r,g,b,.5f) but now geometry overlaps itself and looks wrong have to limit drawing within the shadow shape use stencil to prevent drawing twice in the shadow area see DEMO demoCamera: DEMO demoCamera in jocode folder wrap functionality to draw a shadow note in drawShadow() the stencilOp() function increments the stencil value: gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_INCR); and tests for stencil == 1 only thus eliminating stencil values 2,3,4 etc. which eliminates overlapping rendering.