Here is my problem :
This is an image of my 3D world in the camera view.
On the top left corner we can see that I render the shadow Map I generated.
but it seems like there is some issues with
the light view projection that I can't fix.
After I calculate my centroid in the light view, I rotate it back to world coord and use it to create my projection matrix.
glm::mat4 doRotation(glm::vec3 v1, glm::vec3 v2)
{
v1 = glm::normalize(v1);
v2 = glm::normalize(v2);
glm::vec3 v = glm::cross(v2, v1);
float angle = acos(glm::dot(v2, v1) / (glm::length(v2) * glm::length(v1)));
glm::mat4 rotmat = glm::mat4(1.0);
if (glm::dot(v2, v1) == 1)
return rotmat;
rotmat = glm::rotate(rotmat, angle, v);
return rotmat;
}
glm::vec3 sun(-1, -1, 0);
glm::mat4 rotSun = doRotation(sun, glm::vec3(0, 0, 1));
auto upSun = rotSun * glm::vec4(0, 1, 0, 1);
_upSun = upSun;
glm::mat4 rotBackSun = doRotation(glm::vec3(0, 0, 1), sun);
_centroid = rotBackSun * glm::vec4(centroid, 1.0);
_shadowMap->_shader.use();
glm::mat4 depthProjectionMatrix = glm::ortho<float>(camera->_width * -0.5f, camera->_width * 0.5f,
camera->_height * -0.5f, camera->_height * 0.5f,
camera->_deep * -0.5f, camera->_deep * 0.5f);
glm::mat4 depthViewMatrix = glm::lookAt(camera->_centroid, camera->_centroid + glm::vec3(-1, -1, 0) , camera->_upSun);
glm::mat4 depthModelMatrix = glm::mat4(1.0);
glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
_shadowMap->_shader.setMat4("mvp", depthMVP);
Thanks