Hey,
I'm trying to implement Cascaded Shadow Mapping. Currently I have a problem creating the correct View and Projection Matrices for the light camera. I proceed as follows:
Let 0.1 be the near plane and 8.0f the far plane for the first cascade. Then I compute the following:
float fovRadians = 70.0 * ( M_PI / 180.0);
float viewRatio = 16.0 / 9.0;
float nearHeight = 2.0 * tan(fovRadians / 2.0) * 8.0;
float nearWidth = nearHeight * viewRatio;
// Compute the 8 corner point of the bounding box around the view frustum from the near plane to the plane at depth 8.0f in world space.
frontLeftBottom = (cam->getCamPos() + cam->getViewVector() * 0.1f - cam->getRightVector() * 0.5f * nearWidth - cam->getUpVector() * 0.5f * nearHeight);
frontLeftTop = (cam->getCamPos() + cam->getViewVector() * 0.1f - cam->getRightVector() * 0.5f * nearWidth + cam->getUpVector() * 0.5f * nearHeight);
frontRightBottom = (cam->getCamPos() + cam->getViewVector() * 0.1f + cam->getRightVector() * 0.5f * nearWidth - cam->getUpVector() * 0.5f * nearHeight);
frontRightTop = (cam->getCamPos() + cam->getViewVector() * 0.1f + cam->getRightVector() * 0.5f * nearWidth + cam->getUpVector() * 0.5f * nearHeight);
backLeftBottom = (cam->getCamPos() + cam->getViewVector() * 8.0f - cam->getRightVector() * 0.5f * nearWidth - cam->getUpVector() * 0.5f * nearHeight);
backLeftTop = (cam->getCamPos() + cam->getViewVector() * 8.0f - cam->getRightVector() * 0.5f * nearWidth + cam->getUpVector() * 0.5f * nearHeight);
backRightBottom = (cam->getCamPos() + cam->getViewVector() * 8.0f + cam->getRightVector() * 0.5f * nearWidth - cam->getUpVector() * 0.5f * nearHeight);
backRightTop = (cam->getCamPos() + cam->getViewVector() * 8.0f + cam->getRightVector() * 0.5f * nearWidth + cam->getUpVector() * 0.5f * nearHeight);
// Compute the center of that box
centroid = (frontLeftBottom + frontLeftTop + frontRightBottom + frontRightTop + backLeftBottom + backLeftTop + backRightBottom + backRightTop) * 1.0f/8.0f;
right = glm::cross(glm::vec3(0,-1.0,-1.0),glm::vec3(0,1,0));
up = glm::cross(glm::vec3(0,-1.0,-1.0),right);
// This is the view matrix for the light and the first shadow cascade ( (0,-1.0,-1.0) is the direction of the light )
V = glm::lookAt(centroid,centroid + glm::vec3(0,-1.0,-1.0), up);
How do I now compute an orthographic projection matrix for the light space (directional light)? Ignore, that there may be shadow casters that are far away, I want an orthographic projection, that projects exactly the bounding box that I have computed above in world space. How exactly do I get my projection matrix from that 8 world space positions?
Thank you very much.
Xardes