Thursday, March 5, 2009

Casting shadows in 3D

The effort required to cast a shadow of a polygon onto another is far simpler that you may realize. All polygons are composed of vectors and for the sake of simplifying this discussion, I will only talk about creating a shadow on the ground which we will assume is flat. This isn't much harder for uneven surfaces, but it involves some polygon-on-polygon clipping which we'll discuss some other time.




So for
this discussion imagine a polygon floating up in the air above the Y-plane (the plane that rotates freely around the up vector). Here we have a single polygon floating above the y-plane and we'd like to figure out what it's shadow would look like. Note that all we have is the polygon and the up vector for information.











We can deconstruct the polygon into individual vectors, create a polygon from that and move it on top of the ground. From this new polygon, we simply fill it with some alpha-blended color and we have a shadow.












Here I've shown an individual vector and this is what it might look like. You will be surprised at how little math this takes. Once you have the shadow vector, you do need to translate it into the space directly under the vector which can be done using my earlier finding a point closest to a triangle.


Here is the code for creating the vector of the shadow.





Vector GetInPlanePortionOfVector (const Vector& vect, const Vector& PlaneNormal)
{
float height = (vect.Dot (PlaneNormal));
Vector shadow = vect - height * PlaneNormal;
return shadow;
}

No comments: