Example #1
0
        public MyOpenVR()
        {
            if (true)
            {
                m_viewHMD     = Matrix.Identity;
                m_headsetPosD = MatrixD.Identity;
                m_c1pos       = Matrix.Identity;
                m_c2pos       = Matrix.Identity;

                //IntPtr Handle = MyRender11.LoadLibrary(@"D:\KeenSWH.VR\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll");
                //IntPtr Handle = LoadLibrary(@"C:\Program Files (x86)\Steam\SteamApps\common\SteamVR\bin\win64\openvr_api.dll");
                IntPtr Handle = LoadLibrary(@"openvr_api.dll");
                //IntPtr Handle = MyRender11.LoadLibrary(@"c:\Program Files (x86)\Steam\bin\openvr_api.dll");//err code 193 -  not a valid Win32 application
                //Assembly.LoadFile(@"D:\KeenSWH\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll");
                if (Handle == IntPtr.Zero)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode));
                }

                EVRInitError error = EVRInitError.None;
                IntPtr       ptr   = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene);
                if (error == EVRInitError.None)
                {
                    m_vrSystem = new CVRSystem(ptr);

                    uint sizeX = 0, sizeY = 0;
                    m_vrSystem.GetRecommendedRenderTargetSize(ref sizeX, ref sizeY);

                    m_vrSystem.SetDisplayVisibility(true);

                    ETrackedPropertyError pError = 0;
                    m_ipd_2 = 0.5f * m_vrSystem.GetFloatTrackedDeviceProperty(0, ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref pError);
                    SetIPD(m_ipd_2 * 2);

                    IntPtr pointer = OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error);
                    if (error == EVRInitError.None)
                    {
                        m_vrCompositor = new CVRCompositor(pointer);
                        m_vrCompositor.CompositorBringToFront();
                        m_vrCompositor.ShowMirrorWindow();
                        m_openVR = this;
                    }
                    else
                    {
                        var errString = OpenVR.GetStringForHmdError(error);
                        Log.WriteLineAndConsole(errString);
                        Debug.Fail("No compositor interface");
                        throw new Exception(errString);
                    }
                }
                else
                {
                    var errString = OpenVR.GetStringForHmdError(error);
                    Log.WriteLineAndConsole(errString);
                    //Debug.Fail("OpenVR init failed");
                    throw new Exception(errString);
                }
                InitOverlays();
            }
        }