Overte C++ Documentation
egacy/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 <OVR_CAPI_GL.h>
11 #include <GLMHelpers.h>
12 #include <glm/gtc/type_ptr.hpp>
13 #include <glm/gtc/matrix_transform.hpp>
14 
15 // Convenience method for looping over each eye with a lambda
16 template <typename Function>
17 inline void ovr_for_each_eye(Function function) {
18  for (ovrEyeType eye = ovrEyeType::ovrEye_Left;
19  eye < ovrEyeType::ovrEye_Count;
20  eye = static_cast<ovrEyeType>(eye + 1)) {
21  function(eye);
22  }
23 }
24 
25 inline glm::mat4 toGlm(const ovrMatrix4f & om) {
26  return glm::transpose(glm::make_mat4(&om.M[0][0]));
27 }
28 
29 inline glm::mat4 toGlm(const ovrFovPort & fovport, float nearPlane = 0.01f, float farPlane = 10000.0f) {
30  return toGlm(ovrMatrix4f_Projection(fovport, nearPlane, farPlane, true));
31 }
32 
33 inline glm::vec3 toGlm(const ovrVector3f & ov) {
34  return glm::make_vec3(&ov.x);
35 }
36 
37 inline glm::vec2 toGlm(const ovrVector2f & ov) {
38  return glm::make_vec2(&ov.x);
39 }
40 
41 inline glm::uvec2 toGlm(const ovrSizei & ov) {
42  return glm::uvec2(ov.w, ov.h);
43 }
44 
45 inline glm::quat toGlm(const ovrQuatf & oq) {
46  return glm::make_quat(&oq.x);
47 }
48 
49 inline glm::mat4 toGlm(const ovrPosef & op) {
50  glm::mat4 orientation = glm::mat4_cast(toGlm(op.Orientation));
51  glm::mat4 translation = glm::translate(glm::mat4(), toGlm(op.Position));
52  return translation * orientation;
53 }
54 
55 inline ovrMatrix4f ovrFromGlm(const glm::mat4 & m) {
56  ovrMatrix4f result;
57  glm::mat4 transposed(glm::transpose(m));
58  memcpy(result.M, &(transposed[0][0]), sizeof(float) * 16);
59  return result;
60 }
61 
62 inline ovrVector3f ovrFromGlm(const glm::vec3 & v) {
63  return{ v.x, v.y, v.z };
64 }
65 
66 inline ovrVector2f ovrFromGlm(const glm::vec2 & v) {
67  return{ v.x, v.y };
68 }
69 
70 inline ovrSizei ovrFromGlm(const glm::uvec2 & v) {
71  return{ (int)v.x, (int)v.y };
72 }
73 
74 inline ovrQuatf ovrFromGlm(const glm::quat & q) {
75  return{ q.x, q.y, q.z, q.w };
76 }
77 
78 inline ovrPosef ovrPoseFromGlm(const glm::mat4 & m) {
79  glm::vec3 translation = glm::vec3(m[3]) / m[3].w;
80  glm::quat orientation = glm::quat_cast(m);
81  ovrPosef result;
82  result.Orientation = ovrFromGlm(orientation);
83  result.Position = ovrFromGlm(translation);
84  return result;
85 }