public void CameraPerspectiveInitTestSuccess()
        {
            DataSet set = new DataSet();

            set.Add(ComponentConstants.CAMERA_POS_X, "0.1");
            set.Add(ComponentConstants.CAMERA_POS_Y, "0.2");
            set.Add(ComponentConstants.CAMERA_POS_Z, "0.3");
            set.Add(ComponentConstants.CAMERA_TARGET_X, "0.4");
            set.Add(ComponentConstants.CAMERA_TARGET_Y, "0.5");
            set.Add(ComponentConstants.CAMERA_TARGET_Z, "0.6");
            set.Add(ComponentConstants.CAMERA_UP_X, "0.7");
            set.Add(ComponentConstants.CAMERA_UP_Y, "0.8");
            set.Add(ComponentConstants.CAMERA_UP_Z, "0.9");
            set.Add(ComponentConstants.CAMERA_ZNEAR, "1.0");
            set.Add(ComponentConstants.CAMERA_ZFAR, "1.1");
            set.Add(ComponentConstants.CAMERA_TYPE, Enum.GetName(typeof(ComponentConstants.CAM_TYPES), ComponentConstants.CAM_TYPES.PERSPECTIVE));
            set.Add(ComponentConstants.CAMERA_FOV, "1.2");
            set.Add(ComponentConstants.CAMERA_ASPECT, "1.3");

            CameraComponent           target      = new CameraComponent();
            PrivateObject             obj         = new PrivateObject(target);
            Mock <IExecutableContext> contextMock = new Mock <IExecutableContext>();
            Mock <IMessageRouter>     routerMock  = new Mock <IMessageRouter>();

            contextMock.Setup(f => f.Entity.Name).Returns("TEST");
            contextMock.Setup(f => f.MessageRouter).Returns(routerMock.Object);


            target.Init(contextMock.Object, set);


            Vector3d actualPos      = (Vector3d)obj.GetFieldOrProperty("Pos");
            Vector3d actualTarget   = (Vector3d)obj.GetFieldOrProperty("Target");
            Vector3d actualUp       = (Vector3d)obj.GetFieldOrProperty("Up");
            Vector3d expectedPos    = new Vector3d(0.1, 0.2, 0.3);
            Vector3d expectedTarget = new Vector3d(0.4, 0.5, 0.6);
            Vector3d expectedUp     = new Vector3d(0.7, 0.8, 0.9);
            double   actualZNear    = (double)obj.GetFieldOrProperty("zNear");
            double   actualZFar     = (double)obj.GetFieldOrProperty("zFar");
            double   expectedZNear  = 1.0;
            double   expectedZFar   = 1.1;

            ComponentConstants.CAM_TYPES actualCameraType   = (ComponentConstants.CAM_TYPES)obj.GetFieldOrProperty("cameraType");
            ComponentConstants.CAM_TYPES expectedCameraType = ComponentConstants.CAM_TYPES.PERSPECTIVE;
            double actualFOV      = (double)obj.GetFieldOrProperty("cameraFOV");
            double actualAspect   = (double)obj.GetFieldOrProperty("cameraAspect");
            double expectedFOV    = 1.2;
            double expectedAspect = 1.3;

            Assert.IsTrue(actualPos.Equals(expectedPos));
            Assert.IsTrue(actualTarget.Equals(expectedTarget));
            Assert.IsTrue(actualUp.Equals(expectedUp));
            Assert.AreEqual(expectedZFar, actualZFar);
            Assert.AreEqual(expectedZNear, actualZNear);
            Assert.AreEqual(expectedCameraType, actualCameraType);
            Assert.AreEqual(expectedFOV, actualFOV);
            Assert.AreEqual(expectedAspect, actualAspect);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates a 4x4 double Matrix representing the camera's View (with Perspective added)
        /// </summary>
        /// <param name="Pos">The camera's Position</param>
        /// <param name="Up">The camera's Up vector</param>
        /// <param name="Target">The camera's Target vector</param>
        /// <param name="cameraType">The camera's Type</param>
        /// <param name="zFar">The camera's ZFar</param>
        /// <param name="zNear">The camera's zNear</param>
        /// <param name="cameraWidth">The camera's width</param>
        /// <param name="cameraHeight">The camera's height</param>
        /// <param name="cameraAspect">The camera's aspect ratio</param>
        /// <param name="cameraFOV">The camera's field of view</param>
        /// <returns>a 4x4 double Matrix</returns>
        private Matrix4d generateMatrix(Vector3d Pos, Vector3d Up, Vector3d Target, ComponentConstants.CAM_TYPES cameraType, double zNear, double zFar, double cameraWidth = 0, double cameraHeight = 0, double cameraAspect = 0, double cameraFOV = 0)
        {
            Matrix4d ProjectionMatrix;

            if (cameraType == ComponentConstants.CAM_TYPES.ORTHOGRAPHIC)
            {
                ProjectionMatrix = Matrix4d.CreateOrthographic(cameraWidth, cameraHeight, zNear, zFar);
            }
            else
            {
                ProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView(cameraFOV, cameraAspect, zNear, zFar);
            }

            Matrix4d ViewMatrix = Matrix4d.LookAt(Pos, Target, Up);

            Matrix4d ResultMatrix = ProjectionMatrix * ViewMatrix;

            return(ResultMatrix);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the Camera. Needs the following from the Dataset:
        /// CamPosX - The camera's X position
        /// CamPosY - The camera's Y position
        /// CamPosZ - The camera's Z position
        /// CamTargetX - The camera target's X position
        /// CamTargetY - The camera target's Y position
        /// CamTargetZ - The camera target's Z position
        /// CamUpX - The camera up vector's X component
        /// CamUpY - The camera up vector's Y component
        /// CamUpZ - The camera up vector's Z component
        /// CamNear - the Z value of the Near Plane
        /// CamFar - the Z value of the FarPlane
        /// CamType - {PERSPECTIVE | ORTHOGRAPHIC}
        /// If CameraType = PERSPECTIVE
        ///     CamFOV - The field of view of the camera
        ///     CamAspect - The aspect ratio of the camera
        /// If CameraType = ORTHOGRAHIC
        ///     CamWidth - The width of the camera field
        ///     CamHeight - the Height of the camera field
        /// </summary>
        /// <param name="Context"></param>
        /// <param name="data"></param>
        public override void Init(IExecutableContext Context, DataSet data = null)
        {
            base.Init(Context, data);

            //Get the Position
            Pos.X = double.Parse(data[ComponentConstants.CAMERA_POS_X]);
            Pos.Y = double.Parse(data[ComponentConstants.CAMERA_POS_Y]);
            Pos.Z = double.Parse(data[ComponentConstants.CAMERA_POS_Z]);

            //Get the Target vector
            Target.X = double.Parse(data[ComponentConstants.CAMERA_TARGET_X]);
            Target.Z = double.Parse(data[ComponentConstants.CAMERA_TARGET_Z]);
            Target.Y = double.Parse(data[ComponentConstants.CAMERA_TARGET_Y]);

            //Get the Up vector
            Up.X = double.Parse(data[ComponentConstants.CAMERA_UP_X]);
            Up.Y = double.Parse(data[ComponentConstants.CAMERA_UP_Y]);
            Up.Z = double.Parse(data[ComponentConstants.CAMERA_UP_Z]);

            //Get the near and far planes
            zNear = double.Parse(data[ComponentConstants.CAMERA_ZNEAR]);
            zFar  = double.Parse(data[ComponentConstants.CAMERA_ZFAR]);

            //determine the type of camera the user selected
            cameraType = (ComponentConstants.CAM_TYPES)Enum.Parse(typeof(ComponentConstants.CAM_TYPES), data[ComponentConstants.CAMERA_TYPE]);
            if (cameraType == ComponentConstants.CAM_TYPES.ORTHOGRAPHIC)
            {
                cameraWidth  = double.Parse(data[ComponentConstants.CAMERA_WIDTH]);
                cameraHeight = double.Parse(data[ComponentConstants.CAMERA_HEIGHT]);
            }
            else
            {
                cameraFOV    = double.Parse(data[ComponentConstants.CAMERA_FOV]);
                cameraAspect = double.Parse(data[ComponentConstants.CAMERA_ASPECT]);
            }

            viewPerspectiveMatrix = generateMatrix(Pos, Up, Target, cameraType, zNear, zFar, cameraWidth, cameraHeight, cameraAspect, cameraFOV);
            isDirty = true;
        }