1081a04937767ee68cb09521522fa00cfc32ab32
[libs/vr.git] / source / openvr / openvrdevice.cpp
1 #include <openvr.h>
2 #include "openvrdevice.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace VR {
8
9 unsigned OpenVRDevice::n_instances = 0;
10
11 OpenVRDevice::OpenVRDevice()
12 {
13         if(!n_instances)
14         {
15                 vr::EVRInitError init_err;
16                 vr::VR_Init(&init_err, vr::VRApplication_Scene);
17                 if(init_err!=vr::VRInitError_None)
18                         throw runtime_error("OpenVR initialization failed");
19         }
20         ++n_instances;
21
22         vr::IVRCompositor *compositor = vr::VRCompositor();
23         if(!compositor)
24                 throw runtime_error("OpenVR compositor initialization failed");
25
26         vr::VRCompositor()->SetTrackingSpace(vr::TrackingUniverseSeated);
27 }
28
29 OpenVRDevice::~OpenVRDevice()
30 {
31         if(!--n_instances)
32                 vr::VR_Shutdown();
33 }
34
35 OpenVRCamera *OpenVRDevice::create_camera(const GL::Camera &bc)
36 {
37         return new OpenVRCamera(*this, bc);
38 }
39
40 OpenVRCombiner *OpenVRDevice::create_combiner(GL::View &)
41 {
42         return new OpenVRCombiner(*this);
43 }
44
45 void OpenVRDevice::update_pose_matrices()
46 {
47         vector<vr::TrackedDevicePose_t> poses;
48         poses.resize(vr::k_unTrackedDeviceIndex_Hmd+1);
49         vr::VRCompositor()->WaitGetPoses(&poses[0], poses.size(), 0, 0);
50
51         vr::TrackedDevicePose_t &hmd_pose = poses[vr::k_unTrackedDeviceIndex_Hmd];
52         if(hmd_pose.bPoseIsValid)
53         {
54                 for(unsigned i=0; i<3; ++i)
55                         for(unsigned j=0; j<4; ++j)
56                                 hmd_matrix(i, j) = hmd_pose.mDeviceToAbsoluteTracking.m[i][j];
57         }
58 }
59
60 } // namespace VR
61 } // namespace Msp