コード例 #1
0
        /// <summary>
        /// OVR initialization
        /// </summary>
        private void OVRInitialization()
        {
            try
            {
                this.adapter.GraphicsDevice.IsSrgbModeEnabled = true;
                var renderTargetManager = this.adapter.Graphics.RenderTargetManager as RenderTargetManager;

                OVRTypes.Result result;

                // Retrieve the DXGI device, in order to set the maximum frame latency.
                using (SharpDX.DXGI.Device1 dxgiDevice = this.device.QueryInterface <SharpDX.DXGI.Device1>())
                {
                    dxgiDevice.MaximumFrameLatency = 1;
                }

                this.ovrLayers   = new Layers();
                this.layerEyeFov = this.ovrLayers.AddLayerEyeFov();

                // Create a set of layers to submit.
                this.eyeProperties       = new VREye[3];
                this.oculusEyePoses      = new OVRTypes.Posef[2];
                this.hmdToEyeViewOffsets = new OVRTypes.Vector3f[2];

                for (int i = 0; i < this.eyeProperties.Length; i++)
                {
                    this.eyeProperties[i] = new VREye();
                }

                result = this.CreateVRSwapTextureSet();
                OculusVRHelpers.WriteErrorDetails(this.Oculus, result, "Failed to create swap texture set.");

                for (int eyeIndex = 0; eyeIndex < 2; eyeIndex++)
                {
                    OVRTypes.EyeType   eye        = (OVRTypes.EyeType)eyeIndex;
                    OculusVREyeTexture eyeTexture = new OculusVREyeTexture();
                    this.eyeProperties[eyeIndex].Texture = eyeTexture;

                    // Retrieve size and position of the texture for the current eye.
                    eyeTexture.FieldOfView           = this.Hmd.DefaultEyeFov[eyeIndex];
                    eyeTexture.NearPlane             = DefaultNearClip;
                    eyeTexture.FarPlane              = DefaultFarClip;
                    eyeTexture.TextureSize           = new OVRTypes.Sizei(this.swapRenderTargets[0].Width, this.swapRenderTargets[0].Height);
                    eyeTexture.RenderDescription     = this.Hmd.GetRenderDesc(eye, this.Hmd.DefaultEyeFov[eyeIndex]);
                    eyeTexture.HmdToEyeViewOffset    = eyeTexture.RenderDescription.HmdToEyeOffset;
                    eyeTexture.ViewportSize.Position = new OVRTypes.Vector2i(this.recommendedTextureSize[0].Width * eyeIndex, 0);
                    eyeTexture.ViewportSize.Size     = this.recommendedTextureSize[eyeIndex];
                    eyeTexture.Viewport              = new Viewport(
                        eyeTexture.ViewportSize.Position.x / (float)this.swapRenderTargets[0].Width,
                        eyeTexture.ViewportSize.Position.y / (float)this.swapRenderTargets[0].Height,
                        eyeTexture.ViewportSize.Size.Width / (float)this.swapRenderTargets[0].Width,
                        eyeTexture.ViewportSize.Size.Height / (float)this.swapRenderTargets[0].Height);

                    this.hmdToEyeViewOffsets[eyeIndex] = eyeTexture.HmdToEyeViewOffset;

                    // Specify the texture to show on the HMD.
                    this.layerEyeFov.ColorTexture[eyeIndex] = this.eyeTextureSwapChain.TextureSwapChainPtr;
                    this.layerEyeFov.Viewport[eyeIndex]     = eyeTexture.ViewportSize;
                    this.layerEyeFov.Fov[eyeIndex]          = eyeTexture.FieldOfView;
                    this.layerEyeFov.Header.Flags           = OVRTypes.LayerFlags.HighQuality;
                }

                // Define the texture used to display the rendered result on the computer monitor.
                OVRTypes.MirrorTextureDesc mirrorTextureDescription = new OVRTypes.MirrorTextureDesc()
                {
                    Format    = OVRTypes.TextureFormat.R8G8B8A8_UNORM_SRGB,
                    Width     = this.Width,
                    Height    = this.Height,
                    MiscFlags = OVRTypes.TextureMiscFlags.None
                };

                OculusWrap.MirrorTexture mirrorTexture;

                // Create the texture used to display the rendered result on the computer monitor.
                result = this.Hmd.CreateMirrorTextureDX(this.device.NativePointer, mirrorTextureDescription, out mirrorTexture);
                OculusVRHelpers.WriteErrorDetails(this.Oculus, result, "Failed to create mirror texture.");

                // Retrieve the Direct3D texture contained in the Oculus MirrorTexture.
                IntPtr mirrorTextureComPtr = IntPtr.Zero;
                result = mirrorTexture.GetBufferDX(this.textureInterfaceId, out mirrorTextureComPtr);
                OculusVRHelpers.WriteErrorDetails(this.Oculus, result, "Failed to retrieve the texture from the created mirror texture buffer.");

                this.mirrorTexture         = new Texture2D(mirrorTextureComPtr);
                this.HMDMirrorRenderTarget = renderTargetManager.CreateRenderTarget(this.mirrorTexture.NativePointer);

                WaveServices.RegisterService(new OculusVRService(this));

                this.IsConnected = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
コード例 #2
0
        /// <summary>
        /// OVR initialization
        /// </summary>
        private void OVRInitialization()
        {
            try
            {
                this.adapter.GraphicsDevice.IsSrgbModeEnabled = true;
                var renderTargetManager = this.adapter.Graphics.RenderTargetManager as RenderTargetManager;

                // Specify which head tracking capabilities to enable.
                this.Hmd.SetEnabledCaps(OVR.HmdCaps.DebugDevice);

                // Start the sensor which informs of the Rift's pose and motion
                this.Hmd.ConfigureTracking(OVR.TrackingCaps.ovrTrackingCap_Orientation | OVR.TrackingCaps.ovrTrackingCap_MagYawCorrection | OVR.TrackingCaps.ovrTrackingCap_Position, OVR.TrackingCaps.None);

                OVR.ovrResult result;

                // Retrieve the DXGI device, in order to set the maximum frame latency.
                using (SharpDX.DXGI.Device1 dxgiDevice = device.QueryInterface <SharpDX.DXGI.Device1>())
                {
                    dxgiDevice.MaximumFrameLatency = 1;
                }

                this.ovrLayers   = new Layers();
                this.layerEyeFov = this.ovrLayers.AddLayerEyeFov();

                // Create a set of layers to submit.
                this.eyeTextures         = new OculusVREyeTexture[2];
                this.eyePoses            = new VREyePose[3];
                this.oculusEyePoses      = new OVR.Posef[2];
                this.hmdToEyeViewOffsets = new OVR.Vector3f[2];

                result = this.CreateVRSwapTextureSet();
                OculusVRHelpers.WriteErrorDetails(this.Oculus, result, "Failed to create swap texture set.");

                for (int eyeIndex = 0; eyeIndex < 2; eyeIndex++)
                {
                    OVR.EyeType        eye        = (OVR.EyeType)eyeIndex;
                    OculusVREyeTexture eyeTexture = new OculusVREyeTexture();
                    this.eyeTextures[eyeIndex] = eyeTexture;

                    // Retrieve size and position of the texture for the current eye.
                    eyeTexture.FieldOfView           = this.Hmd.DefaultEyeFov[eyeIndex];
                    eyeTexture.NearPlane             = DefaultNearClip;
                    eyeTexture.FarPlane              = DefaultFarClip;
                    eyeTexture.TextureSize           = new OVR.Sizei(this.swapRenderTargets[0].Width, this.swapRenderTargets[0].Height);
                    eyeTexture.RenderDescription     = this.Hmd.GetRenderDesc(eye, this.Hmd.DefaultEyeFov[eyeIndex]);
                    eyeTexture.HmdToEyeViewOffset    = eyeTexture.RenderDescription.HmdToEyeViewOffset;
                    eyeTexture.ViewportSize.Position = new OVR.Vector2i(this.recommendedTextureSize[0].Width * eyeIndex, 0);
                    eyeTexture.ViewportSize.Size     = this.recommendedTextureSize[eyeIndex];
                    eyeTexture.Viewport              = new Viewport(
                        eyeTexture.ViewportSize.Position.x / (float)this.swapRenderTargets[0].Width,
                        eyeTexture.ViewportSize.Position.y / (float)this.swapRenderTargets[0].Height,
                        eyeTexture.ViewportSize.Size.Width / (float)this.swapRenderTargets[0].Width,
                        eyeTexture.ViewportSize.Size.Height / (float)this.swapRenderTargets[0].Height,
                        0.0f,
                        1.0f);

                    this.hmdToEyeViewOffsets[eyeIndex] = eyeTexture.HmdToEyeViewOffset;

                    // Specify the texture to show on the HMD.
                    this.layerEyeFov.ColorTexture[eyeIndex] = this.eyeSwapTextureSet.SwapTextureSetPtr;
                    this.layerEyeFov.Viewport[eyeIndex]     = eyeTexture.ViewportSize;
                    this.layerEyeFov.Fov[eyeIndex]          = eyeTexture.FieldOfView;
                    this.layerEyeFov.Header.Flags           = OVR.LayerFlags.HighQuality;
                }

                // Define the texture used to display the rendered result on the computer monitor.
                Texture2DDescription mirrorTextureDescription = new Texture2DDescription();
                mirrorTextureDescription.Width             = this.Width;
                mirrorTextureDescription.Height            = this.Height;
                mirrorTextureDescription.ArraySize         = 1;
                mirrorTextureDescription.MipLevels         = 1;
                mirrorTextureDescription.Format            = Format.R8G8B8A8_UNorm_SRgb;
                mirrorTextureDescription.SampleDescription = new SampleDescription(1, 0);
                mirrorTextureDescription.Usage             = ResourceUsage.Default;
                mirrorTextureDescription.CpuAccessFlags    = CpuAccessFlags.None;
                mirrorTextureDescription.BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget;

                // Convert the SharpDX texture description to the native Direct3D texture description.
                OVR.D3D11.D3D11_TEXTURE2D_DESC mirrorTextureDescriptionD3D11 = OculusVRHelpers.CreateTexture2DDescription(mirrorTextureDescription);
                OculusWrap.D3D11.MirrorTexture mirrorTexture;

                // Create the texture used to display the rendered result on the computer monitor.
                result = this.Hmd.CreateMirrorTextureD3D11(device.NativePointer, ref mirrorTextureDescriptionD3D11, OVR.D3D11.SwapTextureSetD3D11Flags.None, out mirrorTexture);
                OculusVRHelpers.WriteErrorDetails(this.Oculus, result, "Failed to create mirror texture.");

                this.mirrorTexture         = new Texture2D(mirrorTexture.Texture.Texture);
                this.HMDMirrorRenderTarget = renderTargetManager.CreateRenderTarget(this.mirrorTexture.NativePointer);

                WaveServices.RegisterService(new OculusVRService(this));

                this.IsConnected = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }