PDA

apapaxionga
03-18-2011, 09:47 PM
Recently, i was learning shadow map. However ,when i was learning the course written by http://www.paulsprojects.net/tutorials/smt/smt.html, i do not understand the operation on the matrix as followed:
T is the texture matrix
Pl is the light's projection matrix
Vl is the light's view matrix
Vc is the camera's view matrix

T=P1xV1xVc-1

this equation transform the camera's eye space to light's clip space, ok, i understand how it works. But, in the fellowing code: textureMatrix=biasMatrix*lightProjectionMatrix*lig htViewMatrix;

the order of the matrix multiplication is not the same as T=P1xV1xVc-1 ,can somebody explain the meaning of the matrix multiplication?

Following is the corresponding code

//Calculate texture matrix for projection
//This matrix takes us from eye space to the light's clip space
//It is postmultiplied by the inverse of the current view matrix when specifying texgen

static MATRIX4X4 biasMatrix
(0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f);

MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lig htViewMatrix;

//Set up texture coordinate generation.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

MrManlyMan
03-20-2011, 12:47 PM
The goal is to transform from the fragments position from camera's model view to shadow clip space with.

T = biasMatrix * lightProjection * lightView * cameraView-1;

So to start from the right:

// * cameraView-1
1. take the fragments modelview position and go backwards to world space. ( this step is left out because glTexGen does that behind the scenes, if you don't use glTexGen you need to do this step )

// * lightView
2. take the position now in world space and go into the light's model view space

// * lightProjection
3. now enter the light's clips space from the light's model view space

// * biasMatrix
4. finally enter the shadow maps screen/texture space which is 0-1, not -1 to 1

now the fragments position is in the same coordinate system as the shadowmap and can be used to sample the shadow map texture.

As will all matrix crap you just have to be aware of what coordinate space you are in. as long as you are comparing apples and apples everything works. the gotchas here are sneaky stuff like glTexGen