10 #include <OVR_CAPI_GL.h>
11 #include <GLMHelpers.h>
12 #include <glm/gtc/type_ptr.hpp>
13 #include <glm/gtc/matrix_transform.hpp>
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)) {
25 inline glm::mat4 toGlm(
const ovrMatrix4f & om) {
26 return glm::transpose(glm::make_mat4(&om.M[0][0]));
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));
33 inline glm::vec3 toGlm(
const ovrVector3f & ov) {
34 return glm::make_vec3(&ov.x);
37 inline glm::vec2 toGlm(
const ovrVector2f & ov) {
38 return glm::make_vec2(&ov.x);
41 inline glm::uvec2 toGlm(
const ovrSizei & ov) {
42 return glm::uvec2(ov.w, ov.h);
45 inline glm::quat toGlm(
const ovrQuatf & oq) {
46 return glm::make_quat(&oq.x);
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;
55 inline ovrMatrix4f ovrFromGlm(
const glm::mat4 & m) {
57 glm::mat4 transposed(glm::transpose(m));
58 memcpy(result.M, &(transposed[0][0]),
sizeof(
float) * 16);
62 inline ovrVector3f ovrFromGlm(
const glm::vec3 & v) {
63 return{ v.x, v.y, v.z };
66 inline ovrVector2f ovrFromGlm(
const glm::vec2 & v) {
70 inline ovrSizei ovrFromGlm(
const glm::uvec2 & v) {
71 return{ (int)v.x, (
int)v.y };
74 inline ovrQuatf ovrFromGlm(
const glm::quat & q) {
75 return{ q.x, q.y, q.z, q.w };
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);
82 result.Orientation = ovrFromGlm(orientation);
83 result.Position = ovrFromGlm(translation);