I’ve implemented Cascaded Shadow Mapping as in the nvidia SDK (http://developer.download.nvidia.com/SDK/10.5/Samples/cascaded_shadow_maps.zip). However my lookup just doesn’t seem to work.
Here’s a picture depicting my current state: https://i.stack.imgur.com/PRAht.png
The problem is, I end up in the first split right away eventhough I’m far away from it. As you can see, the other splits aren’t even considered.
I thought the reason for this might come from a different projection matrix the main engine is using. It’s different from the one I supply to the algorithm but I also tried passing the same matrix to the shader and compute this way: gl_Position = matProj * gl_ModelViewMatrix * gl_Vertex
That really didn’t change a thing though. I still ended up with only one split.
Here are my shaders:
[vertex]
varying vec3 vecLight;
varying vec3 vecEye;
varying vec3 vecNormal;
varying vec4 vecPos;
varying vec4 fragCoord;
void main(void)
{
vecPos = gl_Vertex;
vecNormal = normalize(gl_NormalMatrix * gl_Normal);
vecLight = normalize(gl_LightSource[0].position.xyz);
vecEye = normalize(-vecPos.xyz);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
[fragment] (just the shadow part)
vec4 getShadow()
{
vec4 sm_coord_c = texmat_3*vecPos;
float shadow = texture2D(smap_3, sm_coord_c.xy).x;
float s = (shadow < sm_coord_c.z) ? 0.0 : 1.0;
vec4 shadow_c = vec4(1.0, 1.0, 1.0, 1.0) * s;
if(gl_FragCoord.z < vecFarbound.x)
{
vec4 sm_coord_c = texmat_0*vecPos;
float shadow = texture2D(smap_0, sm_coord_c.xy).x;
float s = (shadow < sm_coord_c.z) ? 0.0 : 1.0;
shadow_c = vec4(0.7, 0.7, 1.0, 1.0) * s;
}
else if(gl_FragCoord.z < vecFarbound.y)
{
vec4 sm_coord_c = texmat_1*vecPos;
float shadow = texture2D(smap_1, sm_coord_c.xy).x;
float s = (shadow < sm_coord_c.z) ? 0.0 : 1.0;
shadow_c = vec4(0.7, 1.0, 0.7, 1.0) * s;
}
else if(gl_FragCoord.z < vecFarbound.z)
{
vec4 sm_coord_c = texmat_2*vecPos;
float shadow = texture2D(smap_2, sm_coord_c.xy).x;
float s = (shadow < sm_coord_c.z) ? 0.0 : 1.0;
shadow_c = vec4(1.0, 0.7, 0.7, 1.0) * s;
}
return shadow_c;
}
So for some reason, gl_FragCoord.z is smaller than vecFarbound.x no matter where in the scene I’m at. (Also notice the shadowed area to the far left, this one increases the higher I move the camera and soon takes over all the scene..)
I’ve checked the vecFarbound values and they’re similar to the ones in nvidia’s code so I assume I calculated them right.
Is there a way to check gl_FragCoord.z’s value?
in my old csm implementation I simply used distance in camera space
Thsi solution was a bit simplier for me to understand and I got better control over the splits. When you use Z value (in clip space) there might be some problems that comes from z value being not-linear.
I suggest doing split tests in viewSpace, and then (if it works) use gl_FragCoord.z..