Example #1
0
        protected void trackball_TrackBallMoved(Object sender, EventArgs e)
        {
            System.Windows.Media.Media3D.Quaternion delta = ((Trackball)sender).Delta;
            Vector3 deltaAxis = new Vector3();

            deltaAxis.X = (float)delta.Axis.X;
            deltaAxis.Y = (float)delta.Axis.Y;
            deltaAxis.Z = (float)delta.Axis.Z;
            deltaAxis.Normalize();
            Vector3 cameraLookDirection = viewport3DImage.CameraTarget - viewport3DImage.CameraPosition;
            Vector3 cameraUpDirection   = viewport3DImage.CameraUpVector;

            cameraLookDirection.Normalize();
            // Subtract any component of cameraUpDirection along cameraLookDirection
            cameraUpDirection = cameraUpDirection - Vector3.Multiply(cameraLookDirection, Vector3.Dot(cameraUpDirection, cameraLookDirection));
            cameraUpDirection.Normalize();
            Vector3 cameraX = Vector3.Cross(cameraLookDirection, cameraUpDirection);
            // Get axis of rotation in the camera coordinates
            Vector3 deltaAxisWorld = Vector3.Multiply(cameraX, deltaAxis.X) +
                                     Vector3.Multiply(cameraUpDirection, deltaAxis.Y) +
                                     Vector3.Multiply(cameraLookDirection, -deltaAxis.Z);

            SharpDX.Matrix cameraTransform = SharpDX.Matrix.RotationAxis(deltaAxisWorld, (float)(delta.Angle * Math.PI / 180.0));
            viewport3DImage.CameraTarget   = Vector3.Transform(viewport3DImage.CameraTarget, cameraTransform).ToVector3();
            viewport3DImage.CameraPosition = Vector3.Transform(viewport3DImage.CameraPosition, cameraTransform).ToVector3();
            viewport3DImage.CameraUpVector = Vector3.Transform(viewport3DImage.CameraUpVector, cameraTransform).ToVector3();
            double newPhi = FindPhi();

            if (newPhi != lastPhi)
            {
                lastPhi = newPhi;
                axes.UpdateOpenSides(newPhi);
            }
        }
Example #2
0
        public void Initialize()
        {
            Background        = null;
            viewport3DControl = new Viewport3DControl();
            viewport3DControl.SetValue(Grid.ZIndexProperty, 100);
            this.Children.Add(viewport3DControl);

            viewport3DImage = viewport3DControl.Viewport3DImage;
            if (viewport3DImage == null)
            {
                axes = new Axes3D();
                SetValue(ModelsProperty, new Model3DCollection(null));
                return;
            }
            viewport3DImage.ViewPort3D = this;

            SetValue(ModelsProperty, viewport3DImage.Models);
            // Set the owner of the Model3DCollection to be the D3DImageViewport
            // This ensures that the Model3D objects are rendered by the D3DImageViewport
            viewport3DImage.SetLayer2D(viewport3DImage.Canvas, GraphToWorld);

            viewport3DImage.Models.Changed += new Model3DCollection.ItemEventHandler(Models_Changed);

            trackball                       = new Trackball();
            trackball.EventSource           = viewport3DControl; //viewport3DImage.Canvas;
            trackball.OnTrackBallMoved     += new TrackballEventHandler(trackball_TrackBallMoved);
            trackball.OnTrackBallZoom      += new TrackballEventHandler(trackball_OnTrackBallZoom);
            trackball.OnTrackBallTranslate += new TrackballEventHandler(trackball_OnTrackBallTranslate);

            axes = new Axes3D();
            viewport3DImage.Models.Add(axes);

            viewport3DImage.CameraPosition = new Vector3(-3f, -3f, 2f);
            viewport3DImage.CameraTarget   = new Vector3(0f, 0f, 0f);
            //
            Binding bindingGraphMin = new Binding("GraphMin");

            bindingGraphMin.Source = this;
            bindingGraphMin.Mode   = BindingMode.TwoWay;
            BindingOperations.SetBinding(axes, Axes3D.GraphMinProperty, bindingGraphMin);
            Binding bindingGraphMax = new Binding("GraphMax");

            bindingGraphMax.Source = this;
            bindingGraphMax.Mode   = BindingMode.TwoWay;
            BindingOperations.SetBinding(axes, Axes3D.GraphMaxProperty, bindingGraphMax);
            Binding bindingGraphToWorld = new Binding("GraphToWorld");

            bindingGraphToWorld.Source = this;
            bindingGraphToWorld.Mode   = BindingMode.OneWay;
            BindingOperations.SetBinding(viewport3DImage, ViewportImage.ModelToWorldProperty, bindingGraphToWorld);
            ////
            GraphMax = new Point3D(1, 1, 1);
            GraphMin = new Point3D(-1, -1, -1);
            WorldMin = new Point3D(-1, -1, -1);
            WorldMax = new Point3D(1, 1, 1);
            axes.UpdateOpenSides(FindPhi());

            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(Viewport3D_IsVisibleChanged);
        }
Example #3
0
        public void Initialize()
        {
            Background = null;
            viewport3DControl = new Viewport3DControl();
            viewport3DControl.SetValue(Grid.ZIndexProperty, 100);
            this.Children.Add(viewport3DControl);

            viewport3DImage = viewport3DControl.Viewport3DImage;
            if (viewport3DImage == null)
            {
                axes = new Axes3D();
                SetValue(ModelsProperty, new Model3DCollection(null));
                return;
            }
            viewport3DImage.ViewPort3D = this;

            SetValue(ModelsProperty, viewport3DImage.Models);
            // Set the owner of the Model3DCollection to be the D3DImageViewport
            // This ensures that the Model3D objects are rendered by the D3DImageViewport
            viewport3DImage.SetLayer2D(viewport3DImage.Canvas, GraphToWorld);

            viewport3DImage.Models.Changed += new Model3DCollection.ItemEventHandler(Models_Changed);

            trackball = new Trackball();
            trackball.EventSource = viewport3DControl; //viewport3DImage.Canvas;
            trackball.OnTrackBallMoved += new TrackballEventHandler(trackball_TrackBallMoved);
            trackball.OnTrackBallZoom += new TrackballEventHandler(trackball_OnTrackBallZoom);
            trackball.OnTrackBallTranslate += new TrackballEventHandler(trackball_OnTrackBallTranslate);

            axes = new Axes3D();
            viewport3DImage.Models.Add(axes);

            viewport3DImage.CameraPosition = new Vector3(-3f, -3f, 2f);
            viewport3DImage.CameraTarget = new Vector3(0f, 0f, 0f);
            //
            Binding bindingGraphMin = new Binding("GraphMin");
            bindingGraphMin.Source = this;
            bindingGraphMin.Mode = BindingMode.TwoWay;
            BindingOperations.SetBinding(axes, Axes3D.GraphMinProperty, bindingGraphMin);
            Binding bindingGraphMax = new Binding("GraphMax");
            bindingGraphMax.Source = this;
            bindingGraphMax.Mode = BindingMode.TwoWay;
            BindingOperations.SetBinding(axes, Axes3D.GraphMaxProperty, bindingGraphMax);
            Binding bindingGraphToWorld = new Binding("GraphToWorld");
            bindingGraphToWorld.Source = this;
            bindingGraphToWorld.Mode = BindingMode.OneWay;
            BindingOperations.SetBinding(viewport3DImage, ViewportImage.ModelToWorldProperty, bindingGraphToWorld);
            ////
            GraphMax = new Point3D(1, 1, 1);
            GraphMin = new Point3D(-1, -1, -1);
            WorldMin = new Point3D(-1, -1, -1);
            WorldMax = new Point3D(1, 1, 1);
            axes.UpdateOpenSides(FindPhi());

            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(Viewport3D_IsVisibleChanged);
        }