Beispiel #1
0
        protected override void Draw()
        {
            // Ensure transforms are updated and clear; otherwise leave to Model3D tree
            GraphicsDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(1.0f, 1.0f, 1.0f, 1.0f), 1.0f, 0);
            GraphicsDevice.BeginScene();
            float aspect = (float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height;

            switch (this.ViewPort3D.ProjectionType)
            {
            case ProjectionType.Perspective:
                projection = Matrix.PerspectiveFovRH(fov, aspect, 0.01f, 10000f);
                break;

            case ProjectionType.Orthogonal:
                float width  = (float)this.Models.Select(m => m.Bounds.Maximum.X).Max() * aspect;
                float height = (float)this.Models.Select(m => m.Bounds.Maximum.Y).Max();
                projection = Matrix.OrthoRH(width / this.Scale, height / this.Scale, 1, 100);
                break;
            }

            view  = Matrix.LookAtRH(CameraPosition, CameraTarget, CameraUpVector);
            world = Matrix.Identity;
            // ENDTODO
            GraphicsDevice.SetTransform(TransformState.Projection, ref projection);
            GraphicsDevice.SetTransform(TransformState.View, ref view);
            GraphicsDevice.SetTransform(TransformState.World, ref world);

            foreach (Model3D model in this.Models)
            {
                model.Draw();
            }
            GraphicsDevice.EndScene();
            GraphicsDevice.Present();
        }
Beispiel #2
0
        public MainWindow()
        {
            InitializeComponent();

            Loaded += (sender, args) =>
                      Catch(() =>
            {
                _manager = new DxManager(SharpDxElement);
                Prepare();
            });


            SharpDxElement.OnResized +=
                (source, e) =>
                Catch(
                    () =>

                    _proj =
                        Matrix.PerspectiveFovRH(
                            MathUtil.PiOverFour,
                            (float)SharpDxElement.ActualWidth / (float)SharpDxElement.ActualHeight,
                            0.1f,
                            1000.0f)

                    );


            new DispatcherTimer
            {
                Interval  = TimeSpan.FromMilliseconds(15),
                IsEnabled = true,
            }.Tick += (sender, args) => Draw();



            Closing += (sender, args) => Clean();

            Point popPos = default(Point);

            _distSum = 40;

            SharpDxElement.MouseDown += (s, a) => popPos = a.GetPosition(this);

            SharpDxElement.MouseMove += (s, a) =>
            {
                if (a.LeftButton == MouseButtonState.Released)
                {
                    return;
                }
                Point position = a.GetPosition(this);

                float viewRotationAngleY = -(float)(position.Y - popPos.Y) / 80f;
                float viewRotationAngleZ = -(float)(position.X - popPos.X) / 80f;

                _viewRotation = Quaternion.RotationAxis(Vector3.UnitY, viewRotationAngleY) * Quaternion.RotationAxis(Vector3.UnitZ, viewRotationAngleZ) * _viewRotation;

                popPos = position;
            };

            SharpDxElement.MouseWheel += (sender, args) => _distSum = Math.Max(3f, _distSum - args.Delta / 10f);
        }