Varjo Native SDK
Varjo_math.h
Go to the documentation of this file.
1 // Copyright 2019-2021 Varjo Technologies Oy. All rights reserved.
2 
3 #ifndef VARJO_MATH_H
4 #define VARJO_MATH_H
5 
6 #include "Varjo_export.h"
7 #include "Varjo_types.h"
8 #include <math.h>
9 #include <string.h>
10 
11 #if defined __cplusplus
12 extern "C" {
13 #endif
14 
29 VARJO_API struct varjo_Vector3D varjo_GetEulerAngles(
30  struct varjo_Matrix* matrix, varjo_EulerOrder order, varjo_RotationDirection direction, varjo_Handedness handedness);
31 
37 VARJO_API struct varjo_Vector3D varjo_GetPosition(struct varjo_Matrix* matrix);
38 
47 static void varjo_UpdateNearFarPlanes(double* projectionMatrix, varjo_ClipRange clipRange, double nearZ, double farZ)
48 {
49  if (projectionMatrix == NULL) {
50  return;
51  }
52 
53  if (clipRange == varjo_ClipRangeZeroToOne) {
54  if (farZ == HUGE_VAL) {
55  // Normal Z-range, infinite far clip plane, D3D/Vulkan convention, OpenGL with glClipControl(GL_ZERO_TO_ONE)
56  projectionMatrix[10] = -1.;
57  projectionMatrix[14] = -nearZ;
58  } else if (nearZ == HUGE_VAL) {
59  // Inverted Z-range, infinite near clip plane, D3D/Vulkan convention, OpenGL with glClipControl(GL_ZERO_TO_ONE)
60  projectionMatrix[10] = 0.;
61  projectionMatrix[14] = farZ;
62  } else {
63  // Note: Following formula handles both cases: when farZ > nearZ and when nearZ > farZ
64  projectionMatrix[10] = farZ / (nearZ - farZ);
65  projectionMatrix[14] = (nearZ * farZ) / (nearZ - farZ);
66  }
67  } else {
68  if (farZ == HUGE_VAL) {
69  // Normal Z-range, infinite far clip plane, OpenGL with default clip range
70  projectionMatrix[10] = -1.;
71  projectionMatrix[14] = -2. * nearZ;
72  } else if (nearZ == HUGE_VAL) {
73  // Inverted Z-range, infinite near clip plane, OpenGL with default clip range
74  projectionMatrix[10] = 1.;
75  projectionMatrix[14] = 2. * farZ;
76  } else {
77  // Handles both normal and inverted Z-ranges in default OpenGL convention
78  projectionMatrix[10] = (nearZ + farZ) / (nearZ - farZ);
79  projectionMatrix[14] = (2. * nearZ * farZ) / (nearZ - farZ);
80  }
81 
82  }
83 }
92 static struct varjo_Matrix varjo_GetProjectionMatrix(struct varjo_FovTangents* tangents)
93 {
94  struct varjo_Matrix varjoMatrix;
95  memset(&varjoMatrix, 0, sizeof(struct varjo_Matrix));
96 
97  if (tangents == NULL) {
98  varjoMatrix.value[0] = 1.0f;
99  varjoMatrix.value[5] = 1.0f;
100  varjoMatrix.value[10] = 1.0f;
101  varjoMatrix.value[15] = 1.0f;
102  } else {
103  const double invSizeX = 1.0 / (tangents->right - tangents->left);
104  const double invSizeY = 1.0 / (tangents->top - tangents->bottom);
105  const double offsetX = (tangents->left + tangents->right) * invSizeX;
106  const double offsetY = (tangents->bottom + tangents->top) * invSizeY;
107 
108  varjoMatrix.value[0] = 2.0 * invSizeX;
109  varjoMatrix.value[5] = 2.0 * invSizeY;
110  varjoMatrix.value[8] = offsetX;
111  varjoMatrix.value[9] = offsetY;
112  varjoMatrix.value[10] = -1.0;
113  varjoMatrix.value[11] = -1.0;
114  varjoMatrix.value[14] = -0.1;
115  }
116 
117  return varjoMatrix;
118 }
119 
120 
121 #if defined __cplusplus
122 }
123 #endif
124 
125 #endif // VARJO_MATH_H
Varjo_export.h
varjo_EulerOrder
int64_t varjo_EulerOrder
Definition: Varjo_types.h:244
varjo_GetPosition
struct varjo_Vector3D varjo_GetPosition(struct varjo_Matrix *matrix)
Gets the translation part from a transformation matrix.
varjo_ClipRange
int64_t varjo_ClipRange
Definition: Varjo_types.h:260
varjo_FovTangents
Definition: Varjo_types.h:555
varjo_GetProjectionMatrix
static struct varjo_Matrix varjo_GetProjectionMatrix(struct varjo_FovTangents *tangents)
Creates a projection matrix from given FOV tangents.
Definition: Varjo_math.h:92
varjo_UpdateNearFarPlanes
static void varjo_UpdateNearFarPlanes(double *projectionMatrix, varjo_ClipRange clipRange, double nearZ, double farZ)
Updates the near and far clip plane distances in the projection matrix.
Definition: Varjo_math.h:47
varjo_GetEulerAngles
struct varjo_Vector3D varjo_GetEulerAngles(struct varjo_Matrix *matrix, varjo_EulerOrder order, varjo_RotationDirection direction, varjo_Handedness handedness)
Extracts the euler angles in radians from the given matrix.
Varjo_types.h
varjo_Matrix
Double precision 4x4 matrix.
Definition: Varjo_types.h:330
varjo_Vector3D
Definition: Varjo_types.h:361
varjo_RotationDirection
int64_t varjo_RotationDirection
Definition: Varjo_types.h:252
varjo_ClipRangeZeroToOne
static const varjo_ClipRange varjo_ClipRangeZeroToOne
Definition: Varjo_types.h:261
varjo_Handedness
int64_t varjo_Handedness
Definition: Varjo_types.h:256