private void UpdateShadowSettings()
        {
            if (MainDXViewportView.DXScene == null) // Probably using WPF 3D rendering
            {
                return;
            }

            // When the ShadowMapSize or ShadowDepthBluringSize is changed,
            // we need to dispose the current VarianceShadowRenderingProvider and create a new one
            if (_varianceShadowRenderingProvider != null &&
                (_varianceShadowRenderingProvider.ShadowMapSize != _shadowMapSize ||
                 _varianceShadowRenderingProvider.ShadowDepthBluringSize != _shadowDepthBluringSize))
            {
                MainDXViewportView.DXScene.InitializeShadowRendering(null);

                _varianceShadowRenderingProvider.Dispose();
                _varianceShadowRenderingProvider = null;
            }

            if (_varianceShadowRenderingProvider == null)
            {
                _varianceShadowRenderingProvider = new VarianceShadowRenderingProvider()
                {
                    ShadowMapSize          = _shadowMapSize,
                    ShadowDepthBluringSize = _shadowDepthBluringSize,
                    ShadowThreshold        = _shadowThreshold,
                    ShadowDepthBias        = _shadowDepthBias
                };

                MainDXViewportView.DXScene.InitializeShadowRendering(_varianceShadowRenderingProvider);
            }
            else
            {
                // ShadowThreshold can be changed without reinitializing the shadow rendering
                _varianceShadowRenderingProvider.ShadowThreshold = _shadowThreshold;
                _varianceShadowRenderingProvider.ShadowDepthBias = _shadowDepthBias;
            }

            UpdateCastingShadowLight();


            MainDXViewportView.Refresh(); // Render the scene again
        }
Beispiel #2
0
        public ShadowRenderingSample()
        {
            InitializeComponent();

            // Default values can be get before Loaded event
            _shadowMapSize          = 512;
            _shadowDepthBluringSize = 4;
            _shadowTreshold         = 0.2f;


            CreateCustomScene();
            CreateLights();
            CreateLightSphere();


            _isShadowEnabled = true;

            UpdateCastingShadowLight();


            MainDXViewportView.DXSceneInitialized += delegate(object sender, EventArgs args)
            {
                if (MainDXViewportView.DXScene == null)
                {
                    return; // Probably WPF 3D rendering
                }
                // The DXScene.InitializeShadowRendering method must be called with the VarianceShadowRenderingProvider.
                // The VarianceShadowRenderingProvider controls the rendering of shadows.
                //
                // Variance shadow rendering technique can produce nice shadows with soft edges and without the artifacts that are common for some other shadow rendering (Peter panning or Self shadowing).
                // More info can be read in the GPU Gems3 "Chapter 8. Summed-Area Variance Shadow Maps" (https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch08.html)
                _varianceShadowRenderingProvider = new VarianceShadowRenderingProvider();


                // ShadowMapSize represents the size of a shadow depth map texture. For example value 512 means that a 512 x 512 texture will be used.
                // The shadow depth map texture is used to store depth information - distance of a pixel from the light.
                // Bigger texture will produce more detailed shadows but will be slower to render.
                // Also, to bigger texture will require bigger blur amount to achieve nice soft edges.
                // NOTE: Changing the ShadowMapSize after the VarianceShadowRenderingProvider has been initialized has no effect - see UpdateShadowSettings method in this file to see how to change the value
                _varianceShadowRenderingProvider.ShadowMapSize = _shadowMapSize;

                // ShadowDepthBluringSize specifies the blur amount that is applied on the shadow depth map and can produce shadows with nice soft edges.
                // Default value is 4.
                _varianceShadowRenderingProvider.ShadowDepthBluringSize = _shadowDepthBluringSize;

                // ShadowTreshold is a float value that helps prevent light bleeding (having areas that should be in shadow fully illuminated) for variance shadow mapping.
                // The value is used to map all shadow values from 0 ... ShadowTreshold to 0 and then linearly rescale the values from ShadowTreshold to 1 into 0 to 1.
                // For more info see "Shadow bleeding" in "Chapter 8. Summed-Area Variance Shadow Maps" (https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch08.html)
                _varianceShadowRenderingProvider.ShadowTreshold = _shadowTreshold;


                // Initialize shadow rendering
                MainDXViewportView.DXScene.InitializeShadowRendering(_varianceShadowRenderingProvider);



                // When the scene is initialized, we can get the SceneNode that is created from WPF teapot model.
                // This will be used later (in OnObjectsFilterValueChanged) to enable or disable casting and receiving shadow.
                _teapotSceneNode = MainDXViewportView.GetSceneNodeForWpfObject(_teapotModel) as WpfGeometryModel3DNode;


                // To use FilterObjectsFunction to filter which objects are casting and receiving shadow, uncomment the lines below (see comment in OnObjectsFilterValueChanged method for more info):
                //_varianceShadowRenderingProvider.RenderShadowDepthRenderingStep.FilterObjectsFunction = FilterNonTeapotObjectsFunction;
                //_varianceShadowRenderingProvider.RenderNonShadowObjectsRenderingStep.FilterObjectsFunction = FilterTeapotObjectsFunction;
                //MainDXViewportView.DXScene.DefaultRenderObjectsRenderingStep.FilterObjectsFunction = FilterNonTeapotObjectsFunction;

                // IMPORTANT:
                // Af least one light need to set to cast shadow. This can be done with the following code:
                //_directionalLight.SetDXAttribute(DXAttributeType.IsCastingShadow, true);
            };


            StartLightAnimation();

            this.Unloaded += delegate(object sender, RoutedEventArgs args)
            {
                StopLightAnimation();

                if (_varianceShadowRenderingProvider != null)
                {
                    _varianceShadowRenderingProvider.Dispose();
                }

                MainDXViewportView.Dispose();
            };



            this.PreviewKeyDown += OnPreviewKeyDown;

            // This will allow receiving keyboard events
            this.Focusable = true;
            this.Focus();
        }
Beispiel #3
0
        public DucksLakeDemo()
        {
            InitializeComponent();

            MouseCameraControllerInfo1.AddCustomInfoLine(0, MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, "Select duck");


            _duckModel3D = LoadDuckModel();

            _eventManager3D = new Ab3d.Utilities.EventManager3D(MainViewport);
            _eventManager3D.CustomEventsSourceElement = ViewportBorder;


            MainDXViewportView.DXSceneInitialized += delegate(object sender, EventArgs args)
            {
                if (MainDXViewportView.DXScene == null)
                {
                    return; // Probably WPF 3D rendering
                }
                // Setup shadow rendering
                _varianceShadowRenderingProvider = new VarianceShadowRenderingProvider();

                // Because we have a big green plane, we need to increase the shadow map size (increase this more to get a more detailed shadow).
                _varianceShadowRenderingProvider.ShadowMapSize = 1024;

                MainDXViewportView.DXScene.InitializeShadowRendering(_varianceShadowRenderingProvider);

                // Specify the light that cases shadow(note that point light is not supported - only DirectionalLight and SpotLight are supported).
                MainSceneLight.SetDXAttribute(DXAttributeType.IsCastingShadow, true);
            };


            this.Focusable       = true; // by default Page is not focusable and therefore does not receive keyDown event
            this.PreviewKeyDown += OnPreviewKeyDown;
            this.Focus();


            // We need to synchronize the Camera in OverlayViewport with the camera in the MainViewport
            Camera1.CameraChanged += delegate(object s, CameraChangedRoutedEventArgs args)
            {
                OverlayViewport.Camera = MainViewport.Camera;
            };

            this.Loaded += delegate(object sender, RoutedEventArgs args)
            {
                GenerateRandomDucks(10);

                ShowModelMover();
            };

            // IMPORTANT:
            // It is very important to call Dispose method on DXSceneView after the control is not used any more (see help file for more info)
            this.Unloaded += delegate(object sender, RoutedEventArgs args)
            {
                if (_varianceShadowRenderingProvider != null)
                {
                    _varianceShadowRenderingProvider.Dispose();
                }

                MainDXViewportView.Dispose();
            };
        }
        private void InitializeOvrAndDirectX()
        {
            if (UseOculusRift)
            {
                // Initializing Oculus VR is very simple when using OculusWrapVirtualRealityProvider
                // First we create an instance of OculusWrapVirtualRealityProvider
                _oculusRiftVirtualRealityProvider = new OculusWrapVirtualRealityProvider(_ovr, multisamplingCount: 4);

                try
                {
                    // Then we initialize Oculus OVR and create a new DXDevice that uses the same adapter (graphic card) as Oculus Rift
                    _dxDevice = _oculusRiftVirtualRealityProvider.InitializeOvrAndDXDevice(requestedOculusSdkMinorVersion: 17);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to initialize the Oculus runtime library.\r\nError: " + ex.Message, "Oculus error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                string ovrVersionString = _ovr.GetVersionString();
                _originalWindowTitle = string.Format("DXEngine OculusWrap Sample (OVR v{0})", ovrVersionString);
                this.Title           = _originalWindowTitle;


                // Reset tracking origin at startup
                _ovr.RecenterTrackingOrigin(_oculusRiftVirtualRealityProvider.SessionPtr);
            }
            else
            {
                // Create DXDevice that will be used to create DXViewportView
                var dxDeviceConfiguration = new DXDeviceConfiguration();
                dxDeviceConfiguration.DriverType             = DriverType.Hardware;
                dxDeviceConfiguration.SupportedFeatureLevels = new FeatureLevel[] { FeatureLevel.Level_11_0 }; // Oculus requires at least feature level 11.0

                _dxDevice = new DXDevice(dxDeviceConfiguration);
                _dxDevice.InitializeDevice();

                _originalWindowTitle = this.Title;
            }



            // Create WPF's Viewport3D
            _viewport3D = new Viewport3D();

            // Create DXViewportView - a control that will enable DirectX 11 rendering of standard WPF 3D content defined in Viewport3D.
            // We use a specified DXDevice that was created by the _oculusRiftVirtualRealityProvider.InitializeOvrAndDXDevice (this way the same adapter is used by Oculus and DXEngine).
            _dxViewportView = new DXViewportView(_dxDevice, _viewport3D);

            _dxViewportView.BackgroundColor = Colors.Aqua;

            // Currently DXEngine support showing Oculus mirror texture only with DirectXOverlay presentation type (not with DirectXImage)
            _dxViewportView.PresentationType = DXView.PresentationTypes.DirectXOverlay;

            if (UseOculusRift)
            {
                // The _dxViewportView will show Oculus mirrow window.
                // The mirror window can be any size, for this sample we use 1/2 the HMD resolution.
                _dxViewportView.Width  = _oculusRiftVirtualRealityProvider.HmdDescription.Resolution.Width / 2.0;
                _dxViewportView.Height = _oculusRiftVirtualRealityProvider.HmdDescription.Resolution.Height / 2.0;
            }


            // When the DXViewportView is initialized, we set the _oculusRiftVirtualRealityProvider to the DXScene object
            _dxViewportView.DXSceneInitialized += delegate(object sender, EventArgs args)
            {
                if (_dxViewportView.UsedGraphicsProfile.DriverType != GraphicsProfile.DriverTypes.Wpf3D &&
                    _dxViewportView.DXScene != null &&
                    _oculusRiftVirtualRealityProvider != null)
                {
                    // Initialize Virtual reality rendering
                    _dxViewportView.DXScene.InitializeVirtualRealityRendering(_oculusRiftVirtualRealityProvider);


                    // Initialized shadow rendering (see Ab3d.DXEngine.Wpf.Samples project - DXEngine/ShadowRenderingSample for more info
                    _varianceShadowRenderingProvider = new VarianceShadowRenderingProvider()
                    {
                        ShadowMapSize          = 1024,
                        ShadowDepthBluringSize = 2,
                        ShadowTreshold         = 0.2f
                    };

                    _dxViewportView.DXScene.InitializeShadowRendering(_varianceShadowRenderingProvider);
                }
            };


            // Enable collecting rendering statistics (see _dxViewportView.DXScene.Statistics class)
            DXDiagnostics.IsCollectingStatistics = true;

            // Subscribe to SceneRendered to collect FPS statistics
            _dxViewportView.SceneRendered += DXViewportViewOnSceneRendered;

            // Add _dxViewportView to the RootGrid
            // Before that we resize the window to be big enough to show the mirrored texture
            this.Width  = _dxViewportView.Width + 30;
            this.Height = _dxViewportView.Height + 50;

            RootGrid.Children.Add(_dxViewportView);


            // Create FirstPersonCamera
            _camera = new FirstPersonCamera()
            {
                TargetViewport3D = _viewport3D,
                Position         = new Point3D(0, 1, 4),
                Heading          = 0,
                Attitude         = 0,
                ShowCameraLight  = ShowCameraLightType.Never
            };

            RootGrid.Children.Add(_camera);


            // Initialize XBOX controller that will control the FirstPersonCamera
            _xInputCameraController = new XInputCameraController();
            _xInputCameraController.TargetCamera  = _camera;
            _xInputCameraController.MovementSpeed = 0.02;
            _xInputCameraController.MoveVerticallyWithDPadButtons = true;

            // We handle the rotation by ourself to prevent rotating the camera up and down - this is done only by HMD
            _xInputCameraController.RightThumbChanged += delegate(object sender, XInputControllerThumbChangedEventArgs e)
            {
                // Apply only horizontal rotation
                _camera.Heading += e.NormalizedX * _xInputCameraController.RotationSpeed;

                // Mark the event as handled
                e.IsHandled = true;
            };

            _xInputCameraController.StartCheckingController();


            // Now we can create our sample 3D scene
            CreateSceneObjects();

            // Add lights
            var lightsVisual3D = new ModelVisual3D();
            var lightsGroup    = new Model3DGroup();

            var directionalLight = new DirectionalLight(Colors.White, new Vector3D(0.5, -0.3, -0.3));

            directionalLight.SetDXAttribute(DXAttributeType.IsCastingShadow, true); // Set this light to cast shadow
            lightsGroup.Children.Add(directionalLight);

            var ambientLight = new AmbientLight(System.Windows.Media.Color.FromRgb(30, 30, 30));

            lightsGroup.Children.Add(ambientLight);

            lightsVisual3D.Content = lightsGroup;
            _viewport3D.Children.Add(lightsVisual3D);


            // Start rendering
            if (RenderAt90Fps)
            {
                // WPF do not support rendering at more the 60 FPS.
                // But with a trick where a rendering loop is created in a background thread, it is possible to achieve more than 60 FPS.
                // In case of sumbiting frames to Oculus Rift, the ovr.SubmitFrame method will limit rendering to 90 FPS.
                //
                // NOTE:
                // When using DXEngine, it is also possible to render the scene in a background thread.
                // This requires that the 3D scene is also created in the background thread and that the events and other messages are
                // passed between UI and background thread in a thread safe way. This is too complicated for this simple sample project.
                // To see one possible implementation of background rendering, see the BackgroundRenderingSample in the Ab3d.DXEngine.Wpf.Samples project.
                var backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += (object sender, DoWorkEventArgs args) =>
                {
                    // Create an action that will be called by Dispatcher
                    var refreshDXEngineAction = new Action(() =>
                    {
                        UpdateScene();

                        // Render DXEngine's 3D scene again
                        if (_dxViewportView != null)
                        {
                            _dxViewportView.Refresh();
                        }
                    });

                    while (_dxViewportView != null && !_dxViewportView.IsDisposed)                                                       // Render until window is closed
                    {
                        if (_oculusRiftVirtualRealityProvider != null && _oculusRiftVirtualRealityProvider.LastSessionStatus.ShouldQuit) // Stop rendering - this will call RunWorkerCompleted where we can quit the application
                        {
                            break;
                        }

                        // Sleep for 1 ms to allow WPF tasks to complete (for example handling XBOX controller events)
                        System.Threading.Thread.Sleep(1);

                        // Call Refresh to render the DXEngine's scene
                        // This is a synchronous call and will wait until the scene is rendered.
                        // Because Oculus is limited to 90 fps, the call to ovr.SubmitFrame will limit rendering to 90 FPS.
                        Dispatcher.Invoke(refreshDXEngineAction);
                    }
                };

                backgroundWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs args)
                {
                    if (_oculusRiftVirtualRealityProvider != null && _oculusRiftVirtualRealityProvider.LastSessionStatus.ShouldQuit)
                    {
                        this.Close(); // Exit the application
                    }
                };

                backgroundWorker.RunWorkerAsync();
            }
            else
            {
                // Subscribe to WPF rendering event (called approximately 60 times per second)
                CompositionTarget.Rendering += CompositionTargetOnRendering;
            }
        }