PDA

View Full Version : What's wrong with my shadow matrix?



BigShooter
11-24-2003, 02:02 PM
Hi, I'm trying to implement shadows. For some weird reason my shadows are revered. That is they look as if the light is shining from below and the plane on which the shadows are projected is at the top of the object...
Here's my shadow calculating function:
void setShadowMatrix(GLfloat destMat[16], float lightPos[4], float plane[4]) {

GLfloat dot;

// dot product of plane and light position
dot = plane[0] * lightPos[0] + plane[1] * lightPos[1] + plane[1] * lightPos[2] + plane[3] * lightPos[3];

// first column
destMat[0] = dot - lightPos[0] * plane[0];
destMat[4] = 0.0f - lightPos[0] * plane[1];
destMat[8] = 0.0f - lightPos[0] * plane[2];
destMat[12] = 0.0f - lightPos[0] * plane[3];

// second column
destMat[1] = 0.0f - lightPos[1] * plane[0];
destMat[5] = dot - lightPos[1] * plane[1];
destMat[9] = 0.0f - lightPos[1] * plane[2];
destMat[13] = 0.0f - lightPos[1] * plane[3];

// third column
destMat[2] = 0.0f - lightPos[2] * plane[0];
destMat[6] = 0.0f - lightPos[2] * plane[1];
destMat[10] = dot - lightPos[2] * plane[2];
destMat[14] = 0.0f - lightPos[2] * plane[3];

// fourth column
destMat[3] = 0.0f - lightPos[3] * plane[0];
destMat[7] = 0.0f - lightPos[3] * plane[1];
destMat[11] = 0.0f - lightPos[3] * plane[2];
destMat[15] = dot - lightPos[3] * plane[3];
}


and my plane is:
float plane[4] = {0.0, 1.0, 0.0, 0.0 };

which I suppose is right for a ground plane?


Thank you,
Luke

BigShooter
11-24-2003, 02:06 PM
I forgot to mention that before drawing the shadows I do:
glMultMatrixf(g_shadowMatrix); which is the matrix from above function.

Relic
11-25-2003, 05:28 AM
Is that intended?
dot = plane[0] * lightPos[0] + plane[1] * lightPos[1] + plane[1] * lightPos[2] + plane[3] * lightPos[3];

BigShooter
11-25-2003, 07:26 AM
It seems so from the book I based my example on (Opengl game programming). It I change that to [2] the shadows are totally off... :-(

Humus
11-25-2003, 09:34 AM
Working shadow matrix code:




void Matrix::loadShadowMatrix(const Plane &plane, const Vertex &lightPos){
float dist = plane.distance(lightPos);

m[ 0] = dist - lightPos.x * plane.normal.x;
m[ 4] = - lightPos.x * plane.normal.y;
m[ 8] = - lightPos.x * plane.normal.z;
m[12] = - lightPos.x * plane.offset;

m[ 1] = - lightPos.y * plane.normal.x;
m[ 5] = dist - lightPos.y * plane.normal.y;
m[ 9] = - lightPos.y * plane.normal.z;
m[13] = - lightPos.y * plane.offset;

m[ 2] = - lightPos.z * plane.normal.x;
m[ 6] = - lightPos.z * plane.normal.y;
m[10] = dist - lightPos.z * plane.normal.z;
m[14] = - lightPos.z * plane.offset;

m[ 3] = - plane.normal.x;
m[ 7] = - plane.normal.y;
m[11] = - plane.normal.z;
m[15] = dist - plane.offset;
}