private void ToggleCameraAnimation()
 {
     if (_targetPositionCamera.IsRotating)
     {
         _targetPositionCamera.StopRotation();
         animateButton.Text = "Start animation";
     }
     else
     {
         _targetPositionCamera.StartRotation(10, 0); // animate the camera with changing heading for 10 degrees in one second
         animateButton.Text = "Stop animation";
     }
 }
        private void UIThreadCameraRotationButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (_targetPositionCamera.IsRotating)
            {
                _targetPositionCamera.StopRotation();
                UIThreadCameraRotationButton.Content = "Start camera rotate";

                BGThreadCameraRotationButton.IsEnabled = true;
            }
            else
            {
                // When the rotation is stared we need to synchronize the camera's data
                UpdateMainUICamera();

                _targetPositionCamera.StartRotation(40, 0);
                UIThreadCameraRotationButton.Content = "Stop camera rotate";

                BGThreadCameraRotationButton.IsEnabled = false;
            }
        }
Beispiel #3
0
        private void CreateDXViewportView(bool isDirectXOverlay)
        {
            _mainViewport3D = new Viewport3D();

            _lightsGroup = new Model3DGroup();
            _mainViewport3D.Children.Add(_lightsGroup.CreateModelVisual3D());

            UpdateLightingMode();

            _camera1 = new TargetPositionCamera()
            {
                TargetPosition   = new Point3D(0, 0, 0),
                Heading          = 30,
                Attitude         = -10,
                Distance         = 1500,
                ShowCameraLight  = ShowCameraLightType.Never,
                TargetViewport3D = _mainViewport3D
            };

            _mouseCameraController = new MouseCameraController()
            {
                RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed,
                MoveCameraConditions   = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed | MouseCameraController.MouseAndKeyboardConditions.ControlKey,
                EventsSourceElement    = ViewportBorder,
                TargetCamera           = _camera1
            };

            RootGrid.Children.Add(_camera1);
            RootGrid.Children.Add(_mouseCameraController);


            _mainDXViewportView = new DXViewportView(_mainViewport3D)
            {
                BackgroundColor  = Colors.White,
                PresentationType = isDirectXOverlay ? DXView.PresentationTypes.DirectXOverlay : DXView.PresentationTypes.DirectXImage
            };

            var maxBackgroundThreadsCount = (int)ThreadsCountSlider.Value;

            _mainDXViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs args)
            {
                if (_mainDXViewportView.DXScene != null)
                {
                    _mainDXViewportView.DXScene.MaxBackgroundThreadsCount = maxBackgroundThreadsCount;
                }
            };

            _mainDXViewportView.SceneRendered += MainDxViewportViewOnSceneRendered;

            ViewportBorder.Child = _mainDXViewportView;

            _camera1.StartRotation(40, 0);


            // Notify MainWindow about a new MainDXViewportView - so we can open DiagnosticsWindow
            if (_parentWindow != null)
            {
                ((MainWindow)_parentWindow).UnsubscribeLastShownDXViewportView();
                ((MainWindow)_parentWindow).SubscribeDXViewportView(_mainDXViewportView);

                ((MainWindow)_parentWindow).MaxBackgroundThreadsCount = maxBackgroundThreadsCount;  // prevent setting MaxBackgroundThreadsCount from the setting dialog
            }
        }
        public MultiThreadingSample()
        {
            InitializeComponent();

            IsCachingCommandListsInfoControl.InfoText =
                @"When checked then Ab3d.DXEngine records all DirectX commands into DirectX CommandLists and
reuses the command lists on next frame if only camera and light properties have changed.

In this case only the new camera data is loaded into the graphics card and then all the previously rendered
CommandLists are replayed. This way the rendering performance is greatly improved because the render time is almost zero 
(this is the time to prepare and process the DirectX commands by the Ab3d.DXEngine and by the DirectX and drives).

Note that command list caching works only for non-transparent standard geometry objects with WpfMaterial or StandardMaterial.

In this sample you can see command list caching in action with observing the DrawRenderTime.
When command list caching is used, then this time is very small because there is no need to set all DirectX
rendering states and invoke DirectX draw calls. The time for that can be observed when caching is disabled.";

            _currentLightingMode = LightingMode.DirectionalLight;
            _currentObjectsType  = ObjectsTypes.MultiColorBoxes;

            // Create DXViewportView in code behind because we need to support changing PresentationType and
            // this requires that the DXViewportView is recreated from scratch.
            CreateDXViewportView(isDirectXOverlay: PresentationTypeComboBox.SelectedIndex == 0);

            int maxBackgroundThreadsCount = Environment.ProcessorCount - 1;

            ThreadsCountSlider.Maximum = maxBackgroundThreadsCount;
            ThreadsCountSlider.Value   = maxBackgroundThreadsCount;

            ObjectsTypeComboBox.ItemsSource   = Enum.GetNames(typeof(ObjectsTypes));
            ObjectsTypeComboBox.SelectedValue = _currentObjectsType.ToString();

            var possibleObjectCounts = new int[] { 100, 200, 500, 1000, 2500, 5000, 10000, 20000, 40000, 80000, 160000, 320000 };

            ObjectsCountComboBox.ItemsSource   = possibleObjectCounts;
            ObjectsCountComboBox.SelectedIndex = Array.IndexOf(possibleObjectCounts, 20000);

            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                CreateTestScene(possibleObjectCounts[ObjectsCountComboBox.SelectedIndex]);

                _camera1.StartRotation(45, 0);
            }
            finally
            {
                Mouse.OverrideCursor = null;
            }

            _drawRenderTimes     = new List <double>();
            _completeRenderTimes = new List <double>();
            _totalRenderTimes    = new List <double>();


            // Enable collecting statistics
            DXDiagnostics.IsCollectingStatistics = true;

            StatisticsTitleInfoControl.InfoText =
                @"DrawRenderTime: shows time that is needed to draw all objects for a frames (calling DirectX Draw methods and any needed state change methods). Those operations are done in multiple threads so this time is significantly reduced when using multiple threads.

CompleteRenderTime: shows time that is needed to complete the rendering (resolve anti-aliasing, resolve stereoscopic images, calling Present on SwapChain (when using DirectXOverlay) or waiting for the graphics card to finish rendering the frame (when using DirectXImage)). When using DirectXImage this time can increase significantly when using multi-threading because graphics card cannot render as fast as the draw commands are issued.

TotalTimeTextBlock: shows time that is spend in DXEngine to render one frame. Based on this time a theoretical FPS (frames per second) is calculated. It shows how many frames per second could be show when there were no other limitations (for example WPF's frames limit).

WPF FPS: shows number of frames per second in this WPF application (WPF has a cap on 60 frames per second).";


            this.Loaded += delegate(object sender, RoutedEventArgs args)
            {
                _parentWindow = Window.GetWindow(this);

                if (_parentWindow != null)
                {
                    _parentWindow.PreviewKeyDown += ParentWindowOnPreviewKeyDown;
                }
            };

            this.Unloaded += delegate(object sender, RoutedEventArgs args)
            {
                if (_disposables != null)
                {
                    _disposables.Dispose();
                    _disposables = null;
                }

                if (_parentWindow != null)
                {
                    _parentWindow.PreviewKeyDown -= ParentWindowOnPreviewKeyDown;
                }

                DisposeDXViewportView();
            };
        }