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; |