I am doing cascaded shadowmapping and having some issues.
I have a scene like this (yellow light is from a point light; theres also a chair just outside the view to the left):
[attachment=26364:1.png]
If do not use a lights view matrix at all, i.e I only use an orthographic projection matrix when rendering shadow maps, it looks OK. The cascade order is top left, top right, bottom left, bottom right.
[attachment=26365:asd.png]
Now if I use an orthographic projection matrix and use a rotation matrix (as in the code below) based on the lights direction, it instead looks like this:
[attachment=26366:asd2.png]
Which is not correct, for example, the boxes are completely missing, even though they are encompassed in the orthographic projection
The resulting shadows then look like this:
[attachment=26369:asd3.png]
Some parts of shadow are correct, but major artifacts.
Here's how I build the matrix:
Mat4 CreateDirLightVPMatrix(const CameraFrustrum& cameraFrustrum, const Vec3& lightDir /* == Vec3(-1.0f, -1.0f, -1.0f) in this example */)
{
// "cameraFrustrum" contains the 8 corners of the cameras frustrum in world space
float maxZ = cameraFrustrum[0].z, minZ = cameraFrustrum[0].z;
float maxX = cameraFrustrum[0].x, minX = cameraFrustrum[0].x;
float maxY = cameraFrustrum[0].y, minY = cameraFrustrum[0].y;
for (uint32_t i = 1; i < 8; i++)
{
if (cameraFrustrum[i].z > maxZ) maxZ = cameraFrustrum[i].z;
if (cameraFrustrum[i].z < minZ) minZ = cameraFrustrum[i].z;
if (cameraFrustrum[i].x > maxX) maxX = cameraFrustrum[i].x;
if (cameraFrustrum[i].x < minX) minX = cameraFrustrum[i].x;
if (cameraFrustrum[i].y > maxY) maxY = cameraFrustrum[i].y;
if (cameraFrustrum[i].y < minY) minY = cameraFrustrum[i].y;
}
Vec3 right = glm::normalize(glm::cross(glm::normalize(lightDir), Vec3(0.0f, 1.0f, 0.0f)));
Vec3 up = glm::normalize(glm::cross(glm::normalize(lightDir), right));
Mat4 lightViewMatrix = Mat4(Vec4(right, 0.0f),
Vec4(-up, 0.0f), // why do I need to negate this btw?
Vec4(lightDir, 0.0f),
Vec4(0.0f, 0.0f, 0.0f, 1.0f));
return OrthographicMatrix(minX, maxX, maxY, minY, maxZ, minZ) * lightViewMatrix;
}
It was my understanding (based on topics like https://www.opengl.org/discussion_boards/showthread.php/155674-Shadow-maps-for-infinite-light-sources), that all I needed to do shadow mapping for directional light was an orthographic projection matrix and then a rotation matrix (with no translation component, since the dir light has no position, and as I've understod it, translation dosnt matter since it is orthographic meaning no perspective anyway).
Then what is causing the errors in the 3rd image? Is there something I am missing?