/// <summary>
        /// Synchronizes the camera to the controller's current state</summary>
        /// <param name="camera">Camera</param>
        protected override void ControllerToCamera(Camera camera)
        {
            Vec3F lookAt = camera.LookAt;
            Vec3F right  = camera.Right;
            Vec3F up     = camera.Up;

            if (camera.ViewType == ViewTypes.Perspective)
            {
                // override the camera's frame of reference
                float sinPhi   = (float)Math.Sin(m_elevation);
                float cosPhi   = (float)Math.Cos(m_elevation);
                float sinTheta = (float)Math.Sin(m_azimuth);
                float cosTheta = (float)Math.Cos(m_azimuth);

                lookAt = new Vec3F(-cosPhi * sinTheta, -sinPhi, -cosPhi * cosTheta);
                right  = new Vec3F(cosTheta, 0, -sinTheta);
                up     = Vec3F.Cross(right, lookAt); // TODO compute from sin/cos values
            }

            float lookAtOffset = 0;

            if (m_distanceFromLookAt < m_dollyThreshold) // do we need to start dollying?
            {
                lookAtOffset = m_distanceFromLookAt - m_dollyThreshold;
            }

            float eyeOffset = m_distanceFromLookAt;

            Camera.Set(m_lookAtPoint - (eyeOffset * lookAt),    // eye point
                       m_lookAtPoint - (lookAtOffset * lookAt), // look-at point
                       up);                                     // up vector

            base.ControllerToCamera(camera);
        }
Example #2
0
        public override bool MouseMove(object sender, MouseEventArgs e)
        {
            if (m_dragging &&
                InputScheme.ActiveControlScheme.IsControllingCamera(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)))
            {
                Control c  = sender as Control;
                float   dx = e.X - m_lastMousePointX;
                float   dy = e.Y - m_lastMousePointY;

                if (InputScheme.ActiveControlScheme.IsRotating(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)) &&
                    (Camera.ViewType == ViewTypes.Perspective || LockOrthographic == false))
                {
                    var orbitRotationSpeed = .0005f * (float)Math.PI;

                    // just do this orbitting calculation in spherical coords...
                    // it's much easier than any other method
                    var orbitCenter = Camera.LookAtPoint;
                    var spherical   = CartesianToSphericalYUp(orbitCenter - Camera.Eye);
                    spherical[0] += dy * orbitRotationSpeed;
                    spherical[0]  = Clamp(spherical[0], (float)Math.PI * 0.02f, (float)Math.PI * 0.98f);
                    spherical[1] += dx * orbitRotationSpeed;

                    var defaultUp = new Vec3F(0.0f, 1.0f, 0.0f);    // (geometry gets hopelessly unnormalized if we don't reset default-up here)
                    Camera.Set(orbitCenter - SphericalToCartesianYUp(spherical), orbitCenter, defaultUp);
                    if (Camera.ViewType != ViewTypes.Perspective)
                    {
                        Camera.ViewType = ViewTypes.Perspective;
                    }
                }
                else if (InputScheme.ActiveControlScheme.IsZooming(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)))
                {
                    float zoom = (-dy - dx);

                    float adj       = Camera.DistanceFromLookAt;
                    var   zoomSpeed = 0.01f * adj;
                    var   lookAtDir = Vec3F.Normalize(Camera.LookAt);
                    var   movement  = Math.Min(zoom * zoomSpeed, Camera.DistanceFromLookAt - 0.1f);
                    Camera.Set(Camera.Eye + lookAtDir * movement, Camera.LookAtPoint, Camera.Up);
                }
                else if (InputScheme.ActiveControlScheme.IsPanning(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)))
                {
                    float adj         = Camera.DistanceFromLookAt;
                    var   panSpeed    = 0.001f * adj;
                    var   lookAtPoint = Camera.LookAtPoint;
                    var   translation = (Camera.Up * dy * panSpeed) + (Camera.Right * -dx * panSpeed);
                    Camera.Set(Camera.Eye + translation, lookAtPoint + translation, Camera.Up);
                }

                m_lastMousePointX = e.Location.X;
                m_lastMousePointY = e.Location.Y;
                return(true);
            }

            return(base.MouseMove(sender, e));
        }
Example #3
0
        public override bool MouseDown(object sender, MouseEventArgs e)
        {
            if (InputScheme.ActiveControlScheme.IsControllingCamera(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)))
            {
                m_lastMousePointX = e.Location.X;
                m_lastMousePointY = e.Location.Y;
                m_dragging        = true;
                return(true);
            }

            if (Control.ModifierKeys.HasFlag(Keys.Control) && !Control.ModifierKeys.HasFlag(Keys.Shift) && e.Button == MouseButtons.Left)
            {
                // "control + l click" repositions the "focus" point of the camera
                //      -- it's just incredibly useful to be able to manually set the point, because it
                //          allows the user to specify both the speed of the movement of the camera and
                //          the orbit of the camera in a natural way
                //
                // We could expand "ActiveControlScheme" to allow this key binding to be rebound...
                // but just using fixed binding for now.
                // This is a very important key combination (it's just really useful to be able to move
                // the focus around quickly) -- so it should be accessable with any easy combination from
                // anywhere!

                ViewControl           c  = sender as ViewControl;
                GUILayer.IViewContext vc = sender as GUILayer.IViewContext;
                if (c != null && vc != null)
                {
                    // We can use XLEBridgeUtils to do the ray test. This will
                    // execute the native code (which in turn performs the intersection
                    // on the GPU)
                    // Note that we're using the more complex picking interface because
                    // we want to use explicitly pass "Camera" (rather than
                    // getting it from the view control)
                    var hit = XLEBridgeUtils.Picking.RayPick(
                        GUILayer.EngineDevice.GetInstance(),
                        vc.SceneManager, vc.TechniqueContext,
                        c.GetWorldRay(e.Location), XLEBridgeUtils.Utils.AsCameraDesc(Camera), c.ClientSize,
                        XLEBridgeUtils.Picking.Flags.AllWorldObjects);
                    if (hit != null && hit.Length > 0)
                    {
                        Vec3F transformedPt;
                        Camera.AxisSystem.Transform(hit[0].hitPt, out transformedPt);
                        Camera.Set(Camera.Eye, transformedPt, Camera.Up);
                    }
                }

                return(true);
            }

            return(base.MouseDown(sender, e));
        }
Example #4
0
        public override bool MouseWheel(object sender, MouseEventArgs e)
        {
            if (!InputScheme.ActiveControlScheme.IsZooming(KeysInterop.ToAtf(Control.ModifierKeys), MouseEventArgsInterop.ToAtf(e)))
            {
                return(true);
            }

            float adj       = Camera.DistanceFromLookAt;
            var   zoomSpeed = .1f / 120.0f * adj;
            var   lookAtDir = Vec3F.Normalize(Camera.LookAt);
            var   movement  = Math.Min(e.Delta * zoomSpeed, Camera.DistanceFromLookAt - 0.1f);

            Camera.Set(Camera.Eye + lookAtDir * movement, Camera.LookAtPoint, Camera.Up);
            return(true);
        }
Example #5
0
            // render the scene.
            public void Render()
            {
                if (GameEngine.IsInError ||
                    SurfaceId == 0 ||
                    Visible == false ||
                    Width == 0 ||
                    Height == 0 ||
                    Game == null)
                {
                    return;
                }


                NativeObjectAdapter gameLevel = GameEngine.GetGameLevel();

                try
                {
                    NativeObjectAdapter game = Game.As <NativeObjectAdapter>();
                    GameEngine.SetGameLevel(game);
                    GameEngine.SetRenderState(m_renderState);
                    if (Game.RootGameObjectFolder.GameObjects.Count > 0)
                    {
                        GameEngine.Update(0, 0, true);
                    }

                    if (ResetCamera)
                    {
                        // save view type
                        ViewTypes viewtype = this.ViewType;
                        ViewType = ViewTypes.Perspective;
                        Size       sz        = ClientSize;
                        float      aspect    = (float)sz.Width / (float)sz.Height;
                        IBoundable boundable = Game.RootGameObjectFolder.Cast <IBoundable>();
                        Sce.Atf.VectorMath.Sphere3F sphere = boundable.BoundingBox.ToSphere();
                        float nearZ = sphere.Radius * 0.01f;
                        nearZ = Math.Min(0.1f, nearZ);
                        Camera.SetPerspective(
                            (float)Math.PI / 4,
                            aspect,
                            nearZ,
                            sphere.Radius * 10.0f);

                        Vec3F camPos = sphere.Center + new Vec3F(sphere.Radius, sphere.Radius, sphere.Radius) * 1.2f;
                        Camera.Set(camPos, sphere.Center, new Vec3F(0, 1, 0));
                        ViewType    = viewtype;
                        ResetCamera = false;
                    }

                    GameEngine.Begin(SurfaceId, Camera.ViewMatrix, Camera.ProjectionMatrix);
                    if (Game.RootGameObjectFolder.GameObjects.Count > 0)
                    {
                        GameEngine.RenderGame();
                    }
                    string str = "View Type: " + ViewType.ToString();
                    GameEngine.DrawText2D(str, Util3D.CaptionFont, 1, 1, Color.White);
                    GameEngine.End();
                }
                finally
                {
                    GameEngine.SetGameLevel(gameLevel);
                }
            }