I'm troubleshooting cascaded shadow mapping for DX11. I need some input, if the depth textures I generated for the different cascades are correct.
Here's the scene, where you see shadow artifacts from the directional light (looking (-1, -1, -1) - using right-handed coordinate system). Ignore the point light which is casting shadows near the cube.
[attachment=23212:scene.png]
And here are the various cascade depth textures, in the following ranges: [0.1, 6], [6, 12], [12, 18], [18, 100]
[attachment=23213:csm1.png]
[attachment=23214:csm2.png]
[attachment=23215:csm3.png]
[attachment=23217:csm4.png]
But that cant be right now that I think about it - for example, the alien is 6 units into the scene from cameras POV, so it should be clipped in the third image for example?
The way I am creating the view-Projection matrix for this is using the code I used in OpenGL, and I am using the same math library so the matrices should be 1-to-1.
CameraFrustrum CalculateCameraFrustrum(const float fovDegrees, const float aspectRatio, const float minDist, const float maxDist, const Mat4& cameraViewMatrix)
{
CameraFrustrum ret = { Vec4(1.0f, 1.0f, -1.0f, 1.0f), Vec4(1.0f, -1.0f, -1.0f, 1.0f), Vec4(-1.0f, -1.0f, -1.0f, 1.0f), Vec4(-1.0f, 1.0f, -1.0f, 1.0f),
Vec4(1.0f, -1.0f, 1.0f, 1.0f), Vec4(1.0f, 1.0f, 1.0f, 1.0f), Vec4(-1.0f, 1.0f, 1.0f, 1.0f), Vec4(-1.0f, -1.0f, 1.0f, 1.0f), };
const Mat4 perspectiveMatrix = PerspectiveMatrixFov(fovDegrees, aspectRatio, minDist, maxDist);
const Mat4 invMVP = glm::inverse(perspectiveMatrix * cameraViewMatrix);
for (Vec4& corner : ret)
{
corner = invMVP * corner;
corner /= corner.w;
}
return ret;
}
Mat4 CreateDirLightVPMatrix(const CameraFrustrum& cameraFrustrum, const Vec3& lightDir)
{
Mat4 lightViewMatrix = glm::lookAt(Vec3(0.0f), -glm::normalize(lightDir), Vec3(0.0f, -1.0f, 0.0f));
Vec4 transf = lightViewMatrix * cameraFrustrum[0];
float maxZ = transf.z, minZ = transf.z;
float maxX = transf.x, minX = transf.x;
float maxY = transf.y, minY = transf.y;
for (uint32_t i = 1; i < 8; i++)
{
transf = lightViewMatrix * cameraFrustrum[i];
if (transf.z > maxZ) maxZ = transf.z;
if (transf.z < minZ) minZ = transf.z;
if (transf.x > maxX) maxX = transf.x;
if (transf.x < minX) minX = transf.x;
if (transf.y > maxY) maxY = transf.y;
if (transf.y < minY) minY = transf.y;
}
Mat4 viewMatrix(lightViewMatrix);
viewMatrix[3][0] = -(minX + maxX) * 0.5f;
viewMatrix[3][1] = -(minY + maxY) * 0.5f;
viewMatrix[3][2] = -(minZ + maxZ) * 0.5f;
viewMatrix[0][3] = 0.0f;
viewMatrix[1][3] = 0.0f;
viewMatrix[2][3] = 0.0f;
viewMatrix[3][3] = 1.0f;
Vec3 halfExtents((maxX - minX) * 0.5, (maxY - minY) * 0.5, (maxZ - minZ) * 0.5);
return OrthographicMatrix(-halfExtents.x, halfExtents.x, -halfExtents.y, halfExtents.y, halfExtents.z, -halfExtents.z) * viewMatrix;
}