Exemple #1
0
        public void UpdatePoses()
        {
            CVRCompositor compositor = OpenVR.Compositor;

            if (compositor != null)
            {
                compositor.GetLastPoses(this.poses, this.gamePoses);
                SteamVR_Events.NewPoses.Send(this.poses);
                SteamVR_Events.NewPosesApplied.Send();
            }
        }
Exemple #2
0
 public void Clear()
 {
     this.m_pVRSystem          = null;
     this.m_pVRChaperone       = null;
     this.m_pVRChaperoneSetup  = null;
     this.m_pVRCompositor      = null;
     this.m_pVROverlay         = null;
     this.m_pVRRenderModels    = null;
     this.m_pVRExtendedDisplay = null;
     this.m_pVRSettings        = null;
     this.m_pVRApplications    = null;
 }
Exemple #3
0
 public CVRCompositor VRCompositor()
 {
     this.CheckClear();
     if (this.m_pVRCompositor == null)
     {
         EVRInitError eVRInitError     = EVRInitError.None;
         IntPtr       genericInterface = OpenVRInterop.GetGenericInterface("FnTable:IVRCompositor_013", ref eVRInitError);
         if (genericInterface != IntPtr.Zero && eVRInitError == EVRInitError.None)
         {
             this.m_pVRCompositor = new CVRCompositor(genericInterface);
         }
     }
     return(this.m_pVRCompositor);
 }
Exemple #4
0
        private IEnumerator RenderLoop()
        {
            while (Application.isPlaying)
            {
                yield return(this.waitForEndOfFrame);

                if (!SteamVR_Render.pauseRendering)
                {
                    CVRCompositor compositor = OpenVR.Compositor;
                    if (compositor != null)
                    {
                        if (!compositor.CanRenderScene() || cameras.Length < 1)
                        {
                            continue;
                        }
                        compositor.SetTrackingSpace(SteamVR.settings.trackingSpace);
                        SteamVR_Utils.QueueEventOnRenderThread(201510020);
                        SteamVR.Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - Begin");
                        SteamVR_Camera.GetSceneTexture(this.cameras[0].GetComponent <Camera>()).GetNativeTexturePtr();
                        SteamVR.Unity.EventWriteString("[UnityMain] GetNativeTexturePtr - End");
                        compositor.GetLastPoses(this.poses, this.gamePoses);
                        SteamVR_Events.NewPoses.Send(this.poses);
                        SteamVR_Events.NewPosesApplied.Send();
                    }
                    SteamVR_Overlay instance = SteamVR_Overlay.instance;
                    if (instance != null)
                    {
                        instance.UpdateOverlay();
                    }
                    if (this.CheckExternalCamera())
                    {
                        this.RenderExternalCamera();
                    }
                    SteamVR instance2 = SteamVR.instance;
                    this.RenderEye(instance2, EVREye.Eye_Left);
                    this.RenderEye(instance2, EVREye.Eye_Right);
                    foreach (SteamVR_Camera steamVR_Camera in this.cameras)
                    {
                        steamVR_Camera.transform.localPosition = Vector3.zero;
                        steamVR_Camera.transform.localRotation = Quaternion.identity;
                    }
                    if (this.cameraMask != null)
                    {
                        this.cameraMask.Clear();
                    }
                }
            }
            yield break;
        }
Exemple #5
0
    private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor, System.IntPtr pOverlay)
    {
        hmd = new CVRSystem(pHmd);
        Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber);

        compositor = new CVRCompositor(pCompositor);
        overlay = new CVROverlay(pOverlay);

        var capacity = compositor.GetLastError(null, 0);
        if (capacity > 1)
        {
            var result = new System.Text.StringBuilder((int)capacity);
            compositor.GetLastError(result, capacity);
            Debug.Log("Compositor - " + result);
        }

        // Setup render values
        uint w = 0, h = 0;
        hmd.GetRecommendedRenderTargetSize(ref w, ref h);
        sceneWidth = (float)w;
        sceneHeight = (float)h;

        float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);

        float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);

        tanHalfFov = new Vector2(
            Mathf.Max(-l_left, l_right, -r_left, r_right),
            Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));

        textureBounds = new VRTextureBounds_t[2];

        textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
        textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
        textureBounds[0].vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.y;
        textureBounds[0].vMax = 0.5f - 0.5f * l_top / tanHalfFov.y;

        textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
        textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
        textureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.y;
        textureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov.y;

        Unity.SetSubmitParams(textureBounds[0], textureBounds[1], VRSubmitFlags_t.Submit_Default);

        // Grow the recommended size to account for the overlapping fov
        sceneWidth = sceneWidth / Mathf.Max(textureBounds[0].uMax - textureBounds[0].uMin, textureBounds[1].uMax - textureBounds[1].uMin);
        sceneHeight = sceneHeight / Mathf.Max(textureBounds[0].vMax - textureBounds[0].vMin, textureBounds[1].vMax - textureBounds[1].vMin);

        aspect = tanHalfFov.x / tanHalfFov.y;
        fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;

        eyes = new SteamVR_Utils.RigidTransform[] {
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)),
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) };

        if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
            graphicsAPI = GraphicsAPIConvention.API_OpenGL;
        else
            graphicsAPI = GraphicsAPIConvention.API_DirectX;

        SteamVR_Utils.Event.Listen("initializing", OnInitializing);
        SteamVR_Utils.Event.Listen("calibrating", OnCalibrating);
        SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange);
        SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
        SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    }
		public CVRCompositor VRCompositor()
		{
			CheckClear();
			if (m_pVRCompositor == null)
			{
				var eError = EVRInitError.None;
				var pInterface = OpenVRInterop.GetGenericInterface(FnTable_Prefix+IVRCompositor_Version, ref eError);
				if (pInterface != IntPtr.Zero && eError == EVRInitError.None)
					m_pVRCompositor = new CVRCompositor(pInterface);
			}
			return m_pVRCompositor;
		}
        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();
            }
        }
Exemple #8
0
 public void Clear()
 {
     m_pVRSystem = null;
     m_pVRChaperone = null;
     m_pVRChaperoneSetup = null;
     m_pVRCompositor = null;
     m_pVROverlay = null;
     m_pVRRenderModels = null;
     m_pVRExtendedDisplay = null;
     m_pVRSettings = null;
     m_pVRApplications = null;
 }
Exemple #9
0
    private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor)
    {
        hmd = new CVRSystem(pHmd);
        Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber);

        compositor = new CVRCompositor(pCompositor);

        var device = new UnityGraphicsDevice();
        GetUnityGraphicsDevice(ref device);
        switch (device.type)
        {
            case GfxDeviceRenderer.kGfxRendererD3D11:
                compositor.SetGraphicsDevice(Compositor_DeviceType.D3D11, device.ptr);
                break;
            case GfxDeviceRenderer.kGfxRendererOpenGL:
                compositor.SetGraphicsDevice(Compositor_DeviceType.OpenGL, device.ptr);
                break;
            default:
                throw new System.Exception("Unsupported device type.");
        }

        var capacity = compositor.GetLastError(null, 0);
        if (capacity > 1)
        {
            var result = new System.Text.StringBuilder((int)capacity);
            compositor.GetLastError(result, capacity);
            Debug.Log("Compositor - " + result);
        }

        // Register for a callback if our graphics device goes away, so we can properly clean up.
        var resetDelegate = new UnityResetDelegate(SteamVR.SafeDispose);
        callbackHandle = GCHandle.Alloc(resetDelegate);
        SetUnityResetCallback(Marshal.GetFunctionPointerForDelegate(resetDelegate));

        // Hook up the render thread present event just in case we wind up needing to use this.
        var error = HmdError.None;
        SetUnityRenderCallback(OpenVR.GetGenericInterface(IVRHmdDistortPresent_Version, ref error));

        // Setup render values
        uint w = 0, h = 0;
        hmd.GetRecommendedRenderTargetSize(ref w, ref h);
        sceneWidth = (float)w;
        sceneHeight = (float)h;

        float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);

        float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);

        tanHalfFov = new Vector2(
            Mathf.Max(-l_left, l_right, -r_left, r_right),
            Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));

        textureBounds = new Compositor_TextureBounds[2];

        textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
        textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
        textureBounds[0].vMin = 0.5f + 0.5f * l_bottom / tanHalfFov.y;
        textureBounds[0].vMax = 0.5f + 0.5f * l_top / tanHalfFov.y;

        textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
        textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
        textureBounds[1].vMin = 0.5f + 0.5f * r_bottom / tanHalfFov.y;
        textureBounds[1].vMax = 0.5f + 0.5f * r_top / tanHalfFov.y;

        aspect = tanHalfFov.x / tanHalfFov.y;
        fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;

        eyes = new SteamVR_Utils.RigidTransform[] {
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)),
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) };

        SteamVR_Utils.Event.Listen("initializing", OnInitializing);
        SteamVR_Utils.Event.Listen("calibrating", OnCalibrating);
        SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange);
        SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
        SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    }
Exemple #10
0
 public CVRCompositor VRCompositor()
 {
     this.CheckClear();
     if (this.m_pVRCompositor == null)
     {
         EVRInitError eVRInitError = EVRInitError.None;
         IntPtr genericInterface = OpenVRInterop.GetGenericInterface("FnTable:IVRCompositor_013", ref eVRInitError);
         if (genericInterface != IntPtr.Zero && eVRInitError == EVRInitError.None)
         {
             this.m_pVRCompositor = new CVRCompositor(genericInterface);
         }
     }
     return this.m_pVRCompositor;
 }