here is the code for a shadow matrix.
works for any plane, and pointlight and directional light.
void glMultShadowMatrixf(GLfloat groundplane[4], GLfloat lightpos[4])
{
GLfloat shadowMat[4][4];
GLfloat dot;
const int X = 0;
const int Y = 1;
const int Z = 2;
const int W = 3;
/* Find dot product between light position vector and ground plane normal. */
dot = groundplane * lightpos +
groundplane[Y] * lightpos[Y] +
groundplane[Z] * lightpos[Z] +
groundplane[W] * lightpos[W];
shadowMat[0][0] = dot - lightpos * groundplane;
shadowMat[1][0] = 0.f - lightpos * groundplane[Y];
shadowMat[2][0] = 0.f - lightpos * groundplane[Z];
shadowMat[3][0] = 0.f - lightpos * groundplane[W];
shadowMat[1] = 0.f - lightpos[Y] * groundplane;
shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
shadowMat[2] = 0.f - lightpos[Z] * groundplane;
shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
shadowMat[3] = 0.f - lightpos[W] * groundplane;
shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
glMultMatrixf((GLfloat*) shadowMat);
}
void RenderShadow(void) const
{
GLfloat groundplane[4] = { 0, 1, 0, 0 };
GLfloat lightpos [4] = { 0, 100, 0, 1 };
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-0.1f, 0.0f);
glPushMatrix();
glMultShadowMatrixf(groundplane, lightpos);
glColor4f (0.0f, 0.0f, 0.0f, 1.0f);
glTranslatef (P.x, P.y, P.z);
glScalef (S.x, S.y, S.z);
glutSolidCube (1.0f);
glPopMatrix();
glDisable(GL_POLYGON_OFFSET_FILL);
}
void Render(void) const
{
Matrix M;
M.SetAxis(Matrix::eX, A);
M.SetAxis(Matrix::eY, B);
M.SetAxis(Matrix::eZ, C);
glPushMatrix();
glTranslatef (P.x, P.y, P.z);
glScalef (S.x, S.y, S.z);
glColor4f (1.0f, 0.0f, 0.0f, 0.4f);
glutSolidCube (1.0f);
glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
glutWireCube (1.0f);
glPopMatrix();
}
[This message has been edited by oliii (edited 03-19-2003).]