Beispiel #1
0
        public void CanCreateOrthographicMatrix()
        {
            const float width            = 10;
            const float height           = 10;
            const float zNearPlane       = 2;
            const float zFarPlane        = 20;
            Matrix3D    projectionMatrix = Matrix3D.CreateOrthographic(width, height, zNearPlane, zFarPlane);

            AssertMatricesAreEqual(Matrix.OrthoRH(width, height, zNearPlane, zFarPlane), projectionMatrix);
        }
        /// <summary>
        /// Creates the WorldViewProjection matrix from the perspective of the
        /// light using the cameras bounding frustum to determine what is visible
        /// in the scene.
        /// </summary>
        /// <returns>The WorldViewProjection for the light</returns>
        private static Matrix3D CreateLightViewProjectionMatrix(Model model, Vector3D lightDirection)
        {
            // Matrix with that will rotate in points the direction of the light
            Matrix3D lightRotation = Matrix3D.CreateLookAt(Point3D.Zero, -lightDirection, Vector3D.Up);

            /*// Get the corners of the frustum
             * Point3D[] frustumCorners = _cameraFrustum.GetCorners();
             *
             * // Transform the positions of the corners into the direction of the light
             * for (int i = 0; i < frustumCorners.Length; i++)
             *      frustumCorners[i] = Point3D.Transform(frustumCorners[i], lightRotation);*/

            // Find the smallest box around the points
            //AxisAlignedBoundingBox lightBox = new AxisAlignedBoundingBox(frustumCorners);
            AxisAlignedBox3D lightBox = model.SourceScene.Bounds;

            lightBox.Transform(lightRotation);

            //lightBox = lightBox.Transform(Matrix3D.CreateScale(2f));

            Vector3D boxSize     = lightBox.Max - lightBox.Min;
            Vector3D halfBoxSize = boxSize * 0.5f;

            // The position of the light should be in the center of the back
            // pannel of the box.
            Point3D lightPosition = lightBox.Min + halfBoxSize;

            lightPosition.Z = lightBox.Min.Z;

            // We need the position back in world coordinates so we transform
            // the light position by the inverse of the lights rotation
            lightPosition = Point3D.Transform(lightPosition, Matrix3D.Invert(lightRotation));

            // Create the view matrix for the light
            Matrix3D lightView = Matrix3D.CreateLookAt(lightPosition, -lightDirection, Vector3D.Up);

            // Create the projection matrix for the light
            // The projection is orthographic since we are using a directional light
            Matrix3D lightProjection = Matrix3D.CreateOrthographic(boxSize.X * 2, boxSize.Y * 2, -boxSize.Z, boxSize.Z);

            return(lightView * lightProjection);
        }
Beispiel #3
0
 public override Matrix3D GetProjectionMatrix(float aspectRatio)
 {
     return(Matrix3D.CreateOrthographic(Width, Width / aspectRatio,
                                        NearPlaneDistance, FarPlaneDistance));
 }