Shadows

Example

// Use the stencil buffer to make sure shadow is
// drawn only on the background.  Configure stencil
// buffer so subsequent drawing sets stencil
// locations to 1.

glEnable( GL_STENCIL_TEST );
glStencilFunc( GL_ALWAYS, 0x1, 0xffffffff );
glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );

// Draw polygons in the shadow plane.

draw_polygons_in_shadow_plane();

// Configure stencil buffer so that drawing only
// occurs where stencil is 1 and so that drawing
// a pixel resets corresponding stencil value to 0.

glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );

glStencilFunc( GL_EQUAL, 0x1, 0xffffffff );
glStencilOp( GL_KEEP, GL_KEEP, GL_ZERO );

// Set up for blending and define the shadow color.

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glColor4f( 0.0, 0.0, 0.0, shadowAlpha );

// Draw the shadow by modifying the MODELVIEW
// matrix and then drawing the object that casts
// the shadow.

glPushMatrix();
shadowTransform( shadow_plane, light_position );
draw_object();
glPopMatrix();

glDisable( GL_BLEND );
glDisable( GL_STENCIL_TEST );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );

// draw the object

draw_object();

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20