I've been working on CSM, and I'm really close to getting the hard, math part done. I've been following this: https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch10.html and this: http://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf
I'm only doing one cascade right now, so everything near my Camera should be shadowed and everything else should be funky because its not actually in the shadow map. My frustum slice is at 0.1 to 10.0, which should shadow the objects in my scene properly. Here is my code for the crop matrix:
//we have the projected corners, which we no longer need, as well as a crop matrix now glm::vec2 S = glm::vec2(2.0/(max.x-min.x),2.0/(max.y-min.y)); glm::vec2 O = glm::vec2(-0.5*(max.x + min.x)*S.x,-0.5*(max.y + min.y)*S.y); float scaleZ = 1.0f / (max.z - min.z); glm::mat4 cropMatrix = glm::mat4(S.x,0,0,0, 0,S.y,0,0, 0,0,scaleZ,0, O.x,O.y,-min.z*scaleZ,1.0); glm::mat4 newOrtho = glm::ortho(orthoConst, -orthoConst, orthoConst, -orthoConst, 0.1f, 1000.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glLoadMatrixf(&(newOrtho*cropMatrix)); glMatrixMode(GL_MODELVIEW);
The above code doesn't really produce the right results. After a debug draw, I saw that as objects got further away from the light camera, they clipped through the far plane, even though its set to 1,000. Shouldn't it be zooming in to the smaller area as the frustum moves farther away from the light? Thats why I think something is up with the near and far planes. I've tried using the min and max z of the bounding box, but that doesn't seem to help, because I think they are in light clip-space instead of "world space", which is what glOrtho, or in this case glm::ortho uses, I think. I've done debug draws of all the bounding boxes and stuff, and those are correct. When I make the frustum slice to 50, it seems to work fine. Whats wrong with my projection matrix or cropping matrix? Is the cropping matrix scaling the far plane wrong?
Any advice or help would be appreciated. Thanks.