Overte C++ Documentation
OpenVrHelpers.h
1 //
2 // Created by Bradley Austin Davis on 2015/06/12
3 // Copyright 2015 High Fidelity, Inc.
4 // Copyright 2020 Vircadia contributors.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 //
9 #pragma once
10 
11 #include <openvr.h>
12 #include <GLMHelpers.h>
13 #include <glm/gtc/type_ptr.hpp>
14 #include <glm/gtc/matrix_transform.hpp>
15 
16 #include <controllers/Forward.h>
17 #include <plugins/Forward.h>
18 #include <string>
19 
20 bool oculusViaOpenVR(); // is the user using Oculus via OpenVR
21 bool openVrSupported();
22 
23 vr::IVRSystem* acquireOpenVrSystem();
24 void releaseOpenVrSystem();
25 void handleOpenVrEvents();
26 bool openVrQuitRequested();
27 bool isHeadInHeadset();
28 void enableOpenVrKeyboard(PluginContainer* container);
29 void disableOpenVrKeyboard();
30 bool isOpenVrKeyboardShown();
31 QString getVrSettingString(const char* section, const char* setting);
32 std::string getOpenVrDeviceName();
33 
34 
35 template<typename F>
36 void openvr_for_each_eye(F f) {
37  f(vr::Hmd_Eye::Eye_Left);
38  f(vr::Hmd_Eye::Eye_Right);
39 }
40 
41 inline mat4 toGlm(const vr::HmdMatrix44_t& m) {
42  return glm::transpose(glm::make_mat4(&m.m[0][0]));
43 }
44 
45 inline vec3 toGlm(const vr::HmdVector3_t& v) {
46  return vec3(v.v[0], v.v[1], v.v[2]);
47 }
48 
49 inline mat4 toGlm(const vr::HmdMatrix34_t& m) {
50  mat4 result = mat4(
51  m.m[0][0], m.m[1][0], m.m[2][0], 0.0,
52  m.m[0][1], m.m[1][1], m.m[2][1], 0.0,
53  m.m[0][2], m.m[1][2], m.m[2][2], 0.0,
54  m.m[0][3], m.m[1][3], m.m[2][3], 1.0f);
55  return result;
56 }
57 
58 inline vr::HmdMatrix34_t toOpenVr(const mat4& m) {
59  vr::HmdMatrix34_t result;
60  for (uint8_t i = 0; i < 3; ++i) {
61  for (uint8_t j = 0; j < 4; ++j) {
62  result.m[i][j] = m[j][i];
63  }
64  }
65  return result;
66 }
67 
68 struct PoseData {
69  uint32_t frameIndex{ 0 };
70  vr::TrackedDevicePose_t vrPoses[vr::k_unMaxTrackedDeviceCount];
71  mat4 poses[vr::k_unMaxTrackedDeviceCount];
72  vec3 linearVelocities[vr::k_unMaxTrackedDeviceCount];
73  vec3 angularVelocities[vr::k_unMaxTrackedDeviceCount];
74 
75  PoseData() {
76  memset(vrPoses, 0, sizeof(vr::TrackedDevicePose_t) * vr::k_unMaxTrackedDeviceCount);
77  }
78 
79  void update(const glm::mat4& resetMat) {
80  for (uint32_t i = 0; i < vr::k_unMaxTrackedDeviceCount; i++) {
81  if (!vrPoses[i].bPoseIsValid) {
82  continue;
83  }
84  poses[i] = resetMat * toGlm(vrPoses[i].mDeviceToAbsoluteTracking);
85  linearVelocities[i] = transformVectorFast(resetMat, toGlm(vrPoses[i].vVelocity));
86  angularVelocities[i] = transformVectorFast(resetMat, toGlm(vrPoses[i].vAngularVelocity));
87  }
88  }
89 
90  void resetToInvalid() {
91  for (uint32_t i = 0; i < vr::k_unMaxTrackedDeviceCount; i++) {
92  vrPoses[i].bPoseIsValid = false;
93  }
94  }
95 };
96 
97 // FIXME remove once OpenVR header is updated
98 #define VRCompositor_ReprojectionAsync 0x04
99 
100 controller::Pose openVrControllerPoseToHandPose(bool isLeftHand, const mat4& mat, const vec3& linearVelocity, const vec3& angularVelocity);