I’m trying to implement cascaded shadow mapping in my game engine, but I’m somewhat stuck at the last step. For testing purposes I’ve made sure all cascades encompass my entire scene. The result is currently this:
At the top the texture of each cascade is displayed.
As you can see the shadows are rendered properly, however each cascade gets darker the bigger the area for the orthographic projection is. (Shouldn’t it get lighter instead?)
This is how I do the texture lookup for the shadow maps inside the fragment shader:
layout(std140) uniform CSM
{
vec4 csmFard; // far distances for each cascade
mat4 csmVP[4]; // View-Projection Matrix
int numCascades; // Number of cascades to use. In this example it's 4.
};
uniform sampler2DArrayShadow csmTextureArray; // The 4 shadow maps
in vec4 csmPos[4]; // Vertex position in shadow MVP space
float GetShadowCoefficient()
{
int index = numCascades -1;
vec4 shadowCoord;
for(int i=0;i<numCascades;i++)
{
if(gl_FragCoord.z < csmFard[i])
{
shadowCoord = csmPos[i];
index = i;
break;
}
}
shadowCoord.w = shadowCoord.z;
shadowCoord.z = float(index);
shadowCoord.x = shadowCoord.x *0.5f +0.5f;
shadowCoord.y = shadowCoord.y *0.5f +0.5f;
return shadow2DArray(csmTextureArray,shadowCoord).x;
}
Right now I’m simply using the return value and multiply it with the diffuse color. Since I’m grabbing the depth value directly from the texture, that explains the different coloration of each cascade on the world. So either this is the wrong approach, or my cascade textures are incorrect to begin with.
For that reason I ended up trying it with a depth comparison instead, but only had limited success:
[...] // Same code as above
shadowCoord.w = shadowCoord.z;
shadowCoord.z = float(index);
shadowCoord.x = shadowCoord.x *0.5f +0.5f;
shadowCoord.y = shadowCoord.y *0.5f +0.5f;
float z = shadow2DArray(csmTextureArray,shadowCoord).x;
if(z < shadowCoord.w)
return 0.25f;
return 1.f;
}
While this resolves the different intensity of each cascade, it only works for the first cascade, all others are blank:
(I colored the cascades because otherwise the transitions wouldn’t be visible in this case)
What am I missing here?