22#include "openxr/openxr.h"
41XrFovInRadians(
float &fovY,
43 GraphicsAPI graphicsApi,
46 const float tanAngleLeft = tanf(fov.angleLeft);
47 const float tanAngleRight = tanf(fov.angleRight);
49 const float tanAngleDown = tanf(fov.angleDown);
50 const float tanAngleUp = tanf(fov.angleUp);
52 fovX = atan(tanAngleRight - tanAngleLeft);
57 fovY = graphicsApi == GRAPHICS_VULKAN ? (tanAngleDown - tanAngleUp) : (tanAngleUp - tanAngleDown);
68inline static void XrMatrix4x4f_CreateProjection(
XrMatrix4x4f* result, GraphicsAPI graphicsApi,
const float tanAngleLeft,
69 const float tanAngleRight,
const float tanAngleUp,
float const tanAngleDown,
70 const float nearZ,
const float farZ) {
71 const float tanAngleWidth = tanAngleRight - tanAngleLeft;
75 const float tanAngleHeight = graphicsApi == GRAPHICS_VULKAN ? (tanAngleDown - tanAngleUp) : (tanAngleUp - tanAngleDown);
79 const float offsetZ = (graphicsApi == GRAPHICS_OPENGL || graphicsApi == GRAPHICS_OPENGL_ES) ? nearZ : 0;
83 result->m[0] = 2.0f / tanAngleWidth;
85 result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
89 result->m[5] = 2.0f / tanAngleHeight;
90 result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
95 result->m[10] = -1.0f;
96 result->m[14] = -(nearZ + offsetZ);
100 result->m[11] = -1.0f;
101 result->m[15] = 0.0f;
105 result->m[0] = 2.0f / tanAngleWidth;
107 result->m[8] = (tanAngleRight + tanAngleLeft) / tanAngleWidth;
108 result->m[12] = 0.0f;
111 result->m[5] = 2.0f / tanAngleHeight;
112 result->m[9] = (tanAngleUp + tanAngleDown) / tanAngleHeight;
113 result->m[13] = 0.0f;
117 result->m[10] = -(farZ + offsetZ) / (farZ - nearZ);
118 result->m[14] = -(farZ * (nearZ + offsetZ)) / (farZ - nearZ);
122 result->m[11] = -1.0f;
123 result->m[15] = 0.0f;
128inline static void XrMatrix4x4f_CreateProjectionFov(
XrMatrix4x4f* result, GraphicsAPI graphicsApi,
const XrFovf fov,
129 const float nearZ,
const float farZ) {
131 const float tanLeft = tanf(fov.angleLeft);
132 const float tanRight = tanf(fov.angleRight);
134 const float tanDown = tanf(fov.angleDown);
135 const float tanUp = tanf(fov.angleUp);
137 XrMatrix4x4f_CreateProjection(result, graphicsApi, tanLeft, tanRight, tanUp, tanDown, nearZ, farZ);
141XrMatrix4x4f_CreateFromQuaternion(
XrMatrix4x4f *result,
const XrQuaternionf *quat)
143 const float x2 = quat->x + quat->x;
144 const float y2 = quat->y + quat->y;
145 const float z2 = quat->z + quat->z;
147 const float xx2 = quat->x * x2;
148 const float yy2 = quat->y * y2;
149 const float zz2 = quat->z * z2;
151 const float yz2 = quat->y * z2;
152 const float wx2 = quat->w * x2;
153 const float xy2 = quat->x * y2;
154 const float wz2 = quat->w * z2;
155 const float xz2 = quat->x * z2;
156 const float wy2 = quat->w * y2;
158 result->m[0] = 1.0f - yy2 - zz2;
159 result->m[1] = xy2 + wz2;
160 result->m[2] = xz2 - wy2;
163 result->m[4] = xy2 - wz2;
164 result->m[5] = 1.0f - xx2 - zz2;
165 result->m[6] = yz2 + wx2;
168 result->m[8] = xz2 + wy2;
169 result->m[9] = yz2 - wx2;
170 result->m[10] = 1.0f - xx2 - yy2;
171 result->m[11] = 0.0f;
173 result->m[12] = 0.0f;
174 result->m[13] = 0.0f;
175 result->m[14] = 0.0f;
176 result->m[15] = 1.0f;
180XrMatrix4x4f_CreateTranslation(
XrMatrix4x4f *result,
const float x,
const float y,
const float z)
192 result->m[10] = 1.0f;
193 result->m[11] = 0.0f;
197 result->m[15] = 1.0f;
203 result->m[0] = a->m[0] * b->m[0] + a->m[4] * b->m[1] + a->m[8] * b->m[2] + a->m[12] * b->m[3];
204 result->m[1] = a->m[1] * b->m[0] + a->m[5] * b->m[1] + a->m[9] * b->m[2] + a->m[13] * b->m[3];
205 result->m[2] = a->m[2] * b->m[0] + a->m[6] * b->m[1] + a->m[10] * b->m[2] + a->m[14] * b->m[3];
206 result->m[3] = a->m[3] * b->m[0] + a->m[7] * b->m[1] + a->m[11] * b->m[2] + a->m[15] * b->m[3];
208 result->m[4] = a->m[0] * b->m[4] + a->m[4] * b->m[5] + a->m[8] * b->m[6] + a->m[12] * b->m[7];
209 result->m[5] = a->m[1] * b->m[4] + a->m[5] * b->m[5] + a->m[9] * b->m[6] + a->m[13] * b->m[7];
210 result->m[6] = a->m[2] * b->m[4] + a->m[6] * b->m[5] + a->m[10] * b->m[6] + a->m[14] * b->m[7];
211 result->m[7] = a->m[3] * b->m[4] + a->m[7] * b->m[5] + a->m[11] * b->m[6] + a->m[15] * b->m[7];
213 result->m[8] = a->m[0] * b->m[8] + a->m[4] * b->m[9] + a->m[8] * b->m[10] + a->m[12] * b->m[11];
214 result->m[9] = a->m[1] * b->m[8] + a->m[5] * b->m[9] + a->m[9] * b->m[10] + a->m[13] * b->m[11];
215 result->m[10] = a->m[2] * b->m[8] + a->m[6] * b->m[9] + a->m[10] * b->m[10] + a->m[14] * b->m[11];
216 result->m[11] = a->m[3] * b->m[8] + a->m[7] * b->m[9] + a->m[11] * b->m[10] + a->m[15] * b->m[11];
219 a->m[0] * b->m[12] + a->m[4] * b->m[13] + a->m[8] * b->m[14] + a->m[12] * b->m[15];
221 a->m[1] * b->m[12] + a->m[5] * b->m[13] + a->m[9] * b->m[14] + a->m[13] * b->m[15];
223 a->m[2] * b->m[12] + a->m[6] * b->m[13] + a->m[10] * b->m[14] + a->m[14] * b->m[15];
225 a->m[3] * b->m[12] + a->m[7] * b->m[13] + a->m[11] * b->m[14] + a->m[15] * b->m[15];
231 result->m[0] = src->m[0];
232 result->m[1] = src->m[4];
233 result->m[2] = src->m[8];
235 result->m[4] = src->m[1];
236 result->m[5] = src->m[5];
237 result->m[6] = src->m[9];
239 result->m[8] = src->m[2];
240 result->m[9] = src->m[6];
241 result->m[10] = src->m[10];
242 result->m[11] = 0.0f;
243 result->m[12] = -(src->m[0] * src->m[12] + src->m[1] * src->m[13] + src->m[2] * src->m[14]);
244 result->m[13] = -(src->m[4] * src->m[12] + src->m[5] * src->m[13] + src->m[6] * src->m[14]);
245 result->m[14] = -(src->m[8] * src->m[12] + src->m[9] * src->m[13] + src->m[10] * src->m[14]);
246 result->m[15] = 1.0f;
251 const XrVector3f *translation,
252 const XrQuaternionf *rotation)
256 XrMatrix4x4f_CreateFromQuaternion(&rotationMatrix, rotation);
259 XrMatrix4x4f_CreateTranslation(&translationMatrix, translation->x, translation->y, translation->z);
262 XrMatrix4x4f_Multiply(&viewMatrix, &translationMatrix, &rotationMatrix);
264 XrMatrix4x4f_Invert(result, &viewMatrix);
269XrMatrix4x4f_CreateScale(
XrMatrix4x4f *result,
const float x,
const float y,
const float z)
282 result->m[11] = 0.0f;
283 result->m[12] = 0.0f;
284 result->m[13] = 0.0f;
285 result->m[14] = 0.0f;
286 result->m[15] = 1.0f;
291 const XrVector3f *translation,
292 const XrQuaternionf *rotation,
293 const XrVector3f *scale)
296 XrMatrix4x4f_CreateScale(&scaleMatrix, scale->x, scale->y, scale->z);
299 XrMatrix4x4f_CreateFromQuaternion(&rotationMatrix, rotation);
302 XrMatrix4x4f_CreateTranslation(&translationMatrix, translation->x, translation->y, translation->z);
305 XrMatrix4x4f_Multiply(&combinedMatrix, &rotationMatrix, &scaleMatrix);
306 XrMatrix4x4f_Multiply(result, &translationMatrix, &combinedMatrix);