Beispiel #1
0
        public void CanCreateLookAtMatrix()
        {
            Matrix3D viewMatrix         = Matrix3D.CreateLookAt(Point3D.Zero, Vector3D.Forward, Vector3D.Up);
            Matrix   expectedViewMatrix = Matrix.LookAtRH(Vector3.Zero, Vector3.UnitZ * -1, Vector3.UnitY);

            AssertMatricesAreEqual(expectedViewMatrix, viewMatrix);
        }
        /// <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 Renderer(Device device, Model model, int width, int height, Transform3D cameraTransform)
        {
            _device          = device;
            _model           = model;
            _cameraTransform = cameraTransform;

            const float fov = MathUtility.PI_OVER_4;

            AxisAlignedBoundingBox bounds = _model.SourceScene.Bounds;
            Vector3D max    = bounds.Size;
            float    radius = System.Math.Max(max.X, System.Math.Max(max.Y, max.Z));

            _projection = Matrix3D.CreatePerspectiveFieldOfView(
                fov,
                width / (float)height,
                1.0f, radius * 10);

            float dist = radius / MathUtility.Sin(fov / 2);

            _view = Matrix3D.CreateLookAt(
                bounds.Center + Vector3D.Backward * dist,
                Vector3D.Forward,
                Vector3D.Up);
        }
Beispiel #4
0
 public override Matrix3D GetViewMatrix()
 {
     return(Matrix3D.CreateLookAt(Position, LookDirection, UpDirection));
 }
Beispiel #5
0
 public double[] GetWorldMatrix()
 {
     return(Matrix3D.CreateLookAt(Position, Forward, Up).ToArray());
 }