I'm playing around with OpenGL for a few weeks now.
For the following screenshot I picked the glm::ortho values for my lightsource by trial and error. There are two directional light sources with shadows.
I would like to calculate the values for glm::ortho by creating a bounding box around the camera frustum.
I have the corners of the camera frustum in world space ... what is the next step? I think I should move the camera frustum into light space, calculate the bound box and put the dimensions of that bounding box into glm::ortho. But ... how? :)
The block below is a simplified version of my code with only one light source.
// Camera
mat4 cameraViewMatrix = lookAt(
vec3(1.2f, 1.2f, 1.2f),
vec3(0.0f, 0.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f)
);
mat4 cameraProjectionMatrix = perspective(45.0f, 800.0f / 600.0f, 0.5f, 10.0f);
// Light
mat4 lightViewMatrix = lookAt(
vec3(3.0f, -2.0f, 2.0f),
vec3(0.0f, 0.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f)
);
// Camera Frustum
vector<vec4> cubeNDC;
cubeNDC.push_back(vec4(-1.0f, -1.0f, -1.0f, 1.0f));
cubeNDC.push_back(vec4(1.0f, -1.0f, -1.0f, 1.0f));
cubeNDC.push_back(vec4(1.0f, -1.0f, 1.0f, 1.0f));
cubeNDC.push_back(vec4(-1.0f, -1.0f, 1.0f, 1.0f));
cubeNDC.push_back(vec4(-1.0f, 1.0f, -1.0f, 1.0f));
cubeNDC.push_back(vec4(1.0f, 1.0f, -1.0f, 1.0f));
cubeNDC.push_back(vec4(1.0f, 1.0f, 1.0f, 1.0f));
cubeNDC.push_back(vec4(-1.0f, 1.0f, 1.0f, 1.0f));
mat4 viewProjectionMatrixInverse = inverse(matProj * matView);
vector <vec4> cameraFrustum;
for(vec4 vertex: cubeNDC) {
vec4 vertexTransformed = viewProjectionMatrixInverse * vertex;
vertexTransformed /= vertexTransformed.w;
cameraFrustum.push_back(vertexTransformed);
}
// Magic
mat4 lightProjectionMatrix = ortho(...);
Thanx for your help. :)