Every possible projection of three-dimensional space to three-dimensional space can be achieved with a suitable 4×4 invertible matrix and homogeneous coordinates. If the matrix isn’t invertible but has rank 3, it projects three-dimensional space onto a two-dimensional plane. Every such possible projection can be achieved with a suitable rank-3 4×4 matrix. To find the shadow of an arbitrary object on an arbitrary plane from an arbitrary light source (possibly at infinity), you need to find a matrix representing that projection, multiply it on the matrix stack, and draw the object in the shadow color. Keep in mind that you need to project onto each plane that you’re calling the "ground.”
As a simple illustration, assume the light is at the origin, and the equation of the ground plane is ax + by + cz + d = 0. Given a vertex S = (sx, sy, sz, 1), the line from the light through S includes all points aS, where a is an arbitrary real number. The point where this line intersects the plane occurs when
so
Plugging this back into the line, we get
for the point of intersection.
The matrix that maps S to this point for every S is
This matrix can be used if you first translate the world so that the light is at the origin.
If the light is from an infinite source, all you have is a point S and a direction D = (dx, dy, dz). Points along the line are given by
Proceeding as before, the intersection of this line with the plane is given by
Solving for a, plugging that back into the equation for a line, and then determining a projection matrix gives
This matrix works given the plane and an arbitrary direction vector. There’s no need to translate anything first.