XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

View & Projection matrix for diretional light shadow mapping

Last post 11/6/2009 7:21 PM by Postman. 3 replies.
  • 11/1/2009 5:17 PM

    View & Projection matrix for diretional light shadow mapping

    Hi,

    I read some articles about shadow mapping. I have unterstood the principles, but my problem is I don't how to create the view and the projection matrix for a directional light in order to render a scene from the light's point of view.

    I know that the projection matrix has to be orthographic one.

    I have found this article http://www.ziggyware.com/readarticle.php?article_id=235, but I cannot exactly follow the author how to create the martices.

    regards,
    Robert
  • 11/6/2009 5:17 PM In reply to

    Re: View & Projection matrix for diretional light shadow mapping

    I'm a little bit closer to the solution, but I have 2 other question concerning this context:

    1. Could somebody explain me that, please?

    // Matrix with that will rotate in points the direction of the light 
                Matrix lightRotation = Matrix.CreateLookAt(Vector3.Zero,  
                                                           -lightDir,  
                                                           Vector3.Up); 

    2. I have a method creating the view and projection matrices. But putting them into the Creators club example, the shadow caster (a model) is completely covered by the shadow instead of casting it.

    Here is the code:

    // calc bounding box for camera view frustrum in world space 
                Vector3[] points = cameraFrustum.GetCorners(); 
                BoundingBox lightBox = BoundingBox.CreateFromPoints(points); 
     
                // calc centriod of light box 
                Vector3 boxSize = lightBox.Max - lightBox.Min; 
                Vector3 centroid = lightBox.Min + (boxSize / 2.0f); 
     
                // calc distance of centroid to one of the box's corners 
                float distance = Vector3.Distance(centroid, lightBox.Min); 
                Vector3 lightPosition = centroid - (Vector3.Normalize(this.lightDir) * distance); 
     
                // create lights view matrix, so that it is looking right to the center of the bounding box 
                Matrix LightViewMatrix = Matrix.CreateLookAt(lightPosition, centroid, Vector3.Up); 
     
                // bring light box points into light's view space             
                for (int i = 0; i < points.Length; i++) 
                    points[i] = Vector3.Transform(points[i], LightViewMatrix); 
                Vector3 min; 
                Vector3 max; 
                CalcMinMax(points, out min, out max); 
     
                float clipDistance = Math.Abs(max.Z - min.Z); 
     
                // create ortographic projection 
                Matrix LightProjection = Matrix.CreateOrthographicOffCenter(min.X, max.X, min.Y, max.Y, 0, clipDistance);         
     
                return LightViewMatrix * LightProjection; 

  • 11/6/2009 6:42 PM In reply to
    • (2342)
    • premium membership MVP
    • Posts 1,226

    Re: View & Projection matrix for diretional light shadow mapping

    You don't want to use "0" and "clipDistance" as your zNear and zFar parameters.  You want to use -max.Z as your zNear, and -min.Z as your zFar.  Otherwise your zFar won't be large enough.

    Also if you have any other questions about that ziggyware article feel free to ask, since I wrote it. The code in there for calculating a frustum is a little different, since I offset zNear a bit so that it will include areas beyond the view frustum (this way objects that aren't visible can still cast a shadow).
    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone | PIX With XNA Tutorial
  • 11/6/2009 7:21 PM In reply to

    Re: View & Projection matrix for diretional light shadow mapping

    Hi MJP,

    thanks for your response. In the downloaded Creators club example for shadow mapping your suggestions produces the same result. But in my own project both, -max.Z to -min.Z as well as 0 to clipDistance work.

    To your response in detail:
    Why is 'cipDistance' not far enough? It is based the point of the bounding box of the camera's frustum (in light space) which is farthest away to the light camera. But of course you are right with 0 as near plane. I will decrease it to get shadows of object in the back of the light casting shadows into the view frustum.
Page 1 of 1 (4 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG