Compute directional light frustum from view furstum points and light direction
- by Fabian
I'm working on a friends engine project and my task is to construct a new frustum from the light direction that overlaps the view frustum and possible shadow casters. The project already has a function that creates a frustum for this but its way to big and includes way to many casters (shadows) which can't be seen in the view frustum. Now the only parameter of this function are the normalized light direction vector and a view class which lets me extract the 8 view frustum points in world space. I don't have any additional infos about the scene. I have read some of the related Questions here but non seem to fit very well to my problem as they often just point to cascaded shadow maps. Sadly i can't use DX or openGl functions directly because this engine has a dedicated math library.
From what i've read so far the steps are: Transform view frustum points into light space and find min/max x and y values (or sometimes minima and maxima of all three axis) and create a AABB using the min/max vectors.
But what comes after this step? How do i transform this new AABB back to world space?
What i've done so far:
CVector3 Points[8], MinLight = CVector3(FLT_MAX), MaxLight = CVector3(FLT_MAX);
for(int i = 0; i<8;++i){
Points[i] = Points[i] * WorldToShadowMapMatrix;
MinLight = Math::Min(Points[i],MinLight);
MaxLight = Math::Max(Points[i],MaxLight);
}
AABox box(MinLight,MaxLight);
I don't think this is the right way to do it. The near plain probably has to extend into the direction of the light source to include potentional shadow casters.
I've read the Microsoft article about cascaded shadow maps http://msdn.microsoft.com/en-us/library/windows/desktop/ee416307%28v=vs.85%29.aspx which also includes some sample code. But they seem to use the scenes AABB to determine the near and far plane which I can't since i cant access this information from the funtion I'm working in.
Could you guys please link some example code which shows the calculation of such frustum?
Thanks in advance!
Additional questio: is there a way to construct a WorldToFrustum matrix that represents the above transformation?