PDA

View Full Version : Camera space shadow mapping clip space issues



fredvam
09-27-2015, 04:58 PM
Hello, I have a minor issue where the shadow gets clipped for point lights only, spot lights works fine and I am not sure why.
Besides the clipping issue, the shadow is correct. The clipping artifact results in the shadow disappearing as a hard cut when i start point the viewport down, i added some NDC to remove it clipping by looking upwards that may give some hints.

If I change the shaders to work in world space, the point light shadow works fine. I compute the ShadowView with camera inverse (just like for spot lights etc)

Any input is greatly appreciated!


Depth pass is unchanged for world / camera space and is not included in the samples bellow.

From c++ (last multiplication is active only when I test in camera space versus world space that works fine)


// This code fills the glsl uniform ShadowPointView[index]
for (int sv = 0; sv < 6; ++sv) {
shadowView[sv] = (ShadowBiasMatrix * pointProjection * view[sv]) * glm::inverse(m_camera->view());

}

"fs_in.vs_coords" are in camera space, pointViewPosition is multiplication by camera view (just like spot lights)

vec3 lightDirection = Light[0].pointViewPosition - fs_in.vs_coords;

float shadow = CalcShadowFactor(lightDirection);

Just to demonstrate my comment above, when i test in world space, multiplication of camera->view is removed.


for (auto & i : m_pointLights) {
m_shaderModel->setUniform(i->pointViewPositionUniform,
glm::vec3(m_camera->view() * glm::vec4(i->position, 1.0f)));
}


OK so code posted above I don't think has any errors, in the CalcShadowFactor() I think I have missed some NDC conversion or similar...




float CalcShadowFactor(vec3 LightDirection)
{

vec3 lightDirectionShadow = LightDirection;

float axis[6];
// Adding this line solved the shadow clipping when looking upwards in the scene
lightDirectionShadow = vec3(0.5) * lightDirectionShadow.xyz + vec3(0.5);

lightDirectionShadow = lightDirectionShadow ;

axis[0] = -lightDirectionShadow.x;
axis[1] = lightDirectionShadow.x;
axis[2] = -lightDirectionShadow.y;
axis[3] = lightDirectionShadow.y;
axis[4] = -lightDirectionShadow.z;
axis[5] = lightDirectionShadow.z;

int maxAxisID = 0;
for(int i = 1; i < 6; i++) {
if(axis[i] > axis[maxAxisID]) {
maxAxisID = i;
}
}
vec4 shadowCoord = ShadowPointView[maxAxisID] * vec4(fs_in.ws_coords,1);
shadowCoord.xyz /= shadowCoord.w;

shadowCoord.w = shadowCoord.z;// - shadowPointBias;
shadowCoord.z = float(maxAxisID);


float shadow = shadow2DArray(ShadowMap, shadowCoord).x;


return shadow;
}



Attached a file showing a teapot shadow starting to clip for illustration.
2124
Fredrick