Hello all,
I am currently following the tutorial from Fabien Sanglard:
http://fabiensanglard.net/shadowmapping/index.php
I pretty much understand everything except the bias involved when passing the light transformation to the shader:
void setTextureMatrix(void)
{
static double modelView[16];
static double projection[16];
// Moving from unit cube [-1,1] to [0,1]
const GLdouble bias[16] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0};
// Grab modelview and transformation matrices
glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glMatrixMode(GL_TEXTURE);
glActiveTextureARB(GL_TEXTURE7);
glLoadIdentity();
glLoadMatrixd(bias);
// concatating all matrices into one.
glMultMatrixd (projection);
glMultMatrixd (modelView);
// Go back to normal matrix mode
glMatrixMode(GL_MODELVIEW);
}
The fragment shader does something like:
// Vertex shader uses the matrix built in the code:
//ShadowCoord= gl_TextureMatrix[7] * gl_Vertex;
vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w ;
// Used to lower moiré pattern and self-shadowing
shadowCoordinateWdivide.z += 0.0005;
float distanceFromLight = texture2D(ShadowMap,shadowCoordinateWdivide.st).z;
It seems that the viewport scaling is performed before the w divide and I don’t get it, the viewport transform should be done after the w divide.
I have tried to remove the bias and do it after the w divide in the shader:
In the code:
//glLoadMatrixd(bias);
In the shader:
vec4 shadowCoordinateWdivide = ShadowCoord/ShadowCoord.w;
shadowCoordinateWdivide = shadowCoordinateWdivide *0.5+0.5 ;
I still get the shadow by doing the view port after the w divide (as expected).
Now I’ve done it on paper and to perform the viewport transform before the w divide doesn’t produce the same result as doing the w divide and then the viewport transform.
I wonder if the graphic result is “OK” looking but mathematically incorrect in this tutorial.