Пример #1
0
        protected void InitializeHMD(GraphicsDevice graphicsDevice, IntPtr windowHandle)
        {
            // Attach the HMD to window to direct back buffer output from the window to the HMD
            HMD.AttachToWindow(windowHandle);

            // Configure DirectX 11
            var device   = (SharpDX.Direct3D11.Device)graphicsDevice;
            var d3D11Cfg = new D3D11ConfigData
            {
                Header =
                {
                    API            = RenderAPIType.D3D11,
                    BackBufferSize = HMD.Resolution,
                    Multisample    = 1
                },
                pDevice        = device.NativePointer,
                pDeviceContext = device.ImmediateContext.NativePointer,
                pBackBufferRT  = ((RenderTargetView)graphicsDevice.BackBuffer).NativePointer,
                pSwapChain     = ((SwapChain)graphicsDevice.Presenter.NativePresenter).NativePointer
            };

            // Configure the HMD's rendering settings
            EyeRenderDesc = new EyeRenderDesc[2];
            if (!HMD.ConfigureRendering(d3D11Cfg, DistortionCapabilities.Chromatic | DistortionCapabilities.TimeWarp, HMD.DefaultEyeFov, EyeRenderDesc))
            {
                throw new Exception("Failed to configure HMD");
            }

            // IPD
            EyeOffset[0] = EyeRenderDesc[0].HmdToEyeViewOffset;
            EyeOffset[1] = EyeRenderDesc[1].HmdToEyeViewOffset;

            // Enable low persistence and dynamic prediction features
            HMD.EnabledCaps = HMDCapabilities.LowPersistence | HMDCapabilities.DynamicPrediction;
            // Enable all DK2 tracking features
            HMD.ConfigureTracking(TrackingCapabilities.Orientation | TrackingCapabilities.Position | TrackingCapabilities.MagYawCorrection, TrackingCapabilities.None);
            // Dismiss the Heatlh and Safety Window
            HMD.DismissHSWDisplay();

            // Get HMD output display
            var adapter   = (Adapter)graphicsDevice.Adapter;
            var hmdOutput = adapter.Outputs.FirstOrDefault(o => HMD.DeviceName.StartsWith(o.Description.DeviceName, StringComparison.OrdinalIgnoreCase));

            if (hmdOutput != null)
            {
                // Set game to fullscreen on rift
                var swapChain   = (SwapChain)graphicsDevice.Presenter.NativePresenter;
                var description = swapChain.Description.ModeDescription;
                swapChain.ResizeTarget(ref description);
                swapChain.SetFullscreenState(true, hmdOutput);
            }
        }
Пример #2
0
        protected override void Initialize()
        {
            if (Thread.CurrentThread.Name == null)
            {
                Thread.CurrentThread.Name = "TelepresenceSystem thread";
            }
            //            INIT_TP_window();
            //            INIT_TP_renderTarget();
            //            INIT_TP_eyeTextureRendering();
            //            INIT_TP_d3d11();
            //            INIT_TP_hmd();

            //            base.Initialize();

            //            INIT_txu();
            //            //INIT_TP_text();

            // Modify the title of the window
            Window.Title = "RiftGame";

            // Attach HMD to window
            var control = (System.Windows.Forms.Control)Window.NativeWindow;

            hmd.AttachToWindow(control.Handle);

            // Create our render target
            var renderTargetSize = hmd.GetDefaultRenderTargetSize(1.5f);

            renderTarget = RenderTarget2D.New(GraphicsDevice, renderTargetSize.Width, renderTargetSize.Height,
                                              new MipMapCount(1), PixelFormat.B8G8R8X8.UNorm, TextureFlags.RenderTarget | TextureFlags.ShaderResource);
            //    new MipMapCount(1), PixelFormat.R8G8B8A8.UNorm, TextureFlags.RenderTarget | TextureFlags.ShaderResource);
            renderTargetView   = (RenderTargetView)renderTarget;
            renderTargetSRView = (ShaderResourceView)renderTarget;

            // Create a depth stencil buffer for our render target
            depthStencilBuffer = DepthStencilBuffer.New(GraphicsDevice, renderTargetSize.Width, renderTargetSize.Height, DepthFormat.Depth32, true);

            // Adjust render target size if there were any hardware limitations
            renderTargetSize.Width  = renderTarget.Width;
            renderTargetSize.Height = renderTarget.Height;

            // The viewport sizes are re-computed in case renderTargetSize changed
            eyeRenderViewport    = new Rect[2];
            eyeRenderViewport[0] = new Rect(0, 0, renderTargetSize.Width / 2, renderTargetSize.Height);
            eyeRenderViewport[1] = new Rect((renderTargetSize.Width + 1) / 2, 0, eyeRenderViewport[0].Width, eyeRenderViewport[0].Height);

            // Create our eye texture data
            eyeTexture = new D3D11TextureData[2];
            eyeTexture[0].Header.API            = RenderAPIType.D3D11;
            eyeTexture[0].Header.TextureSize    = renderTargetSize;
            eyeTexture[0].Header.RenderViewport = eyeRenderViewport[0];
            eyeTexture[0].pTexture = ((SharpDX.Direct3D11.Texture2D)renderTarget).NativePointer;
            eyeTexture[0].pSRView  = renderTargetSRView.NativePointer;

            // Right eye uses the same texture, but different rendering viewport
            eyeTexture[1] = eyeTexture[0];
            eyeTexture[1].Header.RenderViewport = eyeRenderViewport[1];

            // Configure d3d11
            var             device   = (SharpDX.Direct3D11.Device)GraphicsDevice;
            D3D11ConfigData d3d11cfg = new D3D11ConfigData();

            d3d11cfg.Header.API            = RenderAPIType.D3D11;
            d3d11cfg.Header.BackBufferSize = hmd.Resolution;
            d3d11cfg.Header.Multisample    = 1;
            d3d11cfg.pDevice        = device.NativePointer;
            d3d11cfg.pDeviceContext = device.ImmediateContext.NativePointer;
            d3d11cfg.pBackBufferRT  = ((RenderTargetView)GraphicsDevice.BackBuffer).NativePointer;
            d3d11cfg.pSwapChain     = ((SharpDX.DXGI.SwapChain)GraphicsDevice.Presenter.NativePresenter).NativePointer;

            // Configure rendering
            eyeRenderDesc = new EyeRenderDesc[2];
            if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.Chromatic | DistortionCapabilities.TimeWarp, hmd.DefaultEyeFov, eyeRenderDesc))
            //if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.None, hmd.DefaultEyeFov, eyeRenderDesc))
            //if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.TimeWarp, hmd.DefaultEyeFov, eyeRenderDesc))
            {
                throw new Exception("Failed to configure rendering");
            }

            // Set enabled capabilities
            hmd.EnabledCaps = HMDCapabilities.LowPersistence | HMDCapabilities.DynamicPrediction;

            // Configure tracking
            hmd.ConfigureTracking(TrackingCapabilities.Orientation | TrackingCapabilities.Position | TrackingCapabilities.MagYawCorrection, TrackingCapabilities.None);

            // Dismiss the Heatlh and Safety Window
            hmd.DismissHSWDisplay();

            // Get HMD output
            var adapter   = (Adapter)GraphicsDevice.Adapter;
            var hmdOutput = adapter.Outputs.FirstOrDefault(o => hmd.DeviceName.StartsWith(o.Description.DeviceName, StringComparison.OrdinalIgnoreCase));

            if (hmdOutput != null)
            {
                // Set game to fullscreen on rift
                var swapChain   = (SwapChain)GraphicsDevice.Presenter.NativePresenter;
                var description = swapChain.Description.ModeDescription;
                swapChain.ResizeTarget(ref description);
                swapChain.SetFullscreenState(true, hmdOutput);
            }

            //GraphicsDevice.SetRasterizerState(GraphicsDevice.RasterizerStates.CullBack);
            GraphicsDevice.SetRasterizerState(GraphicsDevice.RasterizerStates.CullFront);


            base.Initialize();
        }