protected override void Terminate() { if (hmd != null) { hmd.Dispose(); } OVR.Shutdown(); }
public Matrix GetProjectionMatrix(EyeType eyeType, float nearPlane = 0.1f, float farPlane = 10000f) { Matrix result; Matrix projection = OVR.MatrixProjection(this.eyeRenderDesc[(int)eyeType].Fov, nearPlane, farPlane, true); Matrix.Transpose(ref projection, out result); return(result); }
/// <summary> /// OVRRender renderer /// </summary> protected override void Render() { int currentIndex = 0; if (this.IsConnected) { OVR.FrameTiming frameTiming = this.Hmd.GetFrameTiming(0); OVR.TrackingState trackingState = this.Hmd.GetTrackingState(frameTiming.DisplayMidpointSeconds); // Calculate the position and orientation of each eye. this.Oculus.CalcEyePoses(trackingState.HeadPose.ThePose, this.hmdToEyeViewOffsets, ref this.oculusEyePoses); trackingState.CameraPose.Position.ToVector3(out this.trackerCameraPose.Position); trackingState.CameraPose.Orientation.ToQuaternion(out this.trackerCameraPose.Orientation); currentIndex = this.eyeSwapTextureSet.CurrentIndex++; for (int eyeIndex = 0; eyeIndex < 2; eyeIndex++) { this.oculusEyePoses[eyeIndex].Position.ToVector3(out this.eyePoses[eyeIndex].Position); this.oculusEyePoses[eyeIndex].Orientation.ToQuaternion(out this.eyePoses[eyeIndex].Orientation); this.eyeTextures[eyeIndex].RenderTarget = (this.msaaSampleCount > 1) ? this.msaaRenderTarget : this.swapRenderTargets[currentIndex]; // Get eye projection OVR.ovrMatrix4f_Projection(this.eyeTextures[eyeIndex].FieldOfView, this.eyeTextures[eyeIndex].NearPlane, this.eyeTextures[eyeIndex].FarPlane, OVR.ProjectionModifier.RightHanded).ToMatrix(out this.eyePoses[eyeIndex].Projection); this.layerEyeFov.RenderPose[eyeIndex] = this.oculusEyePoses[eyeIndex]; } // Calc central position Vector3.Lerp(ref this.eyePoses[0].Position, ref this.eyePoses[1].Position, 0.5f, out this.eyePoses[2].Position); Quaternion.Lerp(ref this.eyePoses[0].Orientation, ref this.eyePoses[1].Orientation, 0.5f, out this.eyePoses[2].Orientation); } base.Render(); if (this.IsConnected) { if (this.msaaSampleCount > 1) { var rtManager = this.adapter.Graphics.RenderTargetManager as RenderTargetManager; var dxRt = rtManager.TargetFromHandle <DXRenderTarget>(this.msaaRenderTarget.TextureHandle); var dxSwapRt = rtManager.TargetFromHandle <DXRenderTarget>(this.swapRenderTargets[currentIndex].TextureHandle); this.context.ResolveSubresource(dxRt.Target, 0, dxSwapRt.Target, 0, Format.R8G8B8A8_UNorm_SRgb); } // Submit frame to HMD this.Hmd.SubmitFrame(0, this.ovrLayers); if (this.ShowHMDMirrorTexture) { // Show mirror texture into BackBuffer this.device.ImmediateContext.CopyResource(this.mirrorTexture, this.backBuffer); } } }
/// <summary> /// Converts an ovrQuatf to a Wave Quaternion. /// </summary> /// <param name="ovrQuatf">The ovr quaternion</param> /// <returns>The Wave Quaternion</returns> public static Quaternion ToQuaternion(OVR.Quaternionf ovrQuatf) { return new Quaternion(ovrQuatf.X, ovrQuatf.Y, ovrQuatf.Z, ovrQuatf.W); }
/// <summary> /// Write out any error details received from the Oculus SDK, into the debug output window. /// Please note that writing text to the debug output window is a slow operation and will affect performance, /// if too many messages are written in a short timespan. /// </summary> /// <param name="oculus">OculusWrap object for which the error occurred.</param> /// <param name="result">Error code to write in the debug text.</param> /// <param name="message">Error message to include in the debug text.</param> public static void WriteErrorDetails(Wrap oculus, OVR.ovrResult result, string message) { if (result >= OVR.ovrResult.Success) { return; } // Retrieve the error message from the last occurring error. OVR.ovrErrorInfo errorInformation = oculus.GetLastError(); string formattedMessage = string.Format("{0}. \nMessage: {1} (Error code={2})", message, errorInformation.ErrorString, errorInformation.Result); throw new Exception(formattedMessage); }
protected override void Initialize() { hmd = null; // Initialize OVR OVR.Initialize(); // Create the HMD hmd = OVR.HmdCreate(0); if (this.IsConnected = (hmd != null)) { this.IsConnected = true; } else { hmd = OVR.HmdCreateDebug(HMDType.DK1); this.IsConnected = false; } // Attach HMD to windows hmd.AttachToWindow(this.application.NativeWindows); //var renderTargetSize = hmd.GetDefaultRenderTargetSize(0.5f); var renderTargetSize = new Size2(1280, 800); // Create our render target renderTarget = WaveServices.GraphicsDevice.RenderTargets.CreateRenderTarget(renderTargetSize.Width, renderTargetSize.Height); var InternalRenderTarget = application.Adapter.Graphics.RenderTargetManager.TargetFromHandle <DXRenderTarget>(renderTarget.TextureHandle); // The viewport sizes Rect[] eyeRenderViewport = new Rect[2]; eyeRenderViewport[0] = new Rect(0, 0, renderTargetSize.Width / 2, renderTargetSize.Height); eyeRenderViewport[1] = new Rect((renderTargetSize.Width + 1) / 2, 0, renderTargetSize.Width / 2, renderTargetSize.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 = InternalRenderTarget.NativeTexture; eyeTexture[0].pSRView = InternalRenderTarget.NativeResourceView; // Right eye uses the same texture, but different rendering viewport eyeTexture[1] = eyeTexture[0]; eyeTexture[1].Header.RenderViewport = eyeRenderViewport[1]; // Configure d3d11 D3D11ConfigData d3d11cfg = new D3D11ConfigData(); d3d11cfg.Header.API = RenderAPIType.D3D11; d3d11cfg.Header.RTSize = hmd.Resolution; d3d11cfg.Header.Multisample = 1; d3d11cfg.pDevice = this.graphicsDevice.NativeDevice; d3d11cfg.pDeviceContext = this.graphicsDevice.NativeContext; d3d11cfg.pBackBufferRT = this.graphicsDevice.NativeBackBuffer; d3d11cfg.pSwapChain = this.graphicsDevice.NativeSwapChain; // Configure rendering eyeRenderDesc = new EyeRenderDesc[2]; if (!hmd.ConfigureRendering(d3d11cfg, DistortionCapabilities.Chromatic | DistortionCapabilities.TimeWarp | DistortionCapabilities.Vignette, hmd.DefaultEyeFov, eyeRenderDesc)) { throw new Exception("Invalid configure rendering"); } // Set enabled capabilities hmd.EnabledCaps = HMDCapabilities.LowPersistence | HMDCapabilities.DynamicPrediction; // Configure tracking hmd.ConfigureTracking(TrackingCapabilities.Orientation | TrackingCapabilities.Position | TrackingCapabilities.MagYawCorrection, TrackingCapabilities.None); // Dimiss the Health and Safety Windows hmd.DismissHSWDisplay(); // Get HMD output this.application.SetAdapterOutput(hmd.DeviceName); }