/// <summary> /// Create object for an existing overlay by overlay key. Useful to gain access to the stock overlays. /// </summary> /// <param name="pchOverlayKey"></param> public Overlay(string pchOverlayKey) { EVROverlayError ovrErr = EVROverlayError.None; ovrErr = SteamVR_WebKit.OverlayManager.FindOverlay(pchOverlayKey, ref _handle); if (ovrErr != EVROverlayError.None) { throw new Exception("Failed to create overlay: " + ovrErr.ToString()); } _key = pchOverlayKey; StringBuilder sb = new StringBuilder(); SteamVR_WebKit.OverlayManager.GetOverlayName(_handle, new StringBuilder(), 1000, ref ovrErr); _name = sb.ToString(); SteamVR_WebKit.OverlayManager.GetOverlayWidthInMeters(_handle, ref _width); SteamVR_WebKit.OverlayManager.GetOverlayAlpha(_handle, ref _alpha); }
void CreateOverlayInSteamVR() { EVROverlayError ovrErr = EVROverlayError.None; if (NexHudEngine.OverlayManager == null) { throw new Exception("NexHud Engine is not started"); } if (_ingame) { ovrErr = NexHudEngine.OverlayManager.CreateOverlay(Key, Name, ref _handle); // ToggleInput(false); } else { ovrErr = NexHudEngine.OverlayManager.CreateDashboardOverlay(Key, Name, ref _handle, ref _thumbnailHandle); // ToggleInput(true); } NexHudEngine.Log("Overlay Handle " + _handle + ", Thumbnail Handle: " + _thumbnailHandle); if (ovrErr != EVROverlayError.None) { throw new Exception("Failed to create overlay: " + ovrErr.ToString()); } NexHudEngine.OverlayManager.SetOverlayColor(_handle, 1.0f, 1.0f, 1.0f); if (_hasBackSide) { ovrErr = NexHudEngine.OverlayManager.CreateOverlay(Key + "_backside", Name, ref _backSideHandle); if (ovrErr != EVROverlayError.None) { throw new Exception("Failed to create backside of overlay: " + ovrErr.ToString()); } NexHudEngine.OverlayManager.SetOverlayColor(_backSideHandle, 1.0f, 1.0f, 1.0f); HmdMatrix34_t matrix = GetMatrixFromPositionAndRotation(new Vector3(0, 0, 0), new Vector3(0, 180, 0)); NexHudEngine.OverlayManager.SetOverlayTransformOverlayRelative(_backSideHandle, _handle, ref matrix); } Alpha = 1.0f; // Because it'll be upside down otherwise. VRTextureBounds_t bounds; bounds.vMax = 0; bounds.vMin = 1; // Flip the Y // Leave as defaults bounds.uMin = 0; bounds.uMax = 1; NexHudEngine.OverlayManager.SetOverlayTextureBounds(_handle, ref bounds); if (_hasBackSide) { bounds.uMin = 1; bounds.uMax = 0; // Flip the backside texture NexHudEngine.OverlayManager.SetOverlayTextureBounds(_backSideHandle, ref bounds); } }
public void SetDeviceAttachment(uint index, Vector3 position, Vector3 rotation) { if (!_ingame) { throw new Exception("Cannot set attachment for dashboard overlay"); } _position = position; _rotation = rotation; HmdMatrix34_t matrix = GetMatrixFromPositionAndRotation(position, rotation); EVROverlayError err = NexHudEngine.OverlayManager.SetOverlayTransformTrackedDeviceRelative(_handle, index, ref matrix); if (err != EVROverlayError.None) { NexHudEngine.Log("Failed to attach " + Key + " to Device " + index + " failed: " + err.ToString()); } }
public void SetAttachment(AttachmentType attachmentType, Vector3 position, Vector3 rotation, string attachmentKey = null) { if (!_ingame) { throw new Exception("Cannot set attachment for dashboard overlay"); } _attachmentType = attachmentType; _position = position; _rotation = rotation; if (attachmentType == AttachmentType.Absolute) { HmdMatrix34_t matrix = GetMatrixFromPositionAndRotation(position, rotation); SteamVR_WebKit.OverlayManager.SetOverlayTransformAbsolute(_handle, ETrackingUniverseOrigin.TrackingUniverseStanding, ref matrix); _sentAttachmentSuccess = true; } else if (attachmentType == AttachmentType.Hmd) { SetDeviceAttachment((uint)0, position, rotation); _sentAttachmentSuccess = true; } else if (attachmentType == AttachmentType.Overlay) { ulong attachmentHandle = 0; if (attachmentType == AttachmentType.Overlay && attachmentKey == null) { attachmentKey = _attachedTo; } if (_attachedTo != attachmentKey) { SteamVR_WebKit.OverlayManager.FindOverlay(attachmentKey, ref attachmentHandle); _attachedToHandle = attachmentHandle; } if (_attachedToHandle != 0) { HmdMatrix34_t matrix = GetMatrixFromPositionAndRotation(position, rotation); EVROverlayError err = SteamVR_WebKit.OverlayManager.SetOverlayTransformOverlayRelative(_handle, _attachedToHandle, ref matrix); if (err != EVROverlayError.None) { SteamVR_WebKit.Log("Failed to attach " + Key + " to Overlay " + attachmentKey + " failed: " + err.ToString()); } _sentAttachmentSuccess = true; } else { SteamVR_WebKit.Log("Attempted to attach to " + attachmentKey + " but it could not be found."); } } else if (attachmentType == AttachmentType.AbsoluteRelative) { ulong attachmentHandle = 0; if (attachmentType == AttachmentType.Overlay && attachmentKey == null) { attachmentKey = _attachedTo; } if (_attachedTo != attachmentKey) { SteamVR_WebKit.OverlayManager.FindOverlay(attachmentKey, ref attachmentHandle); _attachedToHandle = attachmentHandle; } if (_attachedToHandle != 0) { HmdMatrix34_t matrix = GetMatrixFromPositionAndRotation(position, rotation); HmdMatrix34_t parentMatrix = new HmdMatrix34_t(); uint parentWidth = 0, parentHeight = 0; SteamVR_WebKit.OverlayManager.GetOverlayTextureSize(_attachedToHandle, ref parentWidth, ref parentHeight); EVROverlayError err = SteamVR_WebKit.OverlayManager.GetTransformForOverlayCoordinates(_attachedToHandle, ETrackingUniverseOrigin.TrackingUniverseStanding, new HmdVector2_t() { v0 = parentWidth / 2, v1 = parentHeight / 2 }, ref parentMatrix); if (err != EVROverlayError.None) { SteamVR_WebKit.Log("Failed to retrieve overlay position for " + _attachedTo); return; } HmdMatrix34_t resultMatrix = TransformUtils.OpenTKMatrixToOpenVRMatrix(TransformUtils.OpenVRMatrixToOpenTKMatrix(parentMatrix) * TransformUtils.OpenVRMatrixToOpenTKMatrix(matrix)); err = SteamVR_WebKit.OverlayManager.SetOverlayTransformAbsolute(_handle, ETrackingUniverseOrigin.TrackingUniverseStanding, ref resultMatrix); if (err != EVROverlayError.None) { SteamVR_WebKit.Log("Failed to attach " + Key + " to Overlay " + attachmentKey + " failed: " + err.ToString()); } _sentAttachmentSuccess = true; } else { SteamVR_WebKit.Log("Attempted to attach to " + attachmentKey + " but it could not be found."); } } else { SetDeviceAttachment(SteamVR_WebKit.OVRSystem.GetTrackedDeviceIndexForControllerRole(attachmentType == AttachmentType.LeftController ? ETrackedControllerRole.LeftHand : ETrackedControllerRole.RightHand), position, rotation); if (!_controllerListenersSetup) { SteamVR_Event.Listen("TrackedDeviceRoleChanged", HandleDeviceRoleChanged); SteamVR_Event.Listen("device_connected", HandleDeviceConnected); _controllerListenersSetup = true; } else { _sentAttachmentSuccess = true; } } }
private bool StartupOVR() { EVRInitError error = EVRInitError.Driver_Failed; vr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay); if (error != EVRInitError.None) { Debug.WriteLine("OpenVR Init Error: " + error.ToString()); return(false); } overlay = OpenVR.Overlay; EVROverlayError overlayError = overlay.CreateOverlay("Jstf_ovr_cards", "OpenVR Cards System Overlay", ref overlayHandle); if (overlayError != EVROverlayError.None) { OpenVR.Shutdown(); Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString()); return(false); } /*overlayError = overlay.SetOverlayFromFile(overlayHandle, AssetsPath + "overlay.png"); * if (overlayError != EVROverlayError.None) * { * CleanupOVR(); * Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString()); * return false; * }*/ overlayError = overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.Mouse); if (overlayError != EVROverlayError.None) { CleanupOVR(); Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString()); return(false); } overlayTexture = new Texture_t { eType = ETextureType.OpenGL, eColorSpace = EColorSpace.Auto, handle = (IntPtr)textureId }; overlayError = overlay.SetOverlayTexture(overlayHandle, ref overlayTexture); if (overlayError != EVROverlayError.None) { CleanupOVR(); Debug.WriteLine("OpenVR Overlay Error: " + overlayError.ToString()); return(false); } HmdId = OpenVR.k_unTrackedDeviceIndex_Hmd; for (uint i = HmdId + 1; i < OpenVR.k_unMaxTrackedDeviceCount; i++) { if (vr.IsTrackedDeviceConnected(i)) { ETrackedDeviceClass cls = vr.GetTrackedDeviceClass(i); if (cls == ETrackedDeviceClass.Controller) { ETrackedControllerRole rl = vr.GetControllerRoleForTrackedDeviceIndex(i); if (rl == ETrackedControllerRole.LeftHand) { LeftControllerId = i; Debug.WriteLine("Found Left Controller"); } else if (rl == ETrackedControllerRole.RightHand) { RightControllerId = i; Debug.WriteLine("Found Right Controller"); } } } } mat = new HmdMatrix34_t { m0 = 1, m1 = 0, m2 = 0, m3 = 0f, m4 = 0, m5 = 1, m6 = 0, m7 = 0.1f, m8 = 0, m9 = 0, m10 = 1, m11 = 0f }; overlayError = overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, RightControllerId, ref mat); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot bind overlay to Tracked device."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); return(false); } overlayError = overlay.SetOverlayWidthInMeters(overlayHandle, 0.2f); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot set overlay size."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); return(false); } overlayError = overlay.SetOverlayAlpha(overlayHandle, 1); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot set overlay alpha."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); return(false); } overlayError = overlay.SetOverlayColor(overlayHandle, 1, 1, 1); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot set overlay color."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); return(false); } #if DEBUG Debug.WriteLine("OpenVR Startup Complete"); #endif return(true); }
private void RenderRoutine() { StartupGL(); if (!StartupOVR()) { CleanupGL(); return; } EVROverlayError overlayError = overlay.ShowOverlay(overlayHandle); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot show overlay."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); CleanupGL(); return; } while (RenderAlive) { DateTime begin = DateTime.Now; GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); if (CurrentCard == null) { if (CardQueue.TryDequeue(out Card c)) { if (c != null) { CurrentCard = c; continue; } } Thread.Sleep(500); } else { DrawCard(CurrentCard); GL.Clear(ClearBufferMask.DepthBufferBit); if (CurrentCard.Status == CardStatus.Hidden) { CurrentCard = null; Debug.WriteLine("Dropping card."); } overlayError = overlay.SetOverlayTexture(overlayHandle, ref overlayTexture); if (overlayError != EVROverlayError.None) { Debug.WriteLine("Cannot set texture."); Debug.WriteLine("Error: " + overlayError.ToString()); CleanupOVR(); CleanupGL(); return; } } if (EchoWindow) { GL.ClearColor(1, 1, 1, 0); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.DrawBuffer(DrawBufferMode.Back); GL.Viewport(0, 0, render_window.Width, render_window.Height); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Disable(EnableCap.Blend); EchoScene.Instance.Draw(); render_window.SwapBuffers(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId); GL.Enable(EnableCap.Blend); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); GL.Viewport(0, 0, textureSizeX, textureSizeY); GL.ClearColor(0, 0, 0, 0); } VREvent_t ev = new VREvent_t(); if (overlay.PollNextOverlayEvent(overlayHandle, ref ev, (uint)Marshal.SizeOf(ev.GetType()))) { switch ((EVREventType)ev.eventType) { case EVREventType.VREvent_OverlayShown: Debug.WriteLine("Overlay shown."); break; case EVREventType.VREvent_OverlayHidden: Debug.WriteLine("Overlay Hidden."); break; } } if (vr.PollNextEvent(ref ev, (uint)Marshal.SizeOf(ev.GetType()))) { switch ((EVREventType)ev.eventType) { case EVREventType.VREvent_Quit: StopRender(); break; case EVREventType.VREvent_DriverRequestedQuit: StopRender(); break; } } DateTime end = DateTime.Now; double ms = (end - begin).TotalMilliseconds; Thread.Sleep((int)(Math.Max(12.0 - ms, 0))); } CleanupOVR(); CleanupGL(); }
static void Main(string[] args) { /* Get an invisible GL context real quick :> */ var window = new GameWindow(300, 300); var vr = SteamVR.instance; var error = EVRInitError.None; OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay); if (error != EVRInitError.None) { throw new Exception("An error occured while initializing OpenVR!"); } OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error); if (error != EVRInitError.None) { throw new Exception("An error occured while initializing Compositor!"); } OpenVR.GetGenericInterface(OpenVR.IVROverlay_Version, ref error); if (error != EVRInitError.None) { throw new Exception("An error occured while initializing Overlay!"); } var hmd = OpenVR.System; var compositor = OpenVR.Compositor; var overlay = OpenVR.Overlay; ulong overlayHandle = 0; // Non-dashboard overlay EVROverlayError overlayError = overlay.CreateOverlay("overlayTest", "HL3", ref overlayHandle); // Dashboard overlay // ulong thumbnailHandle = 0; // overlay.SetOverlayFromFile(thumbnailHandle, @"image.png"); // EVROverlayError overlayError = overlay.CreateDashboardOverlay("overlayTest", "HL3", ref overlayHandle, ref thumbnailHandle); if (overlayError != EVROverlayError.None) { throw new Exception(overlayError.ToString()); } // Set overlay parameters overlay.SetOverlayWidthInMeters(overlayHandle, 1f); // Non-dashboard overlay stuff var nmatrix = OpenTKMatrixToOpenVRMatrix(new Matrix3x4( new Vector4(1, 0, 0, 0), new Vector4(0, 1, 0, 0), new Vector4(0, 0, 1, 0) )); // TODO: Figure out how to always get the left controller index and not hardcode it to 1 which could end up being the basestations or whatever. overlay.SetOverlayTransformTrackedDeviceRelative(overlayHandle, 1, ref nmatrix); overlay.SetOverlayInputMethod(overlayHandle, VROverlayInputMethod.Mouse); var bmp = new Bitmap(@"image.png"); var textureID = GL.GenTexture(); System.Drawing.Imaging.BitmapData TextureData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb ); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, TextureData.Scan0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); bmp.UnlockBits(TextureData); var texture = new Texture_t(); texture.eType = EGraphicsAPIConvention.API_OpenGL; texture.eColorSpace = EColorSpace.Auto; texture.handle = (IntPtr)textureID; overlay.SetOverlayTexture(overlayHandle, ref texture); overlay.ShowOverlay(overlayHandle); Console.ReadLine(); }