I'm trying to implement the cascaded shadow map, and I have a bug when I want to access to the corresponding depth texture of each partition of my frustum.
To be more specific my problem occur when I want to select the correct shadow texture, if I try this code below, I have an artifact like in this picture, you are looking at the shadow of a cube, the artifact are the little dot/square between the limits of the cutted frustum (red color is for near cut and green is for far cut)
uniform sampler2D shadowMaps[10]; //GL_TEXTURE5
uniform float cameraFrustumZPartitionning[10];
uniform int cameraFrustumSize;
int getCSMlevel(float Z){
for(int iZ = 0 ; iZ < cameraFrustumSize; ++iZ)
if(Z < cameraFrustumZPartitionning[iZ])
return iZ;
return -1;
}
float ShadowCalculation(vec4 fragPosLightSpace[3], int shadowMapId, float NdotD) //WIP
{
int csmLevel = getCSMlevel(ClipSpacePosZ);
vec4 fragPosLS = fragPosLightSpace[csmLevel];
vec3 projCoords = fragPosLS.xyz / fragPosLS.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = 0.0;
if(csmLevel == -1)
return 0.0;
closestDepth = texture(shadowMaps[csmLevel], projCoords.xy).r;
return 1.0 - ((projCoords.z > closestDepth)?1.0:0.0);
}
And if I try this code everything is fine.
uniform sampler2D shadowMaps[10]; //GL_TEXTURE5
uniform float cameraFrustumZPartitionning[10];
uniform int cameraFrustumSize;
int getCSMlevel(float Z){
for(int iZ = 0 ; iZ < cameraFrustumSize; ++iZ)
if(Z < cameraFrustumZPartitionning[iZ])
return iZ;
return -1;
}
float ShadowCalculation(vec4 fragPosLightSpace[3], int shadowMapId, float NdotD) //WIP
{
int csmLevel = getCSMlevel(ClipSpacePosZ);
vec4 fragPosLS = fragPosLightSpace[csmLevel];
vec3 projCoords = fragPosLS.xyz / fragPosLS.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = 0.0;
if(csmLevel == 0)
closestDepth = texture(shadowMaps[0], projCoords.xy).r;
else if(csmLevel == 1)
closestDepth = texture(shadowMaps[1], projCoords.xy).r;
else if(csmLevel == 2)
closestDepth = texture(shadowMaps[2], projCoords.xy).r;
else
return 0.0;
return 1.0 - ((projCoords.z > closestDepth)?1.0:0.0);
}
In GLSL we are able to make array of sampler2D and get the correct one by accessing the array with a variable or I making a huge mistake here?