Directional light, shadow mapping and matrices..

Started by
13 comments, last by KaiserJohan 10 years, 2 months ago

Thanks, I've got shadows working with the static bounding box, I'll be trying what you wrote in an earlier post about inverting the camera view/projection matrix. One thing though, what do you mean by

run the corners of a 2 x 2 x 2 box centered on the origin through it

?

Advertisement

The corners of a 2x2x2 box at the origin are 8 vertices where each component is either a 1 or a -1. Vec3(1,1,1), Vec3(-1,1,1), .. and so on. This corresponds to the view volume in clip-space. The view-projection matrix maps from world space to clip space (it is the concatenation of the view matrix and the projection matrix). So if you invert the view-projection matrix, it will map from clip space to world space. If you apply the inverse-view-projection matrix to each of the 8 corners, you should end up with the 8 corners of the view volume in world-space. The inverse-view-projection matrix will be a 4x4 matrix, so each of your vertices will need to be "promoted" to a Vec4 with a w-component of 1, and the result will be in homogeneous coordinates so you will need to divide by w after the transform.

If I understod correctly;


        Mat4 rotationMatrix(Vec4(perpVec1, 0.0f), Vec4(perpVec2, 0.0f), Vec4(lightDir, 0.0f), Vec4(0.0f, 0.0f, 0.0f, 1.0f));  // again, column major

        Vec4 corners[8] = { 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 inverseVPMatrix = glm::inverse(viewProjectionMatrix);    // the cameras view-projection matrix
        for (uint32_t i = 0; i < 8; i++)
        {
            corners[i] = corners[i] * inverseVPMatrix;
            corners[i] /= corners[i].w;
        }

        for (uint32_t i = 0; i < 8; i++)
            corners[i] = corners[i] * rotationMatrix;

Looking at the corners after multiplying with the inverse VPMatrix and dividing by w, the values all seem to be in the range of [-2.0, 2.0]. which can't be right?

That doesn't sound right. I don't see anything wrong with the code there. There must be something wrong with the viewProjection matrix, make sure view and projection are concatenated in the correct order. For column major matrices, it should be projection * view, not the other way around.

Turned out the issue was with wrong order of multiplication of the corners and inverse VP matrix. This is the correct one:


corners[i] = inverseVPMatrix * corners[i];

Perhaps due to glm being column major?

I consider this topic solved; big thanks for all the help! I'm gonna investigate cascaded shadow mapping next,

This topic is closed to new replies.

Advertisement