Ejemplo n.º 1
0
        void EndCurrentSession()
        {
            try
            {
                if (SessionCreated)
                {
                    OculusWrap.Destroy(SessionPtr);
                    SessionCreated = false;
                }
                if (SessionInitialized)
                {
                    OculusWrap.Shutdown();
                    SessionInitialized = false;
                }
            }
            catch (Exception e)
            {
                LastExceptionMessage = e.Message;
                if (!StopFlag)
                {
                    OculusStatus = OculusConnectionStatus.UnexpectedError;
                }
            }

            if (!StopFlag)
            {
                OculusStatus = OculusConnectionStatus.Resurrecting;
            }

            SessionPtr = IntPtr.Zero;
        }
Ejemplo n.º 2
0
        int GetAudioDeviceOutId()
        {
            if (OculusStatus != OculusConnectionStatus.AllOk)
            {
                return(-1); // windows default
            }
            uint   id = 0;
            Result r  = OculusWrap.GetAudioDeviceOutWaveId(ref id);

            return((int)id);
        }
Ejemplo n.º 3
0
        public bool OculusHMDConnected()
        {
            try
            {
                DetectResult detection = OculusWrap.Detect(1000);
                if (detection.IsOculusHMDConnected)
                {
                    return(true);
                }
            }
            catch (Exception)
            {
                // intentionally ignore and just assume not connected
            }

            return(false);
        }
Ejemplo n.º 4
0
        public override bool GetHmdYaw(ref double yaw)
        {
            if (StopRequested)
            {
                return(false);
            }

            if (OculusStatus != OculusConnectionStatus.AllOk)
            {
                return(false);
            }

            TrackingState ts = OculusWrap.GetTrackingState(SessionPtr, 0, false);

            // I suppose for Oculus there is no need to check if the received orientation is valid... (compare to OpenVR)
            yaw = GetYawFromOrientation(ts.HeadPose.ThePose.Orientation);
            return(true);
        }
Ejemplo n.º 5
0
        void KeepAlive()
        {
            KeepAliveCounter++;

            if (StopRequested)
            {
                Stop();
                return;
            }

            if (RequireHome)
            {
                if (KeepAliveCounter % OculusHomeDivider == 0) // to save CPU, do not check every loop
                {
                    if (System.Diagnostics.Process.GetProcessesByName(Config.OculusHomeProcessName).Any() == false)
                    {
                        if (OculusStatus != OculusConnectionStatus.WaitingHome)
                        {
                            EndCurrentSession();
                            HomePresent  = false;
                            OculusStatus = OculusConnectionStatus.WaitingHome;
                        }
                    }
                    else
                    {
                        HomePresent = true;
                    }
                }
                if (!HomePresent)
                {
                    return;
                }
            }

            if (OculusStatus == OculusConnectionStatus.AllOk)
            {
                // poll quit message on every lap
                LastOculusResult = OculusWrap.GetSessionStatus(SessionPtr, ref SesStatus);
                if (!OculusWrap.OVR_SUCCESS(LastOculusResult) || (SesStatus.ShouldQuit || SesStatus.DisplayLost || !SesStatus.HmdPresent))
                {
                    //StopRequested = true;
                    OculusStatus = OculusConnectionStatus.OculusVRQuit; // to immediately prevent native methods from being called
                    EndCurrentSession();
                    OculusStatus = OculusConnectionStatus.OculusVRQuit; // again to get correct status (changed in EndCurrentSession())
                    // a good sleep before starting to poll OculusVR again
                    Thread.Sleep(15000);
                }
                else if (OculusWrap.OVR_SUCCESS(LastOculusResult))
                {
                    bool mounted = SesStatus.HmdMounted;
                    if (mounted == HMDMounted_previousReading)
                    {
                        HMDMountCounter++;
                    }
                    else
                    {
                        HMDMountCounter = 0;
                    }

                    HMDMounted_previousReading = mounted;

                    if (HMDMountCounter > HMDMountDivider) // some buffer to filter out false flags (and conveniently some delay for notifications)
                    {
                        if (HMDMounted_buffered != mounted)
                        {
                            HMDMounted_buffered = mounted;

                            if (mounted)
                            {
                                Worker.ReportProgress(1, null);
                            }
                            else
                            {
                                Worker.ReportProgress(2, null);
                            }
                        }
                        HMDMountCounter = 0;
                    }
                }
                return;
            }


            // This point is reached only when connection not yet created
            // ******** INITIALIZATION & CREATION ***********

            if (KeepAliveCounter % InitializationDivider != 0) // do not try initialization every loop
            {
                return;
            }

            if (OculusStatus == OculusConnectionStatus.Initialized)
            {
                LastOculusResult = OculusWrap.Create(ref SessionPtr, ref pLuid);
                if (OculusWrap.OVR_SUCCESS(LastOculusResult))
                {
                    OculusStatus   = OculusConnectionStatus.AllOk;
                    SessionCreated = true;
                }
            }
            else
            {
                DetectResult detection = OculusWrap.Detect(1000);
                if (detection.IsOculusHMDConnected)
                {
                    uint libMinorVersion = 0;
                    LastOculusResult = Result.LibVersion;
                    // To initialize directly from LibOVRRT-dll, the requested version is required.
                    // --> Try initialization until reaching the first valid version. (Future proofing.)
                    // (Oculus library defines a lowest requestable version, but it can change in the future.)
                    while (!OculusWrap.OVR_SUCCESS(LastOculusResult) && libMinorVersion < 100)
                    {
                        InitParams par = new InitParams(InitFlags.RequestVersion | InitFlags.Invisible, libMinorVersion);
                        LastOculusResult = OculusWrap.Initialize(par);
                        libMinorVersion++;
                    }
                    if (OculusWrap.OVR_SUCCESS(LastOculusResult))
                    {
                        OculusStatus       = OculusConnectionStatus.Initialized;
                        SessionInitialized = true;
                    }
                    else
                    {
                        OculusStatus = OculusConnectionStatus.InitFail;
                    }
                }
                else if (detection.IsOculusServiceRunning)
                {
                    OculusStatus = OculusConnectionStatus.NoHMD;
                }
                else
                {
                    OculusStatus = OculusConnectionStatus.NoService;
                }
            }
        }