Overte C++ Documentation
src/OculusHelpers.h
1 //
2 // Created by Bradley Austin Davis on 2015/05/26
3 // Copyright 2015 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #pragma once
9 
10 #include <QtCore/QLoggingCategory>
11 
12 #include <ovr_capi.h>
13 #include <GLMHelpers.h>
14 #include <glm/gtc/type_ptr.hpp>
15 #include <glm/gtc/matrix_transform.hpp>
16 
17 #include <controllers/Forward.h>
18 
19 Q_DECLARE_LOGGING_CATEGORY(displayplugins)
20 Q_DECLARE_LOGGING_CATEGORY(oculusLog)
21 
22 namespace hifi {
23 
24 struct ovr {
25  static bool available();
26  static ovrSession acquireRenderSession();
27  static void releaseRenderSession(ovrSession session);
28  static void withSession(const std::function<void(ovrSession)>& f);
29  static ovrSessionStatus getStatus();
30  static ovrSessionStatus getStatus(ovrResult& result);
31  static ovrTrackingState getTrackingState(double absTime = 0.0, ovrBool latencyMarker = ovrFalse);
32  static QString getError();
33 
34  static inline bool quitRequested() { return quitRequested(getStatus()); }
35  static inline bool reorientRequested() { return reorientRequested(getStatus()); }
36  static inline bool hmdMounted() { return hmdMounted(getStatus()); }
37  static inline bool hasInputFocus() { return hasInputFocus(getStatus()); }
38 
39  static inline bool quitRequested(const ovrSessionStatus& status) { return status.ShouldQuit != ovrFalse; }
40  static inline bool displayLost(const ovrSessionStatus& status) { return status.DisplayLost != ovrFalse; }
41  static inline bool isVisible(const ovrSessionStatus& status) { return status.IsVisible != ovrFalse; }
42  static inline bool reorientRequested(const ovrSessionStatus& status) { return status.ShouldRecenter != ovrFalse; }
43  static inline bool hmdMounted(const ovrSessionStatus& status) { return status.HmdMounted != ovrFalse; }
44  static inline bool hasInputFocus(const ovrSessionStatus& status) { return status.HasInputFocus != ovrFalse; }
45 
46  // Convenience method for looping over each eye with a lambda
47  static inline void for_each_eye(const std::function<void(ovrEyeType eye)>& f) {
48  for (ovrEyeType eye = ovrEye_Left; eye < ovrEye_Count; eye = static_cast<ovrEyeType>(eye + 1)) {
49  f(eye);
50  }
51  }
52 
53  static inline void for_each_hand(const std::function<void(ovrHandType eye)>& f) {
54  for (ovrHandType hand = ovrHand_Left; hand < ovrHand_Count; hand = static_cast<ovrHandType>(hand + 1)) {
55  f(hand);
56  }
57  }
58 
59  static inline glm::mat4 toGlm(const ovrMatrix4f& om) {
60  return glm::transpose(glm::make_mat4(&om.M[0][0]));
61  }
62 
63  static inline glm::mat4 toGlm(const ovrFovPort& fovport, float nearPlane = 0.01f, float farPlane = 10000.0f) {
64  return toGlm(ovrMatrix4f_Projection(fovport, nearPlane, farPlane, true));
65  }
66 
67  static inline glm::vec3 toGlm(const ovrVector3f& ov) {
68  return glm::make_vec3(&ov.x);
69  }
70 
71  static inline glm::vec2 toGlm(const ovrVector2f& ov) {
72  return glm::make_vec2(&ov.x);
73  }
74 
75  static inline glm::uvec2 toGlm(const ovrSizei& ov) {
76  return glm::uvec2(ov.w, ov.h);
77  }
78 
79  static inline glm::quat toGlm(const ovrQuatf& oq) {
80  return glm::make_quat(&oq.x);
81  }
82 
83  static inline glm::mat4 toGlm(const ovrPosef& op) {
84  glm::mat4 orientation = glm::mat4_cast(toGlm(op.Orientation));
85  glm::mat4 translation = glm::translate(glm::mat4(), toGlm(op.Position));
86  return translation * orientation;
87  }
88 
89  static inline ovrMatrix4f fromGlm(const glm::mat4& m) {
90  ovrMatrix4f result;
91  glm::mat4 transposed(glm::transpose(m));
92  memcpy(result.M, &(transposed[0][0]), sizeof(float) * 16);
93  return result;
94  }
95 
96  static inline ovrVector3f fromGlm(const glm::vec3& v) {
97  return { v.x, v.y, v.z };
98  }
99 
100  static inline ovrVector2f fromGlm(const glm::vec2& v) {
101  return { v.x, v.y };
102  }
103 
104  static inline ovrSizei fromGlm(const glm::uvec2& v) {
105  return { (int)v.x, (int)v.y };
106  }
107 
108  static inline ovrQuatf fromGlm(const glm::quat& q) {
109  return { q.x, q.y, q.z, q.w };
110  }
111 
112  static inline ovrPosef poseFromGlm(const glm::mat4& m) {
113  glm::vec3 translation = glm::vec3(m[3]) / m[3].w;
114  glm::quat orientation = glm::quat_cast(m);
115  ovrPosef result;
116  result.Orientation = fromGlm(orientation);
117  result.Position = fromGlm(translation);
118  return result;
119  }
120 
121  static controller::Pose toControllerPose(ovrHandType hand, const ovrPoseStatef& handPose);
122  static controller::Pose toControllerPose(ovrHandType hand, const ovrPoseStatef& handPose, const ovrPoseStatef& lastHandPose);
123 
124 };
125 
126 } // namespace hifi