I found a solution somewhere. Apparently you can do a matrix multiplication so it appears to be projected onto a plane.
Code:
void CRender::drawShadows()
{
//following vector and scalar are the plane in which the shadow lies
Vector3 N; //normal of the floor
float D; //distance to origin
Vector3 Q; //position of the light source
N.x = 0;
N.y = 1;
N.z = 0;
D = 3.3f;
Q.x = 0;
Q.y = 5;
Q.z = 15;
float dot = N.x * Q.x + N.y * Q.y + N.z * Q.z + D * 1;
float matrix[4][4];
glPushMatrix();
glEnable(GL_STENCIL_TEST);
if (shadowcaster)
{
shadowcaster = 0;
glClear(GL_STENCIL_BUFFER_BIT); //maak de buffer leeg
//bereken de matrix
matrix[0][0] = dot - Q.x * N.x;
matrix[1][0] = 0.f - Q.x * N.y;
matrix[2][0] = 0.f - Q.x * N.z;
matrix[3][0] = 0.f - Q.x * D;
matrix[0][1] = 0.f - Q.y * N.x;
matrix[1][1] = dot - Q.y * N.y;
matrix[2][1] = 0.f - Q.y * N.z;
matrix[3][1] = 0.f - Q.y * D;
matrix[0][2] = 0.f - Q.z * N.x;
matrix[1][2] = 0.f - Q.z * N.y;
matrix[2][2] = dot - Q.z * N.z;
matrix[3][2] = 0.f - Q.z * D;
matrix[0][3] = 0.f - 1 * N.x;
matrix[1][3] = 0.f - 1 * N.y;
matrix[2][3] = 0.f - 1 * N.z;
matrix[3][3] = dot - 1 * D;
glMultMatrixf((float *)matrix); //vemenigvuldig de nieuwe matrix met de ouwe matrix
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
this->drawObjects(1);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
}
glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glColor4f(0,0,0,0.6f);
this->drawFloor(DRAW_OPTION_NO_COLOR);
glDisable(GL_STENCIL_TEST);
glPopMatrix();
return;
}
I optimised it a bit so that the shadow is recalculated when the camera has moved.
I looked into making a projection, but it's far too slow. The models used have far too many polygons and the shadow shouldn't be simplified as the user is able to view it from very close. Also, the shadow changes from time to time.
The stencil buffer works best IMO.
Sorry for the Dutch comment on some lines. The project-group insisted on using Dutch comments because else the teachers might not be able to read it. Stupid teachers.
BTW, a little screenie of how it looks now with shadow:
http://www.structweb.com/img/connect4/school_c4_2.jpg