Hello, I am implementing shadow mapping in Panda3D by GLSL.
I got problems with the shadowMatrix, the matrix trans world space position to light space position.
Of course I read the sample program written in Cg, it simply pass light camera to the shader and use the shadowMatrix like this:
uniform float4x4 trans_model_to_clip_of_light
Since that is no similar way in GLSL, I calculate the shadowMatrix and pass it to shaders.
Here is how I calculate shadowMatrix.
shadowViewMatrix = self.shadowCam.getMat()
shadowProjectionMatrix = self.shadowCam.node().getLens().getProjectionMat()
self.shadowMatrix = shadowProjectionMatrix * shadowViewMatrix
In fragment shader, I reconstruct fragment view space position from depth texture TexDepthStencil.
float fDepth = texture(TexDepthStencil, fTexCoord).a;
vec4 tmp = p3d_ProjectionMatrixInverse * vec4((fTexCoord.x * 2 - 1.0), (fTexCoord.y * 2 - 1.0), (fDepth * 2 - 1.0), 1.0);
vec3 fPos_view = tmp.xyz / tmp.w;
Then multiple inverse view matrix p3d_ViewMatrixInverse to get the fragment world space position.
vec4 fPos_world = p3d_ViewMatrixInverse * vec4(fPos_view, 1.0);
Multiple shadowMatrix, do perspective divide, and normalize in [0.0, 1.0].
tmp = DirectionalLight.shadowMatrix * fPos_world;
vec3 fPos_light = tmp.xyz / tmp.w;
fPos_light = fPos_light * 0.5 + 0.5;
Finally compare the fragment light space fPos_light depth and shadowMap depth.
float fDepth_light = texture(DirectionalLight.shadowMap, fPos_light.xy).r;
float bias = 0.005;
float shadow = fPos_light.z > fDepth_light + bias? 1.0 : 0.0;
I checked the shadowViewMatrix and shadowProjectionMatrix, they seem ‘correctly’ LookAt Matrix and Ortho Matrix. Sadly, the result doesnot correct. I’d like to know whether my shadowMatrix is right?
If my shadowMatrix is right, what’s wrong in my calculation of fragment light space position?