#version 400
const int NUM_CASCADES = 3;
uniform sampler2D dlShadowMap[NUM_CASCADES];
uniform float cascadeFarPlanes[NUM_CASCADES];
....
float calcShadow(vec4 position, int idx, vec3 normal, vec3 lightNormal) {
vec3 projCoords = position.xyz;
projCoords = projCoords * 0.5 + 0.5;
float bias = 0.0002;
float shadowFactor = 0.0;
vec2 inc = 1.0 / textureSize(dlShadowMap[idx], 0);
for(int row = -1; row <= 1; ++row) {
for(int col = -1; col <= 1; ++col) {
float textDepth = texture(dlShadowMap[idx], projCoords.xy).r;
shadowFactor += projCoords.z - bias > textDepth ? 1.0 : 0.0;
}
}
shadowFactor /= 9.0;
if(projCoords.z > 1.0) {
shadowFactor = 1.0;
}
return 1 - shadowFactor;
}
void main() {
....
int idx;
for (int i=0; i<NUM_CASCADES; i++) {
if ( abs(mvVertexPos.z) < cascadeFarPlanes[i] ) {
idx = i;
break;
}
}
float shadow = calcShadow(mlightviewVertexPos[idx], idx, currNomal, normalize(directionalLight.direction));
fragColor = clamp(ambientC * vec4(ambientLight, 1) + directionalLightResult * shadow + aroundLight, 0, 1);
float distance = length(mvVertexPos);
fragColor = calcDistanceDiffusing(distance, fragColor, distanceDiffusing);
}