QUESTION: 3D transform matrices

QUESTION: 3D transform matrices

Post by Christian Langi » Sat, 29 Sep 2001 06:59:57



I know a bit about 3D transformation matrices (scale, rotation,
rotation) already. But as I study OpenGL, I found in the text book a
matrix transform that creates the shadow of any 3D shape against a plane.

In other words, this computes a perspective projection against an
oblique projection plane. What surprises me is that the transformed
object is flat (2D).

Below is the function that derives the matrix. The points parameters
represent the projection plane. Parameter lightPos is the position of
the spotlight (last parameter is the output matrix).

The question is: how does that work? Any references?
(answer by Email)

thanks

************
void MakeShadowMatrix(GLfloat points[3][3], GLfloat lightPos[4], GLfloat
destMat[4][4])
{
GLfloat planeCoeff[4];
GLfloat dot;

// Find the plane equation coefficients
// (plane normal as a unit vector)
calcNormal(points,planeCoeff);

// the equation of a plane passing through P(x0,y0,z0)
// with a normal N(a,b,c) is
// a(x) + b(y) + c(z) = (a(x0)+b(y0)+c(z0))
// Find the last coefficient by back substitutions
planeCoeff[3] = - (
(planeCoeff[0]*points[2][0]) +
(planeCoeff[1]*points[2][1]) +
(planeCoeff[2]*points[2][2])
);

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

// Now do the projection
// First column
destMat[0][0] = dot - lightPos[0] * planeCoeff[0];
destMat[1][0] = 0.0f - lightPos[0] * planeCoeff[1];
destMat[2][0] = 0.0f - lightPos[0] * planeCoeff[2];
destMat[3][0] = 0.0f - lightPos[0] * planeCoeff[3];

// Second column
destMat[0][1] = 0.0f - lightPos[1] * planeCoeff[0];
destMat[1][1] = dot - lightPos[1] * planeCoeff[1];
destMat[2][1] = 0.0f - lightPos[1] * planeCoeff[2];
destMat[3][1] = 0.0f - lightPos[1] * planeCoeff[3];

// Third Column
destMat[0][2] = 0.0f - lightPos[2] * planeCoeff[0];
destMat[1][2] = 0.0f - lightPos[2] * planeCoeff[1];
destMat[2][2] = dot - lightPos[2] * planeCoeff[2];
destMat[3][2] = 0.0f - lightPos[2] * planeCoeff[3];

// Fourth Column
destMat[0][3] = 0.0f - lightPos[3] * planeCoeff[0];
destMat[1][3] = 0.0f - lightPos[3] * planeCoeff[1];
destMat[2][3] = 0.0f - lightPos[3] * planeCoeff[2];
destMat[3][3] = dot - lightPos[3] * planeCoeff[3];

Quote:}

 
 
 

QUESTION: 3D transform matrices

Post by j » Sat, 29 Sep 2001 21:52:05


Better than absolutely any book is experience.
I suggest you do the following, which tought me
a great deal on how these things work.

set up a window and its callbacks for keyboard input
( glutCreateWindow and glutKeyboardFunc etc )

have a global matrix GLfloat mat[16];
initially set it to Identity,( you may want to set the ' I ' key to do
this )
and lock 16 other keys to the values of the matrix.
f.i. 'q' = mat[0]-=0.01; 'Q' = mat[0]-=0.01; 'a' = mat[1]-=0.01; etc.
and print out the values of the matrix on each change into the console.

then on idle callback, draw a cube through this matrix like

glMatrixMode(GL_PROJECTION)
glLoadMatrixf(mat);
glutSolidCube(1.0);

and you can interactively see what happens.
sort of empirical programming, this is. : )

Jonathan



Quote:

> I know a bit about 3D transformation matrices (scale, rotation,
> rotation) already. But as I study OpenGL, I found in the text book a
> matrix transform that creates the shadow of any 3D shape against a plane.

> In other words, this computes a perspective projection against an
> oblique projection plane. What surprises me is that the transformed
> object is flat (2D).

> Below is the function that derives the matrix. The points parameters
> represent the projection plane. Parameter lightPos is the position of
> the spotlight (last parameter is the output matrix).

> The question is: how does that work? Any references?
> (answer by Email)

> thanks

> ************
> void MakeShadowMatrix(GLfloat points[3][3], GLfloat lightPos[4], GLfloat
> destMat[4][4])
> {
> GLfloat planeCoeff[4];
> GLfloat dot;

> // Find the plane equation coefficients
> // (plane normal as a unit vector)
> calcNormal(points,planeCoeff);

> // the equation of a plane passing through P(x0,y0,z0)
> // with a normal N(a,b,c) is
> // a(x) + b(y) + c(z) = (a(x0)+b(y0)+c(z0))
> // Find the last coefficient by back substitutions
> planeCoeff[3] = - (
> (planeCoeff[0]*points[2][0]) +
> (planeCoeff[1]*points[2][1]) +
> (planeCoeff[2]*points[2][2])
> );

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

> // Now do the projection
> // First column
> destMat[0][0] = dot - lightPos[0] * planeCoeff[0];
> destMat[1][0] = 0.0f - lightPos[0] * planeCoeff[1];
> destMat[2][0] = 0.0f - lightPos[0] * planeCoeff[2];
> destMat[3][0] = 0.0f - lightPos[0] * planeCoeff[3];

> // Second column
> destMat[0][1] = 0.0f - lightPos[1] * planeCoeff[0];
> destMat[1][1] = dot - lightPos[1] * planeCoeff[1];
> destMat[2][1] = 0.0f - lightPos[1] * planeCoeff[2];
> destMat[3][1] = 0.0f - lightPos[1] * planeCoeff[3];

> // Third Column
> destMat[0][2] = 0.0f - lightPos[2] * planeCoeff[0];
> destMat[1][2] = 0.0f - lightPos[2] * planeCoeff[1];
> destMat[2][2] = dot - lightPos[2] * planeCoeff[2];
> destMat[3][2] = 0.0f - lightPos[2] * planeCoeff[3];

> // Fourth Column
> destMat[0][3] = 0.0f - lightPos[3] * planeCoeff[0];
> destMat[1][3] = 0.0f - lightPos[3] * planeCoeff[1];
> destMat[2][3] = 0.0f - lightPos[3] * planeCoeff[2];
> destMat[3][3] = dot - lightPos[3] * planeCoeff[3];
> }