diff options
| author | Tyge <[email protected]> | 2020-04-19 14:52:39 +0200 |
|---|---|---|
| committer | Tyge <[email protected]> | 2020-04-19 14:57:19 +0200 |
| commit | 76c8cc6e95e630fab87941f4366a46fb6587d7f5 (patch) | |
| tree | 7f494ef800875ae7280d901913feae0c58622113 /glm | |
| parent | d7f49e5c1815a2b5ab75c32378d6d7739c5c0495 (diff) | |
| download | STC-modified-76c8cc6e95e630fab87941f4366a46fb6587d7f5.tar.gz STC-modified-76c8cc6e95e630fab87941f4366a46fb6587d7f5.zip | |
Moved gl math to glm folder
Added clist - single linked list with fast sorting.
Refactoring.
Diffstat (limited to 'glm')
| -rw-r--r-- | glm/mat3.h | 137 | ||||
| -rw-r--r-- | glm/mat4.h | 204 | ||||
| -rw-r--r-- | glm/quat.h | 206 | ||||
| -rw-r--r-- | glm/transform.h | 148 | ||||
| -rw-r--r-- | glm/vec3.h | 219 | ||||
| -rw-r--r-- | glm/vec4.h | 190 |
6 files changed, 1104 insertions, 0 deletions
diff --git a/glm/mat3.h b/glm/mat3.h new file mode 100644 index 00000000..ad9e03ea --- /dev/null +++ b/glm/mat3.h @@ -0,0 +1,137 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef GLM_MAT3__H__ +#define GLM_MAT3__H__ + +#include "vec3.h" + +#define glm_mat3_identity_init {1, 0, 0, 0, 1, 0, 0, 0, 1} +#define glm_mat3_zero_init {0, 0, 0, 0, 0, 0, 0, 0, 0} + +#define glm_mat3f_identity ((glm_mat3f) glm_mat3_identity_init) +#define glm_mat3d_identity ((glm_mat3d) glm_mat3_identity_init) +#define glm_mat3f_zero ((glm_mat3f) glm_mat3_zero_init) +#define glm_mat3d_zero ((glm_mat3d) glm_mat3_zero_init) + +#define declare_glm_mat3(tag, T) \ + typedef const T (*glm_mat3##tag##m)[3]; \ + \ + typedef union glm_mat3##tag { \ + glm_vec3##tag v[3]; \ + T m[3][3], arr[3*3]; \ + } glm_mat3##tag; \ + \ + static inline glm_mat3##tag \ + glm_mat3##tag##_mult(glm_mat3##tag##m m1, glm_mat3##tag##m m2) { \ + const T *a = m1[0], *b = m2[0]; \ + return (glm_mat3##tag) { \ + a[0] * b[0] + a[3] * b[1] + a[6] * b[2], \ + a[1] * b[0] + a[4] * b[1] + a[7] * b[2], \ + a[2] * b[0] + a[5] * b[1] + a[8] * b[2], \ + a[0] * b[3] + a[3] * b[4] + a[6] * b[5], \ + a[1] * b[3] + a[4] * b[4] + a[7] * b[5], \ + a[2] * b[3] + a[5] * b[4] + a[8] * b[5], \ + a[0] * b[6] + a[3] * b[7] + a[6] * b[8], \ + a[1] * b[6] + a[4] * b[7] + a[7] * b[8], \ + a[2] * b[6] + a[5] * b[7] + a[8] * b[8], \ + }; \ + } \ + \ + static inline glm_mat3##tag* \ + glm_mat3##tag##_scale(glm_mat3##tag* self, T s) { \ + T* a = self->arr; \ + a[0] *= s, a[1] *= s, a[2] *= s, \ + a[3] *= s, a[4] *= s, a[5] *= s, \ + a[6] *= s, a[7] *= s, a[8] *= s; \ + return self; \ + } \ + static inline glm_mat3##tag \ + glm_mat3##tag##_scalarMult(glm_mat3##tag##m m, T s) { \ + glm_mat3##tag dst; \ + T *c = dst.arr; const T *a = m[0]; \ + for (int i = 0; i < 9; ++i) *c++ = *a++ * s; \ + return dst; \ + } \ + static inline glm_mat3##tag \ + glm_mat3##tag##_compMult(glm_mat3##tag##m m1, glm_mat3##tag##m m2) { \ + glm_mat3##tag dst; \ + T *c = dst.arr; const T *a = m1[0], *b = m2[0]; \ + for (int i = 0; i < 9; ++i) *c++ = *a++ * *b++; \ + return dst; \ + } \ + static inline glm_vec3##tag \ + glm_mat3##tag##_vecMult(glm_mat3##tag##m m, glm_vec3##tag v) { \ + return (glm_vec3##tag) { \ + m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z, \ + m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z, \ + m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z, \ + }; \ + } \ + \ + static inline glm_mat3##tag \ + glm_mat3##tag##_transpose(glm_mat3##tag##m m) { \ + return (glm_mat3##tag) { \ + m[0][0], m[1][0], m[2][0], \ + m[0][1], m[1][1], m[2][1], \ + m[0][2], m[1][2], m[2][2], \ + }; \ + } \ + \ + static inline T \ + glm_mat3##tag##_trace(glm_mat3##tag##m m) { \ + return m[0][0] + m[1][1] + m[2][2]; \ + } \ + \ + \ + static inline T \ + glm_mat3##tag##_determinant(glm_mat3##tag##m m) { \ + const T *a = m[0]; \ + return a[0] * (a[4] * a[8] - a[7] * a[5]) \ + - a[3] * (a[1] * a[8] - a[2] * a[7]) \ + + a[6] * (a[1] * a[5] - a[2] * a[4]); \ + } \ + \ + static inline glm_mat3##tag \ + glm_mat3##tag##_inverse(glm_mat3##tag##m m) { \ + const T *a = m[0]; \ + T k = 1.0f / glm_mat3##tag##_determinant(m); \ + return (glm_mat3##tag) { \ + (a[4] * a[8] - a[5] * a[7]) * k, \ + -(a[1] * a[8] - a[7] * a[2]) * k, \ + (a[1] * a[5] - a[4] * a[2]) * k, \ + -(a[3] * a[8] - a[6] * a[5]) * k, \ + (a[0] * a[8] - a[2] * a[6]) * k, \ + -(a[0] * a[5] - a[3] * a[2]) * k, \ + (a[3] * a[7] - a[6] * a[4]) * k, \ + -(a[0] * a[7] - a[6] * a[1]) * k, \ + (a[0] * a[4] - a[1] * a[3]) * k, \ + }; \ + } \ + \ + typedef T glm_mat3##tag##_value_t + +declare_glm_mat3(d, double); +declare_glm_mat3(f, float); + + +#endif diff --git a/glm/mat4.h b/glm/mat4.h new file mode 100644 index 00000000..34638ec5 --- /dev/null +++ b/glm/mat4.h @@ -0,0 +1,204 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef GLM_MAT4__H__ +#define GLM_MAT4__H__ + +#include "mat3.h" +#include "vec4.h" + +#define glm_mat4_identity_init {1, 0, 0, 0, 0, 1, 0, 0, \ + 0, 0, 1, 0, 0, 0, 0, 1} +#define glm_mat4_zero_init {0, 0, 0, 0, 0, 0, 0, 0, \ + 0, 0, 0, 0, 0, 0, 0, 0} + +#define glm_mat4f_identity ((glm_mat4f) glm_mat4_identity_init) +#define glm_mat4d_identity ((glm_mat4d) glm_mat4_identity_init) +#define glm_mat4f_zero ((glm_mat4f) glm_mat4_zero_init) +#define glm_mat4d_zero ((glm_mat4d) glm_mat4_zero_init) + +#define declare_glm_mat4(tag, T) \ + typedef const T (*glm_mat4##tag##m)[4]; \ + \ + typedef union glm_mat4##tag { \ + glm_vec4##tag v[4]; \ + T m[4][4], arr[4*4]; \ + } glm_mat4##tag; \ + \ + static inline glm_mat4##tag \ + glm_mat4##tag##_mult(glm_mat4##tag##m mat1, glm_mat4##tag##m mat2) { \ + const T *a = mat1[0], *b = mat2[0]; \ + return (glm_mat4##tag) { \ + a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3], \ + a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3], \ + a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3], \ + a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3], \ + a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7], \ + a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7], \ + a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7], \ + a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7], \ + a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11], \ + a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11], \ + a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11], \ + a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11], \ + a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15], \ + a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15], \ + a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15], \ + a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15], \ + }; \ + } \ + \ + static inline glm_mat4##tag* \ + glm_mat4##tag##_scale(glm_mat4##tag* self, T s) { \ + T* a = self->arr; \ + for (int i = 0; i < 16; ++i) *a++ *= s; \ + return self; \ + } \ + static inline glm_mat4##tag \ + glm_mat4##tag##_scalarMult(glm_mat4##tag##m m, T s) { \ + glm_mat4##tag dst; \ + T *c = dst.arr; const T *a = m[0]; \ + for (int i = 0; i < 16; ++i) *c++ = *a++ * s; \ + return dst; \ + } \ + static inline glm_mat4##tag \ + glm_mat4##tag##_compMult(glm_mat4##tag##m m1, glm_mat4##tag##m m2) { \ + glm_mat4##tag dst; \ + T *c = dst.arr; const T *a = m1[0], *b = m2[0]; \ + for (int i = 0; i < 16; ++i) *c++ = *a++ * *b++; \ + return dst; \ + } \ + static inline glm_vec4##tag \ + glm_mat4##tag##_vecMult(glm_mat4##tag##m m, glm_vec4##tag v) { \ + return (glm_vec4##tag) { \ + m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z + m[3][0] * v.w, \ + m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z + m[3][1] * v.w, \ + m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z + m[3][2] * v.w, \ + m[0][3] * v.x + m[1][3] * v.y + m[2][3] * v.z + m[3][3] * v.w, \ + }; \ + } \ + \ + static inline glm_mat4##tag \ + glm_mat4##tag##_transpose(glm_mat4##tag##m m) { \ + return (glm_mat4##tag) { \ + m[0][0], m[1][0], m[2][0], m[3][0], \ + m[0][1], m[1][1], m[2][1], m[3][1], \ + m[0][2], m[1][2], m[2][2], m[3][2], \ + m[0][3], m[1][3], m[2][3], m[3][3], \ + }; \ + } \ + \ + static inline glm_mat3##tag \ + glm_mat4##tag##_transpose3(glm_mat4##tag##m m) { \ + return (glm_mat3##tag) { \ + m[0][0], m[1][0], m[2][0], \ + m[0][1], m[1][1], m[2][1], \ + m[0][2], m[1][2], m[2][2], \ + }; \ + } \ + \ + static inline glm_mat3##tag \ + glm_mat4##tag##_to3(glm_mat4##tag##m m) { \ + return (glm_mat3##tag) { \ + m[0][0], m[0][1], m[0][2], \ + m[1][0], m[1][1], m[1][2], \ + m[2][0], m[2][1], m[2][2], \ + }; \ + } \ + \ + static inline T \ + glm_mat4##tag##_trace(glm_mat4##tag##m m) { \ + return m[0][0] + m[1][1] + m[2][2] + m[3][3]; \ + } \ + \ + static inline T \ + glm_mat4##tag##_determinant(glm_mat4##tag##m mat) { \ + const T *p = mat[0]; \ + T a, b, c, d, e, f; \ + a = p[10] * p[15] - p[14] * p[11], \ + b = p[ 9] * p[15] - p[13] * p[11], \ + c = p[ 9] * p[14] - p[13] * p[10], \ + d = p[ 8] * p[15] - p[12] * p[11], \ + e = p[ 8] * p[14] - p[12] * p[10], \ + f = p[ 8] * p[13] - p[12] * p[ 9]; \ + return p[0] * (p[5] * a - p[6] * b + p[7] * c) \ + - p[1] * (p[4] * a - p[6] * d + p[7] * e) \ + + p[2] * (p[4] * b - p[5] * d + p[7] * f) \ + - p[3] * (p[4] * c - p[5] * e + p[6] * f); \ + } \ + \ + \ + static inline glm_mat4##tag \ + glm_mat4##tag##_inverse(glm_mat4##tag##m mat) { \ + glm_mat4##tag dst; \ + const T *p = mat[0]; \ + T a, b, c, d, e, f, det; \ + a = p[10] * p[15] - p[14] * p[11], \ + b = p[ 9] * p[15] - p[13] * p[11], \ + c = p[ 9] * p[14] - p[13] * p[10], \ + d = p[ 8] * p[15] - p[12] * p[11], \ + e = p[ 8] * p[14] - p[12] * p[10], \ + f = p[ 8] * p[13] - p[12] * p[ 9]; \ + dst.m[0][0] = (p[5] * a - p[6] * b + p[7] * c); \ + dst.m[1][0] = -(p[4] * a - p[6] * d + p[7] * e); \ + dst.m[2][0] = (p[4] * b - p[5] * d + p[7] * f); \ + dst.m[3][0] = -(p[4] * c - p[5] * e + p[6] * f); \ + \ + dst.m[0][1] = -(p[1] * a - p[2] * b + p[3] * c); \ + dst.m[1][1] = (p[0] * a - p[2] * d + p[3] * e); \ + dst.m[2][1] = -(p[0] * b - p[1] * d + p[3] * f); \ + dst.m[3][1] = (p[0] * c - p[1] * e + p[2] * f); \ + \ + a = p[6] * p[15] - p[14] * p[7], \ + b = p[5] * p[15] - p[13] * p[7], \ + c = p[5] * p[14] - p[13] * p[6], \ + d = p[4] * p[15] - p[12] * p[7], \ + e = p[4] * p[14] - p[12] * p[6], \ + f = p[4] * p[13] - p[12] * p[5]; \ + dst.m[0][2] = (p[1] * a - p[2] * b + p[3] * c); \ + dst.m[1][2] = -(p[0] * a - p[2] * d + p[3] * e); \ + dst.m[2][2] = (p[0] * b - p[1] * d + p[3] * f); \ + dst.m[3][2] = -(p[0] * c - p[1] * e + p[2] * f); \ + \ + a = p[6] * p[11] - p[10] * p[7], \ + b = p[5] * p[11] - p[ 9] * p[7], \ + c = p[5] * p[10] - p[ 9] * p[6], \ + d = p[4] * p[11] - p[ 8] * p[7], \ + e = p[4] * p[10] - p[ 8] * p[6], \ + f = p[4] * p[ 9] - p[ 8] * p[5]; \ + dst.m[0][3] = -(p[1] * a - p[2] * b + p[3] * c); \ + dst.m[1][3] = (p[0] * a - p[2] * d + p[3] * e); \ + dst.m[2][3] = -(p[0] * b - p[1] * d + p[3] * f); \ + dst.m[3][3] = (p[0] * c - p[1] * e + p[2] * f); \ + \ + det = p[0] * dst.m[0][0] + p[1] * dst.m[1][0] \ + + p[2] * dst.m[2][0] + p[3] * dst.m[3][0]; \ + return *glm_mat4##tag##_scale(&dst, 1.0f / det); \ + } \ + \ + typedef T glm_mat4##tag##_value_t + +declare_glm_mat4(d, double); +declare_glm_mat4(f, float); + + +#endif diff --git a/glm/quat.h b/glm/quat.h new file mode 100644 index 00000000..40124eff --- /dev/null +++ b/glm/quat.h @@ -0,0 +1,206 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef GLM_QUAT__H__ +#define GLM_QUAT__H__ + +#include "mat4.h" + +#define glm_quat_arr(q) (&(q).x) +#define glm_quatf_identity ((glm_quatf) {0.f, 0.f, 0.f, 1.f}) +#define glm_quatd_identity ((glm_quatd) {0.0, 0.0, 0.0, 1.0}) + +#define declare_glm_quat(tag, T) \ + typedef glm_vec4##tag glm_quat##tag; \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_init(T x, T y, T z, T w) { return (glm_quat##tag) {x, y, z, w}; } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_unit(glm_quat##tag q) { \ + T dot = _glm_vec4_DOT(q, q); \ + if (dot <= 0.0f) return glm_quat##tag##_identity; \ + return glm_vec4##tag##_mult(q, 1.0f / glm_sqrt_##tag(dot)); \ + } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_fromAxis(T angle, glm_vec3##tag axis) { \ + T a = angle * 0.5f, c = glm_cos_##tag(a), s = glm_sin_##tag(a); \ + glm_vec3##tag u = glm_vec3##tag##_unit(axis); \ + return (glm_quat##tag) {s * u.x, s * u.y, s * u.z, c}; \ + } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_fromVectors(glm_vec3##tag u, glm_vec3##tag v) { \ + T d = glm_sqrt_##tag(_glm_vec3_DOT(u, u) * _glm_vec3_DOT(v, v)); \ + T real_part = d + _glm_vec3_DOT(u, v); \ + glm_vec3##tag t; \ + if (real_part < 1.e-6f * d) { \ + /* If u and v are exactly opposite, rotate 180 degrees */ \ + /* around an arbitrary orthogonal axis. */ \ + real_part = 0; \ + t = fabs(u.x) > fabs(u.z) ? (glm_vec3##tag) {-u.y, u.x, 0} : (glm_vec3##tag) {0, -u.z, u.y}; \ + } else /* Build quaternion the standard way.*/ \ + t = _glm_vec3_CROSS(u, v); \ + glm_quat##tag q = (glm_quat##tag) {t.x, t.y, t.z, real_part}; \ + return glm_quat##tag##_unit(q); \ + } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_fromMat4(glm_mat4##tag##m m) { \ + T trace = m[0][0] + m[1][1] + m[2][2], r, rinv; \ + glm_quat##tag dst; \ + if (trace >= 0.0f) { \ + r = glm_sqrt_##tag(1.0f + trace); \ + rinv = 0.5f / r; \ + dst.x = rinv * (m[1][2] - m[2][1]); \ + dst.y = rinv * (m[2][0] - m[0][2]); \ + dst.z = rinv * (m[0][1] - m[1][0]); \ + dst.w = r * 0.5f; \ + } else if (m[0][0] >= m[1][1] && m[0][0] >= m[2][2]) { \ + r = glm_sqrt_##tag(1.0f + m[0][0] - m[1][1] - m[2][2]); \ + rinv = 0.5f / r; \ + dst.x = r * 0.5f; \ + dst.y = rinv * (m[0][1] + m[1][0]); \ + dst.z = rinv * (m[0][2] + m[2][0]); \ + dst.w = rinv * (m[1][2] - m[2][1]); \ + } else if (m[1][1] >= m[2][2]) { \ + r = glm_sqrt_##tag(1.0f - m[0][0] + m[1][1] - m[2][2]); \ + rinv = 0.5f / r; \ + dst.x = rinv * (m[0][1] + m[1][0]); \ + dst.y = r * 0.5f; \ + dst.z = rinv * (m[1][2] + m[2][1]); \ + dst.w = rinv * (m[2][0] - m[0][2]); \ + } else { \ + r = glm_sqrt_##tag(1.0f - m[0][0] - m[1][1] + m[2][2]); \ + rinv = 0.5f / r; \ + dst.x = rinv * (m[0][2] + m[2][0]); \ + dst.y = rinv * (m[1][2] + m[2][1]); \ + dst.z = r * 0.5f; \ + dst.w = rinv * (m[0][1] - m[1][0]); \ + } \ + return dst; \ + } \ + \ + static inline glm_mat4##tag \ + glm_quat##tag##_toMat4(glm_quat##tag q) { \ + glm_mat4##tag dst; \ + T norm = glm_quat##tag##_length(q), \ + s = norm > 0.0f ? 2.0f / norm : 0.0f, \ + xx = s * q.x * q.x, xy = s * q.x * q.y, wx = s * q.w * q.x, \ + yy = s * q.y * q.y, yz = s * q.y * q.z, wy = s * q.w * q.y, \ + zz = s * q.z * q.z, xz = s * q.x * q.z, wz = s * q.w * q.z; \ + \ + dst.m[0][0] = 1.0f - yy - zz; \ + dst.m[1][1] = 1.0f - xx - zz; \ + dst.m[2][2] = 1.0f - xx - yy; \ + dst.m[0][1] = xy + wz; \ + dst.m[1][2] = yz + wx; \ + dst.m[2][0] = xz + wy; \ + dst.m[1][0] = xy - wz; \ + dst.m[2][1] = yz - wx; \ + dst.m[0][2] = xz - wy; \ + dst.m[0][3] = dst.m[1][3] = dst.m[2][3] = 0.0f; \ + dst.m[3][0] = dst.m[3][1] = dst.m[3][2] = 0.0f; \ + dst.m[3][3] = 1.0f; \ + return dst; \ + } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_conjugate(glm_quat##tag q) { \ + q.x = -q.x, q.y = -q.y, q.z = -q.z; \ + return q; \ + } \ + \ + static inline glm_quat##tag \ + glm_quat##tag##_cross(glm_quat##tag p, glm_quat##tag q) { \ + return (glm_quat##tag) {p.w * q.x + p.x * q.w + p.y * q.z - p.z * q.y, \ + p.w * q.y - p.x * q.z + p.y * q.w + p.z * q.x, \ + p.w * q.z + p.x * q.y - p.y * q.x + p.z * q.w, \ + p.w * q.w - p.x * q.x - p.y * q.y - p.z * q.z}; \ + } \ + static inline glm_quat##tag \ + glm_quat##tag##_inverse(glm_quat##tag q) { \ + q.x = -q.x, q.y = -q.y, q.z = -q.z; \ + return glm_vec4##tag##_mult(q, 1.0f / _glm_vec4_DOT(q, q)); \ + } \ + static inline T \ + glm_quat##tag##_angle(glm_quat##tag q) { \ + /* sin(theta / 2) = length(x*x + y*y + z*z) */ \ + /* cos(theta / 2) = w */ \ + /* theta = 2 * atan(sin(theta / 2) / cos(theta / 2)) */ \ + return 2.0f * glm_atan2_##tag(glm_sqrt_##tag(_glm_vec3_DOT(q, q)), q.w); \ + } \ + static inline T \ + glm_quat##tag##_imagLength(glm_quat##tag q) { \ + return glm_sqrt_##tag(_glm_vec3_DOT(q, q)); \ + } \ + typedef T glm_quat##tag##_value_t + +#define glm_quatf_fromArray(arr) glm_vec4f_fromArray(arr) +#define glm_quatd_fromArray(arr) glm_vec4d_fromArray(arr) + +#define glm_quatf_assign(q, x, y, z, w) glm_vec4f_assign(q, x, y, z, w) +#define glm_quatd_assign(q, x, y, z, w) glm_vec4d_assign(q, x, y, z, w) + +#define glm_quatf_assignArray(q, arr) glm_vec4f_assignArray(q, arr) +#define glm_quatd_assignArray(q, arr) glm_vec4d_assignArray(q, arr) + +#define glm_quatf_length(q) glm_vec4f_length(q) +#define glm_quatd_length(q) glm_vec4d_length(q) + +#define glm_quatf_length2(q) glm_vec4f_length2(q) +#define glm_quatd_length2(q) glm_vec4d_length2(q) + +#define glm_quatf_dot(p, q) glm_vec4f_dot(p, q) +#define glm_quatd_dot(p, q) glm_vec4d_dot(p, q) + +#define glm_quatf_lerp(p, q, t) glm_vec4f_lerp(p, q, t) +#define glm_quatd_lerp(p, q, t) glm_vec4d_lerp(p, q, t) + +#define glm_quatf_swizzle(q, swz) glm_vec4f_swizzle(q, swz) +#define glm_quatd_swizzle(q, swz) glm_vec4d_swizzle(q, swz) + +#define glm_quatf_equals(p, q) glm_vec4f_equals(p, q) +#define glm_quatd_equals(p, q) glm_vec4d_equals(p, q) + +#define glm_quatf_compare(p, q) glm_vec4f_compare(p, q) +#define glm_quatd_compare(p, q) glm_vec4d_compare(p, q) + +#define glm_quatf_imag(q) glm_vec4f_to3(q) +#define glm_quatd_imag(q) glm_vec4d_to3(q) + +#define glm_quatf_real(q) ((float) (q).w) +#define glm_quatd_real(q) ((double) (q).w) + +declare_glm_quat(f, float); +declare_glm_quat(d, double); + + +static inline glm_quatd glm_quatf_tod(glm_quatf v) { + return (glm_quatd) {v.x, v.y, v.z, v.w}; +} +static inline glm_quatf glm_quatd_tof(glm_quatd v) { + return (glm_quatf) {(float) v.x, (float) v.y, (float) v.z, (float) v.w}; +} + +#endif diff --git a/glm/transform.h b/glm/transform.h new file mode 100644 index 00000000..a301b4f8 --- /dev/null +++ b/glm/transform.h @@ -0,0 +1,148 @@ +#include "mat4.h" + +#define declare_mat4_transform(tag, T) \ + /* right handed, opengl spec functions */ \ + \ + static inline glm_mat4##tag glm_mat4##tag_##ortho(T left, T right, T bottom, T top, T zNear, T zFar) \ + { \ + glm_mat4##tag dst = glm_mat4##tag##_identity; \ + dst.m[0][0] = 2.0f / (right - left); \ + dst.m[1][1] = 2.0f / (top - bottom); \ + dst.m[2][2] = - 2.0f / (zFar - zNear); \ + dst.m[3][0] = - (right + left) / (right - left); \ + dst.m[3][1] = - (top + bottom) / (top - bottom); \ + dst.m[3][2] = - (zFar + zNear) / (zFar - zNear); \ + return dst; \ + } \ + \ + static inline glm_mat4##tag glm_mat4##tag_##frustum(T left, T right, T bottom, T top, T nearVal, T farVal) \ + { \ + glm_mat4##tag dst = glm_mat4##tag##_zero; \ + dst.m[0][0] = (2.0f * nearVal) / (right - left); \ + dst.m[1][1] = (2.0f * nearVal) / (top - bottom); \ + dst.m[2][0] = (right + left) / (right - left); \ + dst.m[2][1] = (top + bottom) / (top - bottom); \ + dst.m[2][2] = - (farVal + nearVal) / (farVal - nearVal); \ + dst.m[2][3] = - 1.0f; \ + dst.m[3][2] = - (2.0f * farVal * nearVal) / (farVal - nearVal); \ + return dst; \ + } \ + \ + static inline glm_mat4##tag glm_mat4##tag##_perspective(T fovy, T aspect, T zNear, T zFar) \ + { \ + const T tanHalfFovy = glm_tan_##tag(fovy / 2.0f); \ + \ + glm_mat4##tag dst = glm_mat4##tag##_zero; \ + dst.m[0][0] = 1.0f / (aspect * tanHalfFovy); \ + dst.m[1][1] = 1.0f / (tanHalfFovy); \ + dst.m[2][2] = - (zFar + zNear) / (zFar - zNear); \ + dst.m[2][3] = - 1.0f; \ + dst.m[3][2] = - (2.0f * zFar * zNear) / (zFar - zNear); \ + return dst; \ + } \ + \ + static inline glm_mat4##tag glm_mat4##tag_##perspectiveFov(T fov, T width, T height, T zNear, T zFar) \ + { \ + const T rad = fov; \ + const T h = glm_cos_##tag(0.5f * rad) / glm_sin_##tag(0.5f * rad); \ + const T w = h * height / width; /* todo max(width , Height) / min(width , Height)? */ \ + \ + glm_mat4##tag dst = glm_mat4##tag##_zero; \ + dst.m[0][0] = w; \ + dst.m[1][1] = h; \ + dst.m[2][2] = - (zFar + zNear) / (zFar - zNear); \ + dst.m[2][3] = - 1.0f; \ + dst.m[3][2] = - (2.0f * zFar * zNear) / (zFar - zNear); \ + return dst; \ + } \ + \ + static inline glm_mat4##tag glm_mat4##tag_##infinitePerspective(T fovy, T aspect, T zNear) \ + { \ + const T range = glm_tan_##tag(fovy / 2.0f) * zNear; \ + const T left = -range * aspect; \ + const T right = range * aspect; \ + const T bottom = -range; \ + const T top = range; \ + \ + glm_mat4##tag dst = glm_mat4##tag##_zero; \ + dst.m[0][0] = (2.0f * zNear) / (right - left); \ + dst.m[1][1] = (2.0f * zNear) / (top - bottom); \ + dst.m[2][2] = - 1.0f; \ + dst.m[2][3] = - 1.0f; \ + dst.m[3][2] = - 2.0f * zNear; \ + return dst; \ + } + +// ------------ + + static inline glm_mat4##tag translate(const glm_mat4##tag##m m, glm_vec3##tag v) + { + glm_mat4##tag dst = *(const glm_mat4##tag *) m; + dst.m[3] = m[0] * v[0] + m[1] * v.y + m[2] * v.z + m[3]; + return dst; + } + + static inline glm_mat4##tag rotate(glm_mat4##tag##m m, T angle, glm_vec3##tag v) + { + const T a = angle; + const T c = cos(a); + const T s = sin(a); + + glm_vec3##tag axis(normalize(v)); + glm_vec3##tag temp((T(1) - c) * axis); + + glm_mat4##tag rot; + rot.m[0][0] = c + temp[0] * axis[0]; + rot.m[0][1] = temp[0] * axis[1] + s * axis[2]; + rot.m[0][2] = temp[0] * axis[2] - s * axis[1]; + + rot.m[1][0] = temp[1] * axis[0] - s * axis[2]; + rot.m[1][1] = c + temp[1] * axis[1]; + rot.m[1][2] = temp[1] * axis[2] + s * axis[0]; + + rot.m[2][0] = temp[2] * axis[0] + s * axis[1]; + rot.m[2][1] = temp[2] * axis[1] - s * axis[0]; + rot.m[2][2] = c + temp[2] * axis[2]; + + glm_mat4##tag dst; + dst.m[0] = m[0] * rot.m[0][0] + m[1] * rot.m[0][1] + m[2] * rot.m[0][2]; + dst.m[1] = m[0] * rot.m[1][0] + m[1] * rot.m[1][1] + m[2] * rot.m[1][2]; + dst.m[2] = m[0] * rot.m[2][0] + m[1] * rot.m[2][1] + m[2] * rot.m[2][2]; + dst.m[3] = m[3]; + return dst; + } + + + static inline glm_mat4##tag scale(glm_mat4##tag##m m, glm_vec3##tag v) + { + glm_mat4##tag dst; + dst.m[0] = m[0] * v[0]; + dst.m[1] = m[1] * v[1]; + dst.m[2] = m[2] * v[2]; + dst.m[3] = m[3]; + return dst; + } + + static inline glm_mat4##tag lookAtRH(glm_vec3##tag eye, glm_vec3##tag center, glm_vec3##tag up) + { + const glm_vec3##tag f(normalize(center - eye)); + const glm_vec3##tag s(normalize(cross(f, up))); + const glm_vec3##tag u(cross(s, f)); + + glm_mat4##tag dst(1); + dst.m[0][0] = s.x; + dst.m[1][0] = s.y; + dst.m[2][0] = s.z; + dst.m[0][1] = u.x; + dst.m[1][1] = u.y; + dst.m[2][1] = u.z; + dst.m[0][2] =-f.x; + dst.m[1][2] =-f.y; + dst.m[2][2] =-f.z; + dst.m[3][0] =-dot(s, eye); + dst.m[3][1] =-dot(u, eye); + dst.m[3][2] = dot(f, eye); + return dst; + } + +}//namespace glm diff --git a/glm/vec3.h b/glm/vec3.h new file mode 100644 index 00000000..fb66ce42 --- /dev/null +++ b/glm/vec3.h @@ -0,0 +1,219 @@ +// MIT License
+//
+// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+#ifndef GLM_VEC3__H__
+#define GLM_VEC3__H__
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <math.h>
+
+#define glm_sqrt_f(x) sqrtf(x)
+#define glm_sqrt_d(x) sqrt(x)
+#define glm_sin_f(x) sinf(x)
+#define glm_sin_d(x) sin(x)
+#define glm_cos_f(x) cosf(x)
+#define glm_cos_d(x) cos(x)
+#define glm_atan2_f(x, y) atan2f(x, y)
+#define glm_atan2_d(x, y) atan2(x, y)
+
+#ifdef GLM_NO_ANONYMOUS_STRUCT
+#define _glm_VEC3(tag, T) struct glm_vec3##tag { T x, y, z; }
+#else
+#define _glm_VEC3(tag, T) union glm_vec3##tag { struct { T x, y, z; }; T arr[3]; }
+#endif
+
+#define glm_vec3_arr(v) (&(v).x)
+#define glm_vec3f_zero ((glm_vec3f) {0.f, 0.f, 0.f})
+#define glm_vec3d_zero ((glm_vec3d) {0.0, 0.0, 0.0})
+
+#define declare_glm_vec3(tag, T) \
+ typedef _glm_VEC3(tag, T) glm_vec3##tag; \
+ static glm_vec3##tag \
+ glm_vec3##tag##_axis[3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; \
+ \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_init(T x, T y, T z) { return (glm_vec3##tag) {x, y, z}; } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_fromArray(const T* a) { return (glm_vec3##tag) {a[0], a[1], a[2]}; } \
+ \
+ static inline glm_vec3##tag* \
+ glm_vec3##tag##_assign(glm_vec3##tag* self, T x, T y, T z) { \
+ self->x = x, self->y = y, self->z = z; return self; \
+ } \
+ static inline glm_vec3##tag* \
+ glm_vec3##tag##_assignArray(glm_vec3##tag* self, const T* a) { \
+ self->x = a[0], self->y = a[1], self->z = a[2]; return self; \
+ } \
+ static inline glm_vec3##tag* \
+ glm_vec3##tag##_add(glm_vec3##tag* self, glm_vec3##tag v) { \
+ self->x += v.x, self->y += v.y, self->z += v.z; return self; \
+ } \
+ static inline glm_vec3##tag* \
+ glm_vec3##tag##_sub(glm_vec3##tag* self, glm_vec3##tag v) { \
+ self->x -= v.x, self->y -= v.y, self->z -= v.z; return self; \
+ } \
+ static inline glm_vec3##tag* \
+ glm_vec3##tag##_scale(glm_vec3##tag* self, T s) { \
+ self->x *= s, self->y *= s, self->z *= s; return self; \
+ } \
+ static inline T \
+ glm_vec3##tag##_length(glm_vec3##tag v) { \
+ return glm_sqrt_##tag(_glm_vec3_DOT(v, v)); \
+ } \
+ static inline T \
+ glm_vec3##tag##_length2(glm_vec3##tag v) { \
+ return _glm_vec3_DOT(v, v); \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_plus(glm_vec3##tag u, glm_vec3##tag v) { \
+ u.x += v.x, u.y += v.y, u.z += v.z; return u; \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_minus(glm_vec3##tag u, glm_vec3##tag v) { \
+ u.x -= v.x, u.y -= v.y, u.z -= v.z; return u; \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_mult(glm_vec3##tag v, T s) { \
+ v.x *= s, v.y *= s, v.z *= s; return v; \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_neg(glm_vec3##tag v) { \
+ v.x = -v.x, v.y = -v.y, v.z = -v.z; return v; \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_unit(glm_vec3##tag v) { \
+ T s = 1.0f / glm_sqrt_##tag(_glm_vec3_DOT(v, v)); \
+ s; v.x *= s, v.y *= s, v.z *= s; \
+ return v; \
+ } \
+ static inline T \
+ glm_vec3##tag##_dot(glm_vec3##tag u, glm_vec3##tag v) { \
+ return _glm_vec3_DOT(u, v); \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_cross(glm_vec3##tag u, glm_vec3##tag v) { \
+ return (glm_vec3##tag) {_glm_vec3_CROSS(u, v)}; \
+ } \
+ static inline T \
+ glm_vec3##tag##_triple(glm_vec3##tag u, glm_vec3##tag v, glm_vec3##tag w) { \
+ glm_vec3##tag cross = {_glm_vec3_CROSS(u, v)}; \
+ return _glm_vec3_DOT(cross, w); \
+ } \
+ /* Reflect u on plane with given normal vector pn */ \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_reflect(glm_vec3##tag u, glm_vec3##tag pn) { \
+ T dot2 = 2.0f * _glm_vec3_DOT(u, pn); \
+ u.x -= dot2 * pn.x, u.y -= dot2 * pn.y, u.z -= dot2 * pn.z; \
+ return u; \
+ } \
+ /* Refract u incident on plane with given normal vector pn */ \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_refract(glm_vec3##tag u, glm_vec3##tag pn, T eta) { \
+ T dot = _glm_vec3_DOT(pn, u); \
+ T k = 1.0f - eta * eta * (1.0f - dot * dot); \
+ if (k < 0.0f) return glm_vec3##tag##_zero; \
+ glm_vec3##tag##_scale(&u, eta); \
+ glm_vec3##tag##_scale(&pn, eta * dot + glm_sqrt_##tag(k)); \
+ return *glm_vec3##tag##_sub(&u, pn); \
+ } \
+ static inline T \
+ glm_vec3##tag##_distance(glm_vec3##tag u, glm_vec3##tag v) { \
+ u.x -= v.x, u.y -= v.y, u.z -= v.z; \
+ return glm_sqrt_##tag(_glm_vec3_DOT(u, u)); \
+ } \
+ /* Signed distance between point u and a plane (pp, pn), pn normalized. */ \
+ static inline T \
+ glm_vec3##tag##_distanceToPlane(glm_vec3##tag u, glm_vec3##tag pp, glm_vec3##tag pn) { \
+ u.x -= pp.x, u.y -= pp.y, u.z -= pp.z; \
+ return _glm_vec3_DOT(u, pn); \
+ } \
+ /* Linear interpolation */ \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_lerp(glm_vec3##tag u, glm_vec3##tag v, T t) { \
+ T s = 1.0f - t; \
+ u.x = s*u.x + t*v.x, u.y = s*u.y + t*v.y, u.z = s*u.z + t*v.z; \
+ return u; \
+ } \
+ /* Swizzle */ \
+ glm_vec3##tag##_xzy(glm_vec3##tag u) { \
+ return (glm_vec3##tag) {u.x, u.z, u.y}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_yxz(glm_vec3##tag u) { \
+ return (glm_vec3##tag) {u.y, u.x, u.z}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_yzx(glm_vec3##tag u) { \
+ return (glm_vec3##tag) {u.y, u.z, u.x}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_zxy(glm_vec3##tag u) { \
+ return (glm_vec3##tag) {u.z, u.x, u.y}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_zyx(glm_vec3##tag u) { \
+ return (glm_vec3##tag) {u.z, u.y, u.x}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec3##tag##_swizzle(glm_vec3##tag u, const char* swz) { \
+ T* a = glm_vec3_arr(u); \
+ return (glm_vec3##tag) {a[swz[0] - 'x'], a[swz[1] - 'x'], a[swz[2] - 'x']}; \
+ } \
+ static inline bool \
+ glm_vec3##tag##_rayPlaneIntersection(glm_vec3##tag* out, glm_vec3##tag u, glm_vec3##tag dir, \
+ glm_vec3##tag pp, glm_vec3##tag pn) { \
+ T d = _glm_vec3_DOT(dir, pn); if (d == 0) return false; \
+ T t = (_glm_vec3_DOT(pp, pn) - _glm_vec3_DOT(u, pn)) / d; \
+ if (t < 0) return false; \
+ u.x += dir.x*t, u.y += dir.y*t, u.z += dir.z*t; \
+ *out = u; return true; \
+ } \
+ static inline bool \
+ glm_vec3##tag##_equals(glm_vec3##tag u, glm_vec3##tag v) { \
+ if (u.x != v.x) return false; \
+ if (u.y != v.y) return false; \
+ return u.z == v.z; \
+ } \
+ static inline int \
+ glm_vec3##tag##_compare(glm_vec3##tag* u, glm_vec3##tag* v) { \
+ if (u->x != v->x) return 1 - ((u->x < v->x)<<1); \
+ if (u->y != v->y) return 1 - ((u->y < v->y)<<1); \
+ return u->z == v->z ? 0 : 1 - ((u->z < v->z)<<1); \
+ } \
+ typedef T glm_vec3##tag##_value_t
+
+#define _glm_vec3_DOT(u, v) (u.x*v.x + u.y*v.y + u.z*v.z)
+#define _glm_vec3_SUB(u, v) u.x - v.x, u.y - v.y, u.z - v.z
+#define _glm_vec3_CROSS(u, v) u.y*v.z - v.y*u.z, u.z*v.x - v.z*u.x, u.x*v.y - u.y*v.x
+
+declare_glm_vec3(d, double);
+declare_glm_vec3(f, float);
+
+static inline glm_vec3f glm_vec3d_tof(glm_vec3d v) {
+ return (glm_vec3f) {(float) v.x, (float) v.y, (float) v.z};
+}
+static inline glm_vec3d glm_vec3f_tod(glm_vec3f v) {
+ return (glm_vec3d) {v.x, v.y, v.z};
+}
+
+#endif
diff --git a/glm/vec4.h b/glm/vec4.h new file mode 100644 index 00000000..2ef0d2ea --- /dev/null +++ b/glm/vec4.h @@ -0,0 +1,190 @@ +// MIT License
+//
+// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+#ifndef GLM_VEC4__H__
+#define GLM_VEC4__H__
+
+#include "vec3.h"
+
+#ifdef GLM_NO_ANONYMOUS_STRUCT
+#define _glm_VEC4(tag, T) struct glm_vec4##tag { T x, y, z, w; }
+#else
+#define _glm_VEC4(tag, T) union glm_vec4##tag { struct { T x, y, z, w; }; T arr[4]; }
+#endif
+
+#define glm_vec4_arr(v) (&(v).x)
+#define glm_vec4f_zero ((glm_vec4f) {0.f, 0.f, 0.f, 0.f})
+#define glm_vec4d_zero ((glm_vec4d) {0.0, 0.0, 0.0, 0.0})
+
+#define declare_glm_vec4(tag, T) \
+ typedef _glm_VEC4(tag, T) glm_vec4##tag; \
+ static glm_vec4##tag glm_vec4##tag##_axis[4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; \
+ \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_init(T x, T y, T z, T w) { return (glm_vec4##tag) {x, y, z, w}; } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_fromArray(const T* a) { return (glm_vec4##tag) {a[0], a[1], a[2], a[3]}; } \
+ \
+ static inline glm_vec4##tag* \
+ glm_vec4##tag##_assign(glm_vec4##tag* self, T x, T y, T z, T w) { \
+ self->x = x, self->y = y, self->z = z, self->z = z, self->w = w; return self; \
+ } \
+ static inline glm_vec4##tag* \
+ glm_vec4##tag##_assignArray(glm_vec4##tag* self, const T* a) { \
+ self->x = a[0], self->y = a[1], self->z = a[2], self->w = a[3]; return self; \
+ } \
+ static inline glm_vec4##tag* \
+ glm_vec4##tag##_add(glm_vec4##tag* self, glm_vec4##tag v) { \
+ self->x += v.x, self->y += v.y, self->z += v.z, self->w += v.w; return self; \
+ } \
+ static inline glm_vec4##tag* \
+ glm_vec4##tag##_sub(glm_vec4##tag* self, glm_vec4##tag v) { \
+ self->x -= v.x, self->y -= v.y, self->z -= v.z, self->w -= v.w; return self; \
+ } \
+ static inline glm_vec4##tag* \
+ glm_vec4##tag##_scale(glm_vec4##tag* self, T s) { \
+ self->x *= s, self->y *= s, self->z *= s, self->w *= s; return self; \
+ } \
+ static inline T \
+ glm_vec4##tag##_length(glm_vec4##tag v) { \
+ return glm_sqrt_##tag(_glm_vec4_DOT(v, v)); \
+ } \
+ static inline T \
+ glm_vec4##tag##_length2(glm_vec4##tag v) { \
+ return _glm_vec4_DOT(v, v); \
+ } \
+ static inline T \
+ glm_vec4##tag##_distance(glm_vec4##tag u, glm_vec4##tag v) { \
+ u.x -= v.x, u.y -= v.y, u.z -= v.z, u.w -= v.w; \
+ return glm_sqrt_##tag(_glm_vec4_DOT(u, u)); \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_plus(glm_vec4##tag u, glm_vec4##tag v) { \
+ u.x += v.x, u.y += v.y, u.z += v.z, u.w += v.w; return u; \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_minus(glm_vec4##tag u, glm_vec4##tag v) { \
+ u.x -= v.x, u.y -= v.y, u.z -= v.z, u.w -= v.w; return u; \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_mult(glm_vec4##tag v, T s) { \
+ v.x *= s, v.y *= s, v.z *= s, v.w *= s; return v; \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_neg(glm_vec4##tag v) { \
+ v.x = -v.x, v.y = -v.y, v.z = -v.z, v.w = -v.w; return v; \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_unit(glm_vec4##tag v) { \
+ T s = glm_sqrt_##tag(_glm_vec4_DOT(v, v)); \
+ if (s < 1e-8) return glm_vec4##tag##_zero; \
+ s = 1.0f / s; v.x *= s, v.y *= s, v.z *= s, v.w *= s; \
+ return v; \
+ } \
+ static inline T \
+ glm_vec4##tag##_dot(glm_vec4##tag u, glm_vec4##tag v) { \
+ return _glm_vec4_DOT(u, v); \
+ } \
+ static inline glm_vec4##tag \
+ glm_vec3##tag##_xyzw(glm_vec3##tag u, T w) { \
+ return (glm_vec4##tag) {u.x, u.y, u.z, w}; \
+ } \
+ /* Swizzle */ \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_xyz(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.x, u.y, u.z}; \
+ }; \
+ glm_vec4##tag##_xzy(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.x, u.z, u.y}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_yxz(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.y, u.x, u.z}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_yzx(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.y, u.z, u.x}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_zxy(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.z, u.x, u.y}; \
+ }; \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_zyx(glm_vec4##tag u) { \
+ return (glm_vec3##tag) {u.z, u.y, u.x}; \
+ }; \
+ \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_swizzle(glm_vec4##tag u, const char* swz) { \
+ const T* a = glm_vec4_arr(u); \
+ return (glm_vec4##tag) {a[(swz[0] - 'x') & 3], \
+ a[(swz[1] - 'x') & 3], \
+ a[(swz[2] - 'x') & 3], \
+ a[(swz[3] - 'x') & 3]}; \
+ } \
+ static inline glm_vec3##tag \
+ glm_vec4##tag##_swizzle3(glm_vec4##tag u, const char* swz) { \
+ const T* a = glm_vec4_arr(u); \
+ return (glm_vec3##tag) {a[(swz[0] - 'x') & 3], \
+ a[(swz[1] - 'x') & 3], \
+ a[(swz[2] - 'x') & 3]}; \
+ } \
+ /* Linear interpolation */ \
+ static inline glm_vec4##tag \
+ glm_vec4##tag##_lerp(glm_vec4##tag u, glm_vec4##tag v, T t) { \
+ T s = 1.0f - t; \
+ u.x = s*u.x + t*v.x, u.y = s*u.y + t*v.y, \
+ u.z = s*u.z + t*v.z, u.w = s*u.w + t*v.w; \
+ return u; \
+ } \
+ static inline bool \
+ glm_vec4##tag##_equals(glm_vec4##tag u, glm_vec4##tag v) { \
+ if (u.x != v.x) return false; \
+ if (u.y != v.y) return false; \
+ if (u.z != v.z) return false; \
+ return u.w == v.w; \
+ } \
+ static inline int \
+ glm_vec4##tag##_compare(glm_vec4##tag* u, glm_vec4##tag* v) { \
+ if (u->x != v->x) return 1 - ((u->x < v->x)<<1); \
+ if (u->y != v->y) return 1 - ((u->y < v->y)<<1); \
+ if (u->z != v->z) return 1 - ((u->z < v->z)<<1); \
+ return u->w == v->w ? 0 : 1 - ((u->w < v->w)<<1); \
+ } \
+ typedef T glm_vec4##tag##_value_t
+
+#define _glm_vec4_DOT(u, v) (u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w)
+#define _glm_vec4_SUB(u, v) u.x - v.x, u.y - v.y, u.z - v.z, u.w - v.w
+
+declare_glm_vec4(d, double);
+declare_glm_vec4(f, float);
+
+static inline glm_vec4d
+glm_vec4f_tod(glm_vec4f v) {
+ return (glm_vec4d) {v.x, v.y, v.z, v.w};
+}
+static inline glm_vec4f
+glm_vec4d_tof(glm_vec4d v) {
+ return (glm_vec4f) {(float) v.x, (float) v.y, (float) v.z, (float) v.w};
+}
+
+#endif
|
