public void handleKeyboardUp(object sender, KeyboardKeyEventArgs e) { if (e.Key == Key.Escape) { Exit(); } if(e.Key == Key.Space) { OvrDLL.ovr_RecenterTrackingOrigin(session); } if(e.Key == Key.Up) { zoomFactor *= 2.0f; if (zoomFactor > 128.0f) zoomFactor = 128.0f; } if (e.Key == Key.Down) { zoomFactor /= 2.0f; if (zoomFactor < 0.25f) zoomFactor = 0.25f; } }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { base.OnClosing(e); OvrDLL.ovr_DestroyTextureSwapChain(session, eyes[0].swapChain); OvrDLL.ovr_DestroyTextureSwapChain(session, eyes[1].swapChain); OvrDLL.ovr_DestroyMirrorTexture(session, mirror); OvrDLL.ovr_Destroy(session); OvrDLL.ovr_Shutdown(); }
void bindFbo(EyeTarget eye) { UInt32 chainTexId = 0; OvrDLL.ovr_GetTextureSwapChainBufferGL(session, eye.swapChain, -1, ref chainTexId); //-1 for the chain index is the next buffer GL.BindFramebuffer(FramebufferTarget.Framebuffer, eye.fbo); GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, chainTexId, 0); GL.Enable(EnableCap.FramebufferSrgb); GL.Viewport(0, 0, eye.renderTargetSize.Width, eye.renderTargetSize.Height); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); }
protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); //get the time the frame will be displayed on the oculus double displayMidpoint = OvrDLL.ovr_GetPredictedDisplayTime(session, 0); //get the predicted position of the device at that time TrackingState ts = OvrDLL.ovr_GetTrackingState(session, displayMidpoint, true); //calculate eye poses Vector3[] eyeOffsets = new Vector3[2] { eyes[0].desc.HmdToEyeOffset, eyes[1].desc.HmdToEyeOffset }; Posef[] eyePoses = new Posef[2]; OvrDLL.ovr_CalcEyePoses(ts.HeadPose.ThePose, eyeOffsets, eyePoses); //get the orientation of the hmd if it was tracked if (ts.StatusFlags.HasFlag(StatusBits.OrientationTracked)) { eyes[0].pose.Orientation = eyePoses[0].Orientation; eyes[1].pose.Orientation = eyePoses[1].Orientation; } else { eyes[0].pose.Orientation = Quaternion.Identity; eyes[1].pose.Orientation = Quaternion.Identity; } //get the position of the hmd if it was tracked if (ts.StatusFlags.HasFlag(StatusBits.PositionTracked)) { eyes[0].pose.Position = eyePoses[0].Position; eyes[1].pose.Position = eyePoses[1].Position; } else { eyes[0].pose.Position = Vector3.Zero; eyes[1].pose.Position = Vector3.Zero; } }
void initEyeTarget(EyeType eye) { EyeTarget e = new EyeTarget(); e.eye = eye; e.fbo = GL.GenFramebuffer(); e.depthTexture = GL.GenRenderbuffer(); e.desc = OvrDLL.ovr_GetRenderDesc(session, eye, eye == EyeType.Left ? hmdDesc.LeftDefaultEyeFov : hmdDesc.RightDefaultEyeFov); e.renderTargetSize = OvrDLL.ovr_GetFovTextureSize(session, EyeType.Left, hmdDesc.LeftDefaultEyeFov, 1.0f); e.proj = OvrDLL.ovrMatrix4f_Projection(e.desc.Fov, 0.1f, 1000.0f, ProjectionModifier.ClipRangeOpenGL); //create the texture swap chain TextureSwapChainDesc swapDesc = new TextureSwapChainDesc() { Type = TextureType.Texture2D, ArraySize = 1, Format = TextureFormat.R8G8B8A8_UNORM_SRGB, Width = e.renderTargetSize.Width, Height = e.renderTargetSize.Height, MipLevels = 1, SampleCount = 1, StaticImage = 0 }; result = OvrDLL.ovr_CreateTextureSwapChainGL(session, ref swapDesc, ref e.swapChain); if (result < 0) { Console.WriteLine("Error creating swap chain"); OvrDLL.ovr_GetLastErrorInfo(ref error); Console.WriteLine("Last Error Info: {0}-{1}", error.result, error.ErrorString); } int swapChainLength = 0; OvrDLL.ovr_GetTextureSwapChainLength(session, e.swapChain, ref swapChainLength); Console.WriteLine("Swapchain length: {0}", swapChainLength); for(int i = 0; i< swapChainLength; i++) { UInt32 texId = 0; OvrDLL.ovr_GetTextureSwapChainBufferGL(session, e.swapChain, i, ref texId); GL.BindTexture(TextureTarget.Texture2D, texId); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); } int currentIndex = 0; OvrDLL.ovr_GetTextureSwapChainCurrentIndex(session, e.swapChain, ref currentIndex); Console.WriteLine("Swapchain current index: {0}", currentIndex); UInt32 chainTexId = 0; OvrDLL.ovr_GetTextureSwapChainBufferGL(session, e.swapChain, currentIndex, ref chainTexId); GL.BindFramebuffer(FramebufferTarget.Framebuffer, e.fbo); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, e.depthTexture); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent32f, e.renderTargetSize.Width, e.renderTargetSize.Height); GL.FramebufferRenderbuffer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.Depth, RenderbufferTarget.Renderbuffer, e.depthTexture); GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, chainTexId, 0); DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[1] { DrawBuffersEnum.ColorAttachment0 }; GL.DrawBuffers(1, drawBuffers); //check frame buffer completeness FramebufferErrorCode err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if(err != FramebufferErrorCode.FramebufferComplete) { Console.WriteLine("Error in frame buffer: {0}", err); } eyes[(int)eye] = e; GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); }
protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Vector3 camPos = new Vector3(0, 2, 5); Quaternion camOri = Quaternion.Identity; #if RENDER_OCULUS for (int i = 0; i < 2; i++) { //zoom field of view var fov = new FovPort { DownTan = eyes[i].desc.Fov.DownTan / zoomFactor, UpTan = eyes[i].desc.Fov.UpTan / zoomFactor, LeftTan = eyes[i].desc.Fov.LeftTan / zoomFactor, RightTan = eyes[i].desc.Fov.RightTan / zoomFactor }; eyes[i].proj = OvrDLL.ovrMatrix4f_Projection(fov, 0.1f, 1000.0f, ProjectionModifier.ClipRangeOpenGL); //bind eye fbo bindFbo(eyes[i]); //combine the "camera" position/rotation with the position/rotation of the eye Vector3 finalPos = camPos + (camOri * eyes[i].pose.Position); Matrix4 finalRot = Matrix4.CreateFromQuaternion(camOri * eyes[i].pose.Orientation); //create the view matrix with a lookat basis vectors Vector3 up = eyes[i].pose.Orientation * Vector3.UnitY; Vector3 fwd = eyes[i].pose.Orientation * -Vector3.UnitZ; Matrix4 view = Matrix4.LookAt(finalPos, finalPos + fwd, up); //draw the scene drawScene(view, eyes[i].proj); //commit the swapchain OvrDLL.ovr_CommitTextureSwapChain(session, eyes[i].swapChain); } //send to Oculus LayerEyeFov eyeFov = layers[0] as LayerEyeFov; eyeFov.Header.Flags = LayerFlags.TextureOriginAtBottomLeft; eyeFov.ColorTexture[0] = eyes[0].swapChain; eyeFov.ColorTexture[1] = eyes[1].swapChain; eyeFov.Fov[0] = eyes[0].desc.Fov; eyeFov.Fov[1] = eyes[1].desc.Fov; eyeFov.Viewport[0] = new Recti(new Vector2i(0,0), eyes[0].renderTargetSize); eyeFov.Viewport[1] = new Recti(new Vector2i(0, 0), eyes[1].renderTargetSize); eyeFov.RenderPose[0] = eyes[0].pose; eyeFov.RenderPose[1] = eyes[1].pose; ViewScaleDesc viewScale = new ViewScaleDesc(); result = OvrDLL.ovr_SubmitFrame(session, 0, ref viewScale, layers.GetUnmanagedLayers(), 1); if (result < 0) { Console.WriteLine("Error submitting frame"); OvrDLL.ovr_GetLastErrorInfo(ref error); Console.WriteLine("Last Error Info: {0}-{1}", error.result, error.ErrorString); } //blit mirror to fbo OvrDLL.ovr_GetMirrorTextureBufferGL(session, mirror, ref myMirrorTexture); blitMirror((int)myMirrorTexture); #else Matrix4 view = Matrix4.CreateTranslation(-camPos); Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(60.0f), 1.0f, 0.1f, 1000.0f); drawScene(view, proj); #endif SwapBuffers(); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); initGLObjects(); GL.ClearColor(0.2f, 0.2f, 0.2f, 1.0f); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); //initialize the library InitParams initParams = new InitParams() { Flags = InitFlags.None, RequestedMinorVersion = Constants.OVR_MINOR_VERSION, LogCallback = null, UserData = IntPtr.Zero, ConnectionTimeoutMS = 0 }; result = OvrDLL.ovr_Initialize(ref initParams); if (result < 0) { Console.WriteLine("Failed to initialize OVR"); } //create the session result = OvrDLL.ovr_Create(ref session, ref luid); if (result < 0) { Console.WriteLine("Failed to create OVR session"); } //get device description and capabilities hmdDesc = OvrDLL.ovr_GetHmdDesc(session); Console.WriteLine("HMD Type: {0}", hmdDesc.Type); Console.WriteLine("HMD Product Name: {0}", hmdDesc.ProductName); Console.WriteLine("HMD Manufacturer: {0}", hmdDesc.Manufacturer); Console.WriteLine("HMD Serial Number: {0}", hmdDesc.SerialNumber); Console.WriteLine("HMD Resolution {0}x{1}", hmdDesc.Resolution.Width, hmdDesc.Resolution.Height); Console.WriteLine("Version String: {0}", OvrDLL.ovr_GetVersionString()); //get the eye descriptions eyes[0].desc = OvrDLL.ovr_GetRenderDesc(session, EyeType.Left, hmdDesc.LeftDefaultEyeFov); eyes[1].desc = OvrDLL.ovr_GetRenderDesc(session, EyeType.Right, hmdDesc.RightDefaultEyeFov); //get the tracker (the first one) description and then recenter it TrackerDesc trackDesc = OvrDLL.ovr_GetTrackerDesc(session, 0); Console.WriteLine("Tracker 0 description: FOV H: {0} V: {1} Near: {2} Far: {3}", trackDesc.FrustumHFovInRadians, trackDesc.FrustumVFovInRadians, trackDesc.FrustumNearZInMeters, trackDesc.FrustumFarZInMeters); OvrDLL.ovr_RecenterTrackingOrigin(session); Console.WriteLine("Tracker origin recentered"); //create layers layers.AddLayerEyeFov(); // in slot 0 //init each of the eye targets with swap chains/fbo/etc for (int i = 0; i < 2; i++) { initEyeTarget((EyeType)i); } //create the mirror texture MirrorTextureDesc mirrorDesc = new MirrorTextureDesc() { Format = TextureFormat.R8G8B8A8_UNORM_SRGB, Width = 800, Height = 600 }; result = OvrDLL.ovr_CreateMirrorTextureGL(session, ref mirrorDesc, ref mirror); if (result < 0) { Console.WriteLine("Error creating mirror texture"); OvrDLL.ovr_GetLastErrorInfo(ref error); Console.WriteLine("Last Error Info: {0}-{1}", error.result, error.ErrorString); } OvrDLL.ovr_GetMirrorTextureBufferGL(session, mirror, ref myMirrorTexture); Console.WriteLine("OpenGL Mirror texture ID: {0}", myMirrorTexture); }