I’ve been trying to get a cascaded shadow mapping system implemented on my engine, though it appears to be that the bounding boxes for the cascades aren’t correct.

The part I’m interested in can be found here, under the function name “CalcOrthoProjs”.

I’ve been trying to understand the matrix multiplications with this answer, and the ogldev variable and function names are kind of confusing me.

This is how I modified ogldev’s function to work with my variables:

```
void Scene::calcOrthoProjections(Camera &camera, glm::mat4 LightView, std::vector<glm::mat4> &orthoContainer, std::vector<GLfloat> &cascadeEnd) {
GLfloat FOV, nearPlane, farPlane, ratio;
camera.getPerspectiveInfo(FOV, nearPlane, farPlane);
ratio = static_cast<GLfloat>(RE::config.height) / static_cast<GLfloat>(RE::config.width);
GLfloat tanHalfHFov = glm::tan(glm::radians(FOV / 2.0f));
GLfloat tanHalfVFov = glm::tan(glm::radians((FOV*ratio) / 2.0));
for (GLuint i = 0; i < RE::config.r_shadow_cascade_factor; i++) {
GLfloat xn = cascadeEnd(i) * tanHalfHFov;
GLfloat xf = cascadeEnd(i + 1) * tanHalfHFov;
GLfloat yn = cascadeEnd(i) * tanHalfVFov;
GLfloat yf = cascadeEnd(i + 1) * tanHalfVFov;
//The frustum Corners on View(Camera) space
glm::vec4 frustumCorners(8){
//Near Face
glm::vec4( xn, yn, cascadeEnd(i), 1.0),
glm::vec4(-xn, yn, cascadeEnd(i), 1.0),
glm::vec4( xn, -yn, cascadeEnd(i), 1.0),
glm::vec4(-xn, -yn, cascadeEnd(i), 1.0),
//Far face
glm::vec4( xf, yf, cascadeEnd(i + 1), 1.0),
glm::vec4(-xf, yf, cascadeEnd(i + 1), 1.0),
glm::vec4( xf, -yf, cascadeEnd(i + 1), 1.0),
glm::vec4(-xf, -yf, cascadeEnd(i + 1), 1.0),
};
//The frustum Corners in LightSpace
glm::vec4 frustumCornersL(8);
GLfloat minX, maxX, minY, maxY, minZ, maxZ;
minX = minY = minZ = std::numeric_limits<GLfloat>::max();
maxX = maxY = maxZ = std::numeric_limits<GLfloat>::min();
glm::mat4 cam = (camera.getProjection() * camera.getView());
glm::mat4 camInverse = glm::inverse(cam);
for (GLuint j = 0; j < 8; j++) {
//View(Camera) space to world space
glm::vec4 vW = camInverse * frustumCorners(j);
//world space to light space
frustumCornersL(j) = LightView * vW;
minX = min(minX, frustumCornersL(j).x);
maxX = max(maxX, frustumCornersL(j).x);
minY = min(minY, frustumCornersL(j).y);
maxY = max(maxY, frustumCornersL(j).y);
minZ = min(minZ, frustumCornersL(j).z);
maxZ = max(maxZ, frustumCornersL(j).z);
}
orthoContainer(i) = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ) * LightView;
}
}
```

LightView represents a matrix created with:

```
glm::LookAt(-glm::normalize(light.direction), glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0))
```

camera.getProjection() returns the perspective matrix of the main camera

camera.getView() returns a LookAt at the objective the camera is looking at

The orthoContainer values are then fed into the depth rendering unaltered afterwards (by anything other than the model matrix of each model)

I wrote some comments on how I think the math is done, trying to understand what’s wrong.

The result is a frustum too wide, resulting on low res shadows(even for the closest shadow map):

and this is the depth map of the closest shadow map:

any insight as to why this isn’t working, or any other best practice advice, is welcome. *Thanks in advance!*