CS155 Computer Graphics
Cheesy Shadow Labs
Today you'll build a little scene with cheesy shadows. Start by downloading the file shadow.cpp. Compile this program and run it. You'll see a red floor, a blue cube, and a white background. The general outline of the cheesy shadow implementation is given in the comments. You need to fill in the details.
First draw a black shadow. Uncomment the code that builds the shadow projection matrix and redraws the cube. You'll also need to disable lighting and set the shadow color. Be sure to re-enable lighting after you've drawn the shadow.
Trouble? You may not see the shadow because it is in the same plane as the floor. To make sure the shadow is being draw, comment out the line drawFloor() earlier in the display function. Now you should be able to see the shadow.
Of course you do want to draw the floor. To make sure the shadow is visible, you need to enable polygonOffset before you draw the shadow. Make sure you disable it after the shadow is drawn.
Rather than drawing a black shadow, we'd like to blend it with the floor. To do this you need to enable blending and set the alpha channel of the shadow color. Be sure to disable alpha blending when you are done with the shadow.
The shadow may not look right because each polygon of the cube is producing its own shadow. The final shadow is darker in regions where several polygon shadows are drawn. To avoid this you need to use the stencil buffer. Enable the stencil buffer before the floor is originally drawn. Whenever a pixel is drawn to the frame buffer, you should write a "3" to the stencil buffer. When drawing the shadow, compare the stencil buffer to 2. If the stencil value is greater than 2 then draw the shadow pixel and replace the stencil value by 2. A shadow will never be drawn more than once for any pixel and only on pixels where the floor has been drawn.
That is it!
For more cool effects download and compile this demo dinoshade.c.