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
56  projectionMatrix[10] = -1.;
57  projectionMatrix[14] = -nearZ;
58  } else if (nearZ == HUGE_VAL) {
59  // Inverted Z-range, infinite far clip plane, D3D/Vulkan convention
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 (nearZ == HUGE_VAL || farZ == HUGE_VAL) {
69  // Handles both normal and reverted Z-range in OpenGL convention in
70  // the case of far clip plane located at +infinity
71  projectionMatrix[10] = -1.;
72  projectionMatrix[14] = nearZ <= farZ ? nearZ : farZ;
73  } else {
74  // Handles both normal and reverted Z-ranges in OpenGL convention
75  projectionMatrix[10] = (nearZ + farZ) / (nearZ - farZ);
76  projectionMatrix[14] = (2 * nearZ * farZ) / (nearZ - farZ);
77  }
78  }
79 }
88 static struct varjo_Matrix varjo_GetProjectionMatrix(struct varjo_FovTangents* tangents)
89 {
90  struct varjo_Matrix varjoMatrix;
91  memset(&varjoMatrix, 0, sizeof(struct varjo_Matrix));
92 
93  if (tangents == NULL) {
94  varjoMatrix.value[0] = 1.0f;
95  varjoMatrix.value[5] = 1.0f;
96  varjoMatrix.value[10] = 1.0f;
97  varjoMatrix.value[15] = 1.0f;
98  } else {
99  const double invSizeX = 1.0 / (tangents->right - tangents->left);
100  const double invSizeY = 1.0 / (tangents->top - tangents->bottom);
101  const double offsetX = (tangents->left + tangents->right) * invSizeX;
102  const double offsetY = (tangents->bottom + tangents->top) * invSizeY;
103 
104  varjoMatrix.value[0] = 2.0 * invSizeX;
105  varjoMatrix.value[5] = 2.0 * invSizeY;
106  varjoMatrix.value[8] = offsetX;
107  varjoMatrix.value[9] = offsetY;
108  varjoMatrix.value[10] = -1.0;
109  varjoMatrix.value[11] = -1.0;
110  varjoMatrix.value[14] = -0.1;
111  }
112 
113  return varjoMatrix;
114 }
115 
116 
117 #if defined __cplusplus
118 }
119 #endif
120 
121 #endif // VARJO_MATH_H
Varjo_export.h
varjo_EulerOrder
int64_t varjo_EulerOrder
Definition: Varjo_types.h:231
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:247
varjo_FovTangents
Definition: Varjo_types.h:483
varjo_GetProjectionMatrix
static struct varjo_Matrix varjo_GetProjectionMatrix(struct varjo_FovTangents *tangents)
Creates a projection matrix from given FOV tangents.
Definition: Varjo_math.h:88
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:293
varjo_Vector3D
Definition: Varjo_types.h:324
varjo_RotationDirection
int64_t varjo_RotationDirection
Definition: Varjo_types.h:239
varjo_ClipRangeZeroToOne
static const varjo_ClipRange varjo_ClipRangeZeroToOne
Definition: Varjo_types.h:248
varjo_Handedness
int64_t varjo_Handedness
Definition: Varjo_types.h:243