I’m having trouble implementing shadow mapping for directional lights (lights with a light direction but no defined position; such as the sun).
I managed to get spotlights shadow mapping working in an earlier thread I made.
The problem I have is the construction of the view and the projection matrices which boogles my mind. Here’s what I use right now:
Mat4 viewMatrix = LookAt(Vec3(0.0f) + (directionalLight.mLightDirection * Z_FAR / 2.0f), -directionalLight.mLightDirection, Vec3(0.0f, 1.0f, 0.0f));
Mat4 lightVP = CreateOrthographicMatrix(-Z_FAR, Z_FAR, -Z_FAR, Z_FAR, Z_NEAR, Z_FAR) * viewMatrix;
Z_NEAR is 0.1f and Z_FAR is 100.0f.
My problems:
- For glm::ortho, I understand the first four parameters define the bounding box but does the 5th and 6th argument have to be in the same dimensions or are they constant throughout the program?
- When constructing the view matrix, I run into issues when the camera is far off from the center, say 150 units +X and there are objects there that needs to be shadowed. How do I specify the position properly? If I use the camera position as the position to LookAt(), the shadows will appear to move around when the camera is moving aswell…
- I use (directionalLight.mLightDirection * Z_FAR / 2.0f) to position the lights view far away, but it is almost too far so I need a large bounding box and so loose alot of precision… what is a more proper value to offset the lights view position with?
- I suppose I need the centrum point in the bounding box as the position for the lights view matrix, but how do I define a reasonable bounding box using glm::ortho? The reason I use Z_FAR in all directions is because the camera could be looking in any direction and all withing the viewing range should be shadowed, but the precision in the shadowmap is really bad and the shadows looks very pixelated.
Any pointers to this problem?