How to center directional light depthmap in camera position?

Viewed 36

Right now I am working with shadow maps in my game engine. In the code below I compute View-Projection matrix for directional light source. I have a fixed projection-box size (=50), so now to light-up a box (-50; 50) in all directions positioned in the world center. It works correctly, but I want it to follow camera in the way such that the its position will always be the center of this box. How to do this?

Matrix4x4 DirectionalLight::GetMatrix() const
{
    Vector3 position = Camera::GetPosition();
    float sizeLx = -this->ProjectionSize;
    float sizeRx = +this->ProjectionSize;
    float sizeLy = -this->ProjectionSize;
    float sizeRy = +this->ProjectionSize;
    float sizeLz = -this->ProjectionSize;
    float sizeRz = +this->ProjectionSize;
    Matrix4x4 OrthoProjection = MakeOrthographicMatrix(sizeLx, sizeRx, sizeLy, sizeRy, sizeLz, sizeRz);
    Matrix4x4 LightView = MakeViewMatrix(
        this->Direction,
        MakeVector3(0.0f, 0.0f, 0.0f),
        MakeVector3(0.0f, 1.0f, 0.0f)
    );
    return OrthoProjection * LightView;
}

I am using glm as math library, most functions are aliases/wrappers: MakeOrthographicMatrix is glm::ortho, MakeViewMatrix is glm::lookAt

0 Answers
Related