Overte C++ Documentation
Helpers.h
1 //
2 // Created by Bradley Austin Davis on 2018/11/15
3 // Copyright 2013-2018 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 <functional>
11 #include <glm/glm.hpp>
12 #include <glm/gtc/type_ptr.hpp>
13 #include <glm/gtc/matrix_transform.hpp>
14 #include <VrApi_Types.h>
15 
16 namespace ovr {
17 
18 struct Fov {
19  float leftRightUpDown[4];
20  Fov() {}
21  Fov(const ovrMatrix4f& mat) { extract(mat); }
22  void extract(const ovrMatrix4f& mat);
23  void extend(const Fov& other);
24  glm::mat4 withZ(const glm::mat4& other) const;
25  glm::mat4 withZ(float nearZ, float farZ) const;
26 };
27 
28 // Convenience method for looping over each eye with a lambda
29 static inline void for_each_eye(const std::function<void(ovrEye)>& f) {
30  f(VRAPI_EYE_LEFT);
31  f(VRAPI_EYE_RIGHT);
32 }
33 
34 static inline void for_each_hand(const std::function<void(ovrTrackedDeviceTypeId)>& f) {
35  f(VRAPI_TRACKED_DEVICE_HAND_LEFT);
36  f(VRAPI_TRACKED_DEVICE_HAND_RIGHT);
37 }
38 
39 static inline glm::mat4 toGlm(const ovrMatrix4f& om) {
40  return glm::transpose(glm::make_mat4(&om.M[0][0]));
41 }
42 
43 static inline glm::vec3 toGlm(const ovrVector3f& ov) {
44  return glm::make_vec3(&ov.x);
45 }
46 
47 static inline glm::vec2 toGlm(const ovrVector2f& ov) {
48  return glm::make_vec2(&ov.x);
49 }
50 
51 static inline glm::quat toGlm(const ovrQuatf& oq) {
52  return glm::make_quat(&oq.x);
53 }
54 
55 static inline glm::mat4 toGlm(const ovrPosef& op) {
56  glm::mat4 orientation = glm::mat4_cast(toGlm(op.Orientation));
57  glm::mat4 translation = glm::translate(glm::mat4(), toGlm(op.Position));
58  return translation * orientation;
59 }
60 
61 static inline ovrMatrix4f fromGlm(const glm::mat4& m) {
62  ovrMatrix4f result;
63  glm::mat4 transposed(glm::transpose(m));
64  memcpy(result.M, &(transposed[0][0]), sizeof(float) * 16);
65  return result;
66 }
67 
68 static inline ovrVector3f fromGlm(const glm::vec3& v) {
69  return { v.x, v.y, v.z };
70 }
71 
72 static inline ovrVector2f fromGlm(const glm::vec2& v) {
73  return { v.x, v.y };
74 }
75 
76 static inline ovrQuatf fromGlm(const glm::quat& q) {
77  return { q.x, q.y, q.z, q.w };
78 }
79 
80 static inline ovrPosef poseFromGlm(const glm::mat4& m) {
81  glm::vec3 translation = glm::vec3(m[3]) / m[3].w;
82  glm::quat orientation = glm::quat_cast(m);
83  ovrPosef result;
84  result.Orientation = fromGlm(orientation);
85  result.Position = fromGlm(translation);
86  return result;
87 }
88 
89 }
90 
91 
92 
93 
94