Esempio n. 1
0
        public DXViewPort()
        {
            try
            {
                // required
                InitializeComponent();

                ReCenter();

                DeviceManager.Instance.Viewport = this;
                DeviceManager.Instance.StartDirect3D();

                // set up viewport cameras
                pic3DTopLeft.Camera = CameraFactory.Instance.CreateCamera(PredefinedCameras.Top);

                pic3DBotLeft.Camera = CameraFactory.Instance.CreateCamera(PredefinedCameras.Left);

                pic3DTopRight.Camera = CameraFactory.Instance.CreateCamera(PredefinedCameras.Front);

                pic3DBotRight.Camera   = CameraFactory.Instance.CreateCamera(PredefinedCameras.Perspective);
                pic3DBotRight.DrawMode = DrawMode.solid;
            }
            catch (Exception e)
            {
                MessageBox.Show("The dxViewPort is broken. (Details below.)\n\n" + e.Message +
                                "\n\n\n" + e.StackTrace, "dxViewPort NRE ERROR! (probably)", MessageBoxButtons.AbortRetryIgnore,
                                MessageBoxIcon.Hand);
            }

            try
            {
                // render to display changes
                DeviceManager.UpdateViews();
            }
            catch
            {
                // ignore failed render
            }
        }
Esempio n. 2
0
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (!GetAsyncKeyState(ArcBall.CameraControlKey) && (e.Button == MouseButtons.Left) && selectedObjects.Count != 0)
            {
                if (_editMode == EditMode.Rotate)
                {
                    Vector3 direction  = camera.TargetPoint - camera.EyePosition;
                    Vector3 horizontal = Vector3.Cross(direction, camera.UpVector);
                    Vector3 upVector   = camera.UpVector;

                    horizontal.Normalize();
                    upVector.Normalize();

                    float rotateX = (0.5f * (mouseDownX - e.X)) * upVector.X +
                                    (0.5f * (e.Y - mouseDownY)) * horizontal.X + _startRotation.X;

                    float rotateY = (0.5f * (mouseDownX - e.X)) * upVector.Y +
                                    (0.5f * (e.Y - mouseDownY)) * horizontal.Y + _startRotation.Y;

                    float rotateZ = (0.5f * (mouseDownX - e.X)) * upVector.Z +
                                    (0.5f * (e.Y - mouseDownY)) * horizontal.Z + _startRotation.Z;

                    Midget.Events.EventFactory.Instance.GenerateTransformationRequestEvent(this, selectedObjects,
                                                                                           new AxisValue(rotateX, rotateY, rotateZ),
                                                                                           Midget.Events.Object.Transformation.Transformation.Rotate);

                    mouseDownX = e.X;
                    mouseDownY = e.Y;

                    dm.UpdateViews();
                }
                else if (_editMode == EditMode.Move)
                {
                    // our picking
                    Vector3 pickRayOrigin    = new Vector3();
                    Vector3 pickRayDir       = new Vector3();
                    Vector3 pickRayOriginNew = new Vector3();
                    Vector3 pickRayDirNew    = new Vector3();

                    Vector3 direction  = camera.TargetPoint - camera.EyePosition;
                    Vector3 horizontal = Vector3.Cross(direction, camera.UpVector);
                    Vector3 upVector   = camera.UpVector;

                    horizontal.Normalize();
                    upVector.Normalize();

                    // grab the last selected object
                    IObject3D selObj = (IObject3D)selectedObjects[selectedObjects.Count - 1];

                    // original mouse position
                    camera.UnProjectCoordinates(mouseDownX, mouseDownY, this.Width,
                                                this.Height, ref pickRayOrigin, ref pickRayDir);

                    // new mouse position
                    camera.UnProjectCoordinates(e.X,
                                                e.Y, this.Width,
                                                this.Height, ref pickRayOriginNew, ref pickRayDirNew);

                    // transform by world and object model space
                    pickRayOrigin.TransformCoordinate(Matrix.Invert(camera.WorldMatrix));
                    pickRayOriginNew.TransformCoordinate(Matrix.Invert(camera.WorldMatrix));

                    // find the movement vector
                    Vector3 result = pickRayOriginNew - pickRayOrigin;

                    // pretty crappy compensation
                    if (this.Camera is PerspectiveMidgetCamera)
                    {
                        result *= 10.0f;
                    }

                    // generate an axis value to move by
                    AxisValue axes = new AxisValue(
                        selObj.Translation.X + result.X,
                        selObj.Translation.Y + result.Y,
                        selObj.Translation.Z + result.Z);

                    Midget.Events.EventFactory.Instance.GenerateTransformationRequestEvent(this, selectedObjects,
                                                                                           axes,
                                                                                           Midget.Events.Object.Transformation.Transformation.Translate);

                    mouseDownX = e.X;
                    mouseDownY = e.Y;

                    dm.UpdateViews();
                }
                else if (_editMode == EditMode.Scale)
                {
                    float scaleX = 0.05f * (mouseDownX - e.X) + _startScaling.X;
                    float scaleY = 0.05f * (mouseDownY - e.Y) + _startScaling.Y;

                    Midget.Events.EventFactory.Instance.GenerateTransformationRequestEvent(this, selectedObjects,
                                                                                           new AxisValue(scaleY, scaleY, scaleY),
                                                                                           Midget.Events.Object.Transformation.Transformation.Scale);

                    mouseDownX = e.X;
                    mouseDownY = e.Y;

                    dm.UpdateViews();
                }
            }

            base.OnMouseMove(e);
        }