From f848e4451afe6666c83b9488e4bc70d9c950c8f0 Mon Sep 17 00:00:00 2001 From: acoto87 Date: Thu, 28 Mar 2019 20:56:37 -0600 Subject: [PATCH 01/11] Include `stddef.h` to ensure `size_t` and other dependent types --- include/cglm/common.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/cglm/common.h b/include/cglm/common.h index 422716a..42d5e67 100644 --- a/include/cglm/common.h +++ b/include/cglm/common.h @@ -11,6 +11,7 @@ #define _USE_MATH_DEFINES /* for windows */ #include +#include #include #include From 674e05213a58e3289319616ed38fab4be4adc64a Mon Sep 17 00:00:00 2001 From: acoto87 Date: Thu, 28 Mar 2019 23:41:19 -0600 Subject: [PATCH 02/11] - Add struct types to types.h. - Add structs/vec4.h file. --- include/cglm/structs/vec4.h | 282 ++++++++++++++++++++++++++++++++++++ include/cglm/types.h | 46 ++++++ 2 files changed, 328 insertions(+) create mode 100644 include/cglm/structs/vec4.h diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h new file mode 100644 index 0000000..7290c35 --- /dev/null +++ b/include/cglm/structs/vec4.h @@ -0,0 +1,282 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_vec4s_h +#define cglm_vec4s_h + +#include "common.h" +#include "vec4-ext.h" +#include "util.h" + +#define GLMS_VEC4_ONE_INIT {1.0f, 1.0f, 1.0f, 1.0f} +#define GLMS_VEC4_BLACK_INIT {0.0f, 0.0f, 0.0f, 1.0f} +#define GLMS_VEC4_ZERO_INIT {0.0f, 0.0f, 0.0f, 0.0f} + +#define GLMS_VEC4_ONE ((vec4s)GLM_VEC4_ONE_INIT) +#define GLMS_VEC4_BLACK ((vec4s)GLM_VEC4_BLACK_INIT) +#define GLMS_VEC4_ZERO ((vec4s)GLM_VEC4_ZERO_INIT) + +CGLM_INLINE +vec4s +glms_vec4(vec3s v3, float last) +{ + vec4s r; + glm_vec4(v3.raw, last, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec4_copy3(vec4s v) +{ + vec3s r; + glm_vec4_copy3(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_copy(vec4s v) +{ + vec4s r; + glm_vec4_copy(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_ucopy(vec4s v) +{ + vec4s r; + glm_vec4_ucopy(v.raw, r.raw); + return r; +} + + +CGLM_INLINE +float +glms_vec4_dot(vec4s a, vec4s b) +{ + return glm_vec4_dot(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec4_norm2(vec4s v) +{ + return glm_vec4_norm2(v.raw); +} + +CGLM_INLINE +float +glms_vec4_norm(vec4s v) +{ + return glm_vec4_norm(v.raw); +} + +CGLM_INLINE +vec4s +glms_vec4_add(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_add(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_adds(vec4s v, float s) +{ + vec4s r; + glm_vec4_adds(v.raw s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_sub(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_sub(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_subs(vec4s v, float s) +{ + vec4s r; + glm_vec4_subs(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_mul(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_mul(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_scale(vec4s v, float s) +{ + vec4s r; + glm_vec4_scale(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_scale_as(vec4s v, float s) +{ + vec4s r; + glm_vec4_scale_as(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_div(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_div(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_divs(vec4s v, float s) +{ + vec4s r; + glm_vec4_divs(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_addadd(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_addadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_subadd(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_subadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_muladd(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_muladd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_muladds(vec4s a, float s) +{ + vec4s r; + glm_vec4_muladds(a.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_maxadd(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_maxadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_minadd(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_minadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_negate(vec4s v) +{ + glm_vec4_negate(v.raw); + return v; +} + +CGLM_INLINE +vec4s +glms_vec4_inv(vec4s v) +{ + glm_vec4_inv(v.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_normalize(vec4s v) +{ + glm_vec4_normalize(v.raw); + return v; +} + +CGLM_INLINE +float +glms_vec4_distance(vec4s a, vec4s b) +{ + return glm_vec4_distance(a.raw, b.raw); +} + +CGLM_INLINE +vec4s +glms_vec4_maxv(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_maxv(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_minv(vec4s a, vec4s b) +{ + vec4s r; + glm_vec4_minv(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_clamp(vec4s v, float minVal, float maxVal) +{ + glm_vec4_clamp(v.raw, minVal, maxVal); + return v; +} + +CGLM_INLINE +vec4s +glms_vec4_lerp(vec4s from, vec4s to, float t) +{ + vec4s r; + glm_vec4_lerp(from.raw, to.raw, t, r.raw); + return r; +} + +#endif /* cglm_vec4s_h */ diff --git a/include/cglm/types.h b/include/cglm/types.h index 1190b73..29703df 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -32,6 +32,7 @@ # define CGLM_ALIGN_MAT CGLM_ALIGN(16) #endif +// array types typedef float vec2[2]; typedef float vec3[3]; typedef int ivec3[3]; @@ -45,6 +46,51 @@ typedef CGLM_ALIGN_IF(32) vec4 mat4[4]; typedef CGLM_ALIGN_IF(16) vec4 mat4[4]; #endif +// struct types +typedef union CGLM_ALIGN_IF(8) vec2s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float x; + float y; + }; +#endif + vec2 raw; +} vec2s; + +typedef union CGLM_ALIGN_IF(8) vec3s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float x; + float y; + float z; + }; +#endif + vec3 raw; +} vec3s; + +typedef union CGLM_ALIGN_IF(8) ivec3s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + int x; + int y; + int z; + }; +#endif + ivec3 raw; +} ivec3s; + +typedef union CGLM_ALIGN_IF(16) vec4s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float x; + float y; + float z; + float w; + }; +#endif + vec4 raw; +} vec4s; + #define GLM_E 2.71828182845904523536028747135266250 /* e */ #define GLM_LOG2E 1.44269504088896340735992468100189214 /* log2(e) */ #define GLM_LOG10E 0.434294481903251827651128918916605082 /* log10(e) */ From c25469829a000a397fc164d93896a5505899e859 Mon Sep 17 00:00:00 2001 From: acoto87 Date: Wed, 3 Apr 2019 22:25:49 -0600 Subject: [PATCH 03/11] Initial implementation of struct type `vec4s` --- include/cglm/cglms.h | 20 ++++++ include/cglm/structs/vec4-ext.h | 114 ++++++++++++++++++++++++++++++++ include/cglm/structs/vec4.h | 15 +++-- include/cglm/types.h | 23 +------ test/src/test_common.c | 14 ++++ test/src/test_common.h | 9 ++- test/src/test_quat.c | 2 + test/src/test_vec4.c | 10 ++- 8 files changed, 178 insertions(+), 29 deletions(-) create mode 100644 include/cglm/cglms.h create mode 100644 include/cglm/structs/vec4-ext.h diff --git a/include/cglm/cglms.h b/include/cglm/cglms.h new file mode 100644 index 0000000..4dd471f --- /dev/null +++ b/include/cglm/cglms.h @@ -0,0 +1,20 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_structs_h +#define cglm_structs_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "cglm.h" +#include "structs/vec4.h" + +#ifdef __cplusplus +} +#endif +#endif /* cglm_structs_h */ diff --git a/include/cglm/structs/vec4-ext.h b/include/cglm/structs/vec4-ext.h new file mode 100644 index 0000000..ea84e82 --- /dev/null +++ b/include/cglm/structs/vec4-ext.h @@ -0,0 +1,114 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_vec4s_ext_h +#define cglm_vec4s_ext_h + +#include "common.h" +#include "vec3-ext.h" +#include +#include +#include + +CGLM_INLINE +vec4s +glms_vec4_broadcast(float val, vec4 d) +{ + vec4s r; + glm_vec4_broadcast(val, r.raw); + return r; +} + +CGLM_INLINE +bool +glms_vec4_eq(vec4s v, float val) +{ + return glm_vec4_eq(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec4_eq_eps(vec4 v, float val) +{ + return glm_vec4_eq_eps(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec4_eq_all(vec4s v) +{ + return glm_vec4_eq_all(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_eqv(vec4s a, vec4s b) +{ + return glm_vec4_eqv(a.raw, b.raw); +} + +CGLM_INLINE +bool +glms_vec4_eqv_eps(vec4s a, vec4s b) +{ + return glm_vec4_eqv_eps(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec4_max(vec4s v) +{ + return glm_vec4_max(v.raw); +} + +CGLM_INLINE +float +glms_vec4_min(vec4s v) +{ + return glm_vec4_min(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isnan(vec4s v) +{ + return glm_vec4_isnan(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isinf(vec4s v) +{ + return glm_vec4_isinf(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isvalid(vec4s v) +{ + return glm_vec4_isvalid(v.raw); +} + +CGLM_INLINE +vec4s +glms_vec4_sign(vec4s v) +{ + vec4s r; + glm_vec4_sign(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_sqrt(vec4s v) +{ + vec4s r; + glm_vec4_sqrt(v.raw, r.raw); + return r; +} + +#endif /* cglm_vec4s_ext_h */ diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index 7290c35..b68bb33 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -8,9 +8,11 @@ #ifndef cglm_vec4s_h #define cglm_vec4s_h -#include "common.h" -#include "vec4-ext.h" -#include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" #define GLMS_VEC4_ONE_INIT {1.0f, 1.0f, 1.0f, 1.0f} #define GLMS_VEC4_BLACK_INIT {0.0f, 0.0f, 0.0f, 1.0f} @@ -92,7 +94,7 @@ vec4s glms_vec4_adds(vec4s v, float s) { vec4s r; - glm_vec4_adds(v.raw s, r.raw); + glm_vec4_adds(v.raw, s, r.raw); return r; } @@ -226,7 +228,7 @@ vec4s glms_vec4_inv(vec4s v) { glm_vec4_inv(v.raw); - return r; + return v; } CGLM_INLINE @@ -279,4 +281,7 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) return r; } +#ifdef __cplusplus +} +#endif #endif /* cglm_vec4s_h */ diff --git a/include/cglm/types.h b/include/cglm/types.h index 29703df..69c2885 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -47,17 +47,7 @@ typedef CGLM_ALIGN_IF(16) vec4 mat4[4]; #endif // struct types -typedef union CGLM_ALIGN_IF(8) vec2s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - float x; - float y; - }; -#endif - vec2 raw; -} vec2s; - -typedef union CGLM_ALIGN_IF(8) vec3s { +typedef union CGLM_ALIGN_IF(16) vec3s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { float x; @@ -68,17 +58,6 @@ typedef union CGLM_ALIGN_IF(8) vec3s { vec3 raw; } vec3s; -typedef union CGLM_ALIGN_IF(8) ivec3s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - int x; - int y; - int z; - }; -#endif - ivec3 raw; -} ivec3s; - typedef union CGLM_ALIGN_IF(16) vec4s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { diff --git a/test/src/test_common.c b/test/src/test_common.c index d41d3cb..94331f4 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -57,6 +57,14 @@ test_rand_vec4(vec4 dest) { dest[3] = drand48(); } +vec4s +test_rand_vec4s() +{ + vec4s r; + test_rand_vec4(r.raw); + return r; +} + float test_rand(void) { srand((unsigned int)time(NULL)); @@ -127,6 +135,12 @@ test_assert_vec4_eq(vec4 v1, vec4 v2) { assert_true(fabsf(v1[3] - v2[3]) <= 0.000009); } +void +test_assert_vec4s_eq(vec4s v1, vec4s v2) +{ + test_assert_vec4_eq(v1.raw, v2.raw); +} + void test_assert_quat_eq_abs(versor v1, versor v2) { assert_true(fabsf(fabsf(v1[0]) - fabsf(v2[0])) <= 0.0009); /* rounding errors */ diff --git a/test/src/test_common.h b/test/src/test_common.h index 8a16b0f..c6a2dc4 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -20,6 +20,7 @@ #include #include +#include #include void @@ -46,6 +47,9 @@ test_assert_vec3_eq(vec3 v1, vec3 v2); void test_assert_vec4_eq(vec4 v1, vec4 v2); +void +test_assert_vec4s_eq(vec4s v1, vec4s v2); + void test_assert_quat_eq(versor v1, versor v2); @@ -56,7 +60,10 @@ void test_rand_vec3(vec3 dest); void -test_rand_vec4(vec4 dest) ; +test_rand_vec4(vec4 dest); + +vec4s +test_rand_vec4s(); float test_rand(void); diff --git a/test/src/test_quat.c b/test/src/test_quat.c index 0d64367..d0cc27c 100644 --- a/test/src/test_quat.c +++ b/test/src/test_quat.c @@ -44,9 +44,11 @@ test_quat(void **state) { test_assert_mat4_eq2(inRot, outRot, 0.000009); /* almost equal */ /* 4. test SSE mul and raw mul */ +#if defined( __SSE__ ) || defined( __SSE2__ ) test_quat_mul_raw(inQuat, outQuat, q3); glm_quat_mul_sse2(inQuat, outQuat, q4); test_assert_quat_eq(q3, q4); +#endif } /* 5. test lookat */ diff --git a/test/src/test_vec4.c b/test/src/test_vec4.c index 8e4fda5..5fe3be8 100644 --- a/test/src/test_vec4.c +++ b/test/src/test_vec4.c @@ -65,10 +65,10 @@ test_vec4_clamp(vec4 v, float minVal, float maxVal) { void test_vec4(void **state) { vec4 v, v1, v2, v3, v4; + vec4s vs1, vs2, vs3, vs4; int i; float d1, d2; - for (i = 0; i < 1000; i++) { /* 1. test SSE/SIMD dot product */ test_rand_vec4(v); @@ -175,4 +175,12 @@ test_vec4(void **state) { assert_true(v3[1] >= 0.0999 && v3[1] <= 0.80001); assert_true(v3[2] >= 0.0999 && v3[2] <= 0.80001); assert_true(v3[3] >= 0.0999 && v3[3] <= 0.80001); + + /* structs */ + vs1 = test_rand_vec4s(); + vs2 = test_rand_vec4s(); + + vs3 = glms_vec4_add(vs1, vs2); + vs4 = glms_vec4_maxv(vs1, vs3); + test_assert_vec4s_eq(vs3, vs4); } From 1aa54dc1109ea663b95984417baa0f4a3b851c9d Mon Sep 17 00:00:00 2001 From: acoto87 Date: Mon, 8 Apr 2019 21:06:01 -0500 Subject: [PATCH 04/11] - separating struct types into `types-struct.h` - vec3s implementation - fix style issues --- include/cglm/cglms.h | 2 + include/cglm/structs/vec3-ext.h | 100 ++++++++ include/cglm/structs/vec3.h | 398 ++++++++++++++++++++++++++++++++ include/cglm/structs/vec4-ext.h | 48 ++-- include/cglm/structs/vec4.h | 96 +++----- include/cglm/types-struct.h | 36 +++ include/cglm/types.h | 25 -- test/src/test_common.c | 14 ++ test/src/test_common.h | 6 + test/src/test_vec3.c | 9 + 10 files changed, 617 insertions(+), 117 deletions(-) create mode 100644 include/cglm/structs/vec3-ext.h create mode 100644 include/cglm/structs/vec3.h create mode 100644 include/cglm/types-struct.h diff --git a/include/cglm/cglms.h b/include/cglm/cglms.h index 4dd471f..9f00dc8 100644 --- a/include/cglm/cglms.h +++ b/include/cglm/cglms.h @@ -12,6 +12,8 @@ extern "C" { #endif #include "cglm.h" +#include "types-struct.h" +#include "structs/vec3.h" #include "structs/vec4.h" #ifdef __cplusplus diff --git a/include/cglm/structs/vec3-ext.h b/include/cglm/structs/vec3-ext.h new file mode 100644 index 0000000..0449b3d --- /dev/null +++ b/include/cglm/structs/vec3-ext.h @@ -0,0 +1,100 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_vec3s_ext_h +#define cglm_vec3s_ext_h + +#include "../common.h" +#include "../types-struct.h" +#include "../util.h" +#include "../vec3-ext.h" + +CGLM_INLINE +vec3s +glms_vec3_broadcast(float val) { + vec3s r; + glm_vec3_broadcast(val, r.raw); + return r; +} + +CGLM_INLINE +bool +glms_vec3_eq(vec3s v, float val) { + return glm_vec3_eq(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec3_eq_eps(vec3s v, float val) { + return glm_vec3_eq_eps(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec3_eq_all(vec3s v) { + return glm_vec3_eq_all(v.raw); +} + +CGLM_INLINE +bool +glms_vec3_eqv(vec3s a, vec3s b) { + return glm_vec3_eqv(a.raw, b.raw); +} + +CGLM_INLINE +bool +glms_vec3_eqv_eps(vec3s a, vec3s b) { + return glm_vec3_eqv_eps(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec3_max(vec3s v) { + return glm_vec3_max(v.raw); +} + +CGLM_INLINE +float +glms_vec3_min(vec3s v) { + return glm_vec3_min(v.raw); +} + +CGLM_INLINE +bool +glms_vec3_isnan(vec3s v) { + return glm_vec3_isnan(v.raw); +} + +CGLM_INLINE +bool +glms_vec3_isinf(vec3s v) { + return glm_vec3_isinf(v.raw); +} + +CGLM_INLINE +bool +glms_vec3_isvalid(vec3s v) { + return glm_vec3_isvalid(v.raw); +} + +CGLM_INLINE +vec3s +glms_vec3_sign(vec3s v) { + vec3s r; + glm_vec3_sign(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_sqrt(vec3s v) { + vec3s r; + glm_vec3_sqrt(v.raw, r.raw); + return r; +} + +#endif /* cglm_vec3s_ext_h */ diff --git a/include/cglm/structs/vec3.h b/include/cglm/structs/vec3.h new file mode 100644 index 0000000..ed77c4a --- /dev/null +++ b/include/cglm/structs/vec3.h @@ -0,0 +1,398 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_vec3s_h +#define cglm_vec3s_h + +#include "../common.h" +#include "../types-struct.h" +#include "../util.h" +#include "../vec3.h" +#include "vec3-ext.h" + +#define GLMS_VEC3_ONE_INIT {1.0f, 1.0f, 1.0f} +#define GLMS_VEC3_ZERO_INIT {0.0f, 0.0f, 0.0f} + +#define GLMS_VEC3_ONE ((vec3s)GLMS_VEC3_ONE_INIT) +#define GLMS_VEC3_ZERO ((vec3s)GLMS_VEC3_ZERO_INIT) + +#define GLMS_YUP ((vec3s){0.0f, 1.0f, 0.0f}) +#define GLMS_ZUP ((vec3s){0.0f, 0.0f, 1.0f}) +#define GLMS_XUP ((vec3s){1.0f, 0.0f, 0.0f}) + +CGLM_INLINE +vec3s +glms_vec3(vec4s v4) { + vec3s r; + glm_vec3(v4.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_copy(vec3s v) { + vec3s r; + glm_vec3_copy(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_zero() { + vec3s r; + glm_vec3_zero(r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_one() { + vec3s r; + glm_vec3_one(r.raw); + return r; +} + +CGLM_INLINE +float +glms_vec3_dot(vec3s a, vec3s b) { + return glm_vec3_dot(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec3_norm2(vec3s v) { + glm_vec3_norm2(v.raw); +} + +CGLM_INLINE +float +glms_vec3_norm(vec3s v) { + glm_vec3_norm(v.raw); +} + +CGLM_INLINE +vec3s +glms_vec3_add(vec3s a, vec3s b) { + vec3s r; + glm_vec3_add(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_adds(vec3s a, float s) { + vec3s r; + glm_vec3_adds(a.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_sub(vec3s a, vec3s b) { + vec3s r; + glm_vec3_sub(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_subs(vec3s a, float s) { + vec3s r; + glm_vec3_subs(a.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_mul(vec3s a, vec3s b) { + vec3s r; + glm_vec3_mul(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_scale(vec3s v, float s) { + vec3s r; + glm_vec3_scale(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_scale_as(vec3s v, float s) { + vec3s r; + glm_vec3_scale_as(v.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_div(vec3s a, vec3s b) { + vec3s r; + glm_vec3_div(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_divs(vec3s a, float s) { + vec3s r; + glm_vec3_divs(a.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_addadd(vec3s a, vec3s b) { + vec3s r; + glm_vec3_addadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_subadd(vec3s a, vec3s b) { + vec3s r; + glm_vec3_subadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_muladd(vec3s a, vec3s b) { + vec3s r; + glm_vec3_muladd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_muladds(vec3s a, float s) { + vec3s r; + glm_vec3_muladds(a.raw, s, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_maxadd(vec3s a, vec3s b) { + vec3s r; + glm_vec3_maxadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_minadd(vec3s a, vec3s b) { + vec3s r; + glm_vec3_minadd(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_flipsign(vec3s v) { + glm_vec3_flipsign(v.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_flipsign_to(vec3s v) { + vec3s r; + glm_vec3_flipsign_to(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_negate_to(vec3s v) { + vec3s r; + glm_vec3_negate_to(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_negate(vec3s v) { + glm_vec3_negate(v.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_inv(vec3s v) { + glm_vec3_inv(v.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_inv_to(vec3s v) { + vec3s r; + glm_vec3_inv_to(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_normalize(vec3s v) { + glm_vec3_normalize(v.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_normalize_to(vec3s v) { + vec3s r; + glm_vec3_normalize_to(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_cross(vec3s a, vec3s b) { + vec3s r; + glm_vec3_cross(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_crossn(vec3s a, vec3s b) { + vec3s r; + glm_vec3_crossn(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +float +glms_vec3_distance(vec3s a, vec3s b) { + return glm_vec3_distance(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec3_angle(vec3s a, vec3s b) { + return glm_vec3_angle(a.raw, b.raw); +} + +CGLM_INLINE +vec3s +glms_vec3_rotate(vec3s v, float angle, vec3s axis) { + glm_vec3_rotate(v.raw, angle, axis.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_rotate_m4(mat4 m, vec3s v) { + vec3s r; + glm_vec3_rotate_m4(m, v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_rotate_m3(mat3 m, vec3s v) { + vec3s r; + glm_vec3_rotate_m3(m, v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_proj(vec3s a, vec3s b) { + vec3s r; + glm_vec3_proj(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_center(vec3s a, vec3s b) { + vec3s r; + glm_vec3_center(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +float +glms_vec3_distance2(vec3s a, vec3s b) { + return glm_vec3_distance2(a.raw, b.raw); +} + +CGLM_INLINE +vec3s +glms_vec3_maxv(vec3s a, vec3s b) { + vec3s r; + glm_vec3_maxv(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_minv(vec3s a, vec3s b) { + vec3s r; + glm_vec3_minv(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_ortho(vec3s v) { + vec3s r; + glm_vec3_ortho(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_vec3_clamp(vec3s v, float minVal, float maxVal) { + glm_vec3_clamp(v.raw, minVal, maxVal); + return v; +} + +CGLM_INLINE +vec3s +glms_vec3_lerp(vec3s from, vec3s to, float t) { + vec3s r; + glm_vec3_lerp(from.raw, to.raw, t, r.raw); + return r; +} + +CGLM_INLINE +vec3s +glms_cross(vec3s a, vec3s b) { + vec3s r; + glm_cross(a.raw, b.raw, r.raw); + return r; +} + +CGLM_INLINE +float +glms_dot(vec3s a, vec3s b) { + return glm_dot(a.raw, b.raw); +} + +CGLM_INLINE +vec3s +glms_normalize(vec3s v) { + glm_normalize(v.raw); + return v; +} + +CGLM_INLINE +vec3s +glms_normalize_to(vec3s v) { + vec3s r; + glm_normalize_to(v.raw, r.raw); + return r; +} + +#endif /* cglm_vec3s_h */ diff --git a/include/cglm/structs/vec4-ext.h b/include/cglm/structs/vec4-ext.h index ea84e82..4fbb2d3 100644 --- a/include/cglm/structs/vec4-ext.h +++ b/include/cglm/structs/vec4-ext.h @@ -8,16 +8,14 @@ #ifndef cglm_vec4s_ext_h #define cglm_vec4s_ext_h -#include "common.h" -#include "vec3-ext.h" -#include -#include -#include +#include "../common.h" +#include "../types-struct.h" +#include "../util.h" +#include "../vec4-ext.h" CGLM_INLINE vec4s -glms_vec4_broadcast(float val, vec4 d) -{ +glms_vec4_broadcast(float val) { vec4s r; glm_vec4_broadcast(val, r.raw); return r; @@ -25,78 +23,67 @@ glms_vec4_broadcast(float val, vec4 d) CGLM_INLINE bool -glms_vec4_eq(vec4s v, float val) -{ +glms_vec4_eq(vec4s v, float val) { return glm_vec4_eq(v.raw, val); } CGLM_INLINE bool -glms_vec4_eq_eps(vec4 v, float val) -{ +glms_vec4_eq_eps(vec4s v, float val) { return glm_vec4_eq_eps(v.raw, val); } CGLM_INLINE bool -glms_vec4_eq_all(vec4s v) -{ +glms_vec4_eq_all(vec4s v) { return glm_vec4_eq_all(v.raw); } CGLM_INLINE bool -glms_vec4_eqv(vec4s a, vec4s b) -{ +glms_vec4_eqv(vec4s a, vec4s b) { return glm_vec4_eqv(a.raw, b.raw); } CGLM_INLINE bool -glms_vec4_eqv_eps(vec4s a, vec4s b) -{ +glms_vec4_eqv_eps(vec4s a, vec4s b) { return glm_vec4_eqv_eps(a.raw, b.raw); } CGLM_INLINE float -glms_vec4_max(vec4s v) -{ +glms_vec4_max(vec4s v) { return glm_vec4_max(v.raw); } CGLM_INLINE float -glms_vec4_min(vec4s v) -{ +glms_vec4_min(vec4s v) { return glm_vec4_min(v.raw); } CGLM_INLINE bool -glms_vec4_isnan(vec4s v) -{ +glms_vec4_isnan(vec4s v) { return glm_vec4_isnan(v.raw); } CGLM_INLINE bool -glms_vec4_isinf(vec4s v) -{ +glms_vec4_isinf(vec4s v) { return glm_vec4_isinf(v.raw); } CGLM_INLINE bool -glms_vec4_isvalid(vec4s v) -{ +glms_vec4_isvalid(vec4s v) { return glm_vec4_isvalid(v.raw); } CGLM_INLINE vec4s -glms_vec4_sign(vec4s v) -{ +glms_vec4_sign(vec4s v) { vec4s r; glm_vec4_sign(v.raw, r.raw); return r; @@ -104,8 +91,7 @@ glms_vec4_sign(vec4s v) CGLM_INLINE vec4s -glms_vec4_sqrt(vec4s v) -{ +glms_vec4_sqrt(vec4s v) { vec4s r; glm_vec4_sqrt(v.raw, r.raw); return r; diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index b68bb33..c0a79c3 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -12,7 +12,11 @@ extern "C" { #endif -#include "../cglm.h" +#include "../common.h" +#include "../types-struct.h" +#include "../util.h" +#include "../vec4.h" +#include "vec4-ext.h" #define GLMS_VEC4_ONE_INIT {1.0f, 1.0f, 1.0f, 1.0f} #define GLMS_VEC4_BLACK_INIT {0.0f, 0.0f, 0.0f, 1.0f} @@ -24,8 +28,7 @@ extern "C" { CGLM_INLINE vec4s -glms_vec4(vec3s v3, float last) -{ +glms_vec4(vec3s v3, float last) { vec4s r; glm_vec4(v3.raw, last, r.raw); return r; @@ -33,8 +36,7 @@ glms_vec4(vec3s v3, float last) CGLM_INLINE vec3s -glms_vec4_copy3(vec4s v) -{ +glms_vec4_copy3(vec4s v) { vec3s r; glm_vec4_copy3(v.raw, r.raw); return r; @@ -42,8 +44,7 @@ glms_vec4_copy3(vec4s v) CGLM_INLINE vec4s -glms_vec4_copy(vec4s v) -{ +glms_vec4_copy(vec4s v) { vec4s r; glm_vec4_copy(v.raw, r.raw); return r; @@ -51,8 +52,7 @@ glms_vec4_copy(vec4s v) CGLM_INLINE vec4s -glms_vec4_ucopy(vec4s v) -{ +glms_vec4_ucopy(vec4s v) { vec4s r; glm_vec4_ucopy(v.raw, r.raw); return r; @@ -61,29 +61,25 @@ glms_vec4_ucopy(vec4s v) CGLM_INLINE float -glms_vec4_dot(vec4s a, vec4s b) -{ +glms_vec4_dot(vec4s a, vec4s b) { return glm_vec4_dot(a.raw, b.raw); } CGLM_INLINE float -glms_vec4_norm2(vec4s v) -{ +glms_vec4_norm2(vec4s v) { return glm_vec4_norm2(v.raw); } CGLM_INLINE float -glms_vec4_norm(vec4s v) -{ +glms_vec4_norm(vec4s v) { return glm_vec4_norm(v.raw); } CGLM_INLINE vec4s -glms_vec4_add(vec4s a, vec4s b) -{ +glms_vec4_add(vec4s a, vec4s b) { vec4s r; glm_vec4_add(a.raw, b.raw, r.raw); return r; @@ -91,8 +87,7 @@ glms_vec4_add(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_adds(vec4s v, float s) -{ +glms_vec4_adds(vec4s v, float s) { vec4s r; glm_vec4_adds(v.raw, s, r.raw); return r; @@ -100,8 +95,7 @@ glms_vec4_adds(vec4s v, float s) CGLM_INLINE vec4s -glms_vec4_sub(vec4s a, vec4s b) -{ +glms_vec4_sub(vec4s a, vec4s b) { vec4s r; glm_vec4_sub(a.raw, b.raw, r.raw); return r; @@ -109,8 +103,7 @@ glms_vec4_sub(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_subs(vec4s v, float s) -{ +glms_vec4_subs(vec4s v, float s) { vec4s r; glm_vec4_subs(v.raw, s, r.raw); return r; @@ -118,8 +111,7 @@ glms_vec4_subs(vec4s v, float s) CGLM_INLINE vec4s -glms_vec4_mul(vec4s a, vec4s b) -{ +glms_vec4_mul(vec4s a, vec4s b) { vec4s r; glm_vec4_mul(a.raw, b.raw, r.raw); return r; @@ -127,8 +119,7 @@ glms_vec4_mul(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_scale(vec4s v, float s) -{ +glms_vec4_scale(vec4s v, float s) { vec4s r; glm_vec4_scale(v.raw, s, r.raw); return r; @@ -136,8 +127,7 @@ glms_vec4_scale(vec4s v, float s) CGLM_INLINE vec4s -glms_vec4_scale_as(vec4s v, float s) -{ +glms_vec4_scale_as(vec4s v, float s) { vec4s r; glm_vec4_scale_as(v.raw, s, r.raw); return r; @@ -145,8 +135,7 @@ glms_vec4_scale_as(vec4s v, float s) CGLM_INLINE vec4s -glms_vec4_div(vec4s a, vec4s b) -{ +glms_vec4_div(vec4s a, vec4s b) { vec4s r; glm_vec4_div(a.raw, b.raw, r.raw); return r; @@ -154,8 +143,7 @@ glms_vec4_div(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_divs(vec4s v, float s) -{ +glms_vec4_divs(vec4s v, float s) { vec4s r; glm_vec4_divs(v.raw, s, r.raw); return r; @@ -163,8 +151,7 @@ glms_vec4_divs(vec4s v, float s) CGLM_INLINE vec4s -glms_vec4_addadd(vec4s a, vec4s b) -{ +glms_vec4_addadd(vec4s a, vec4s b) { vec4s r; glm_vec4_addadd(a.raw, b.raw, r.raw); return r; @@ -172,8 +159,7 @@ glms_vec4_addadd(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_subadd(vec4s a, vec4s b) -{ +glms_vec4_subadd(vec4s a, vec4s b) { vec4s r; glm_vec4_subadd(a.raw, b.raw, r.raw); return r; @@ -181,8 +167,7 @@ glms_vec4_subadd(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_muladd(vec4s a, vec4s b) -{ +glms_vec4_muladd(vec4s a, vec4s b) { vec4s r; glm_vec4_muladd(a.raw, b.raw, r.raw); return r; @@ -190,8 +175,7 @@ glms_vec4_muladd(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_muladds(vec4s a, float s) -{ +glms_vec4_muladds(vec4s a, float s) { vec4s r; glm_vec4_muladds(a.raw, s, r.raw); return r; @@ -199,8 +183,7 @@ glms_vec4_muladds(vec4s a, float s) CGLM_INLINE vec4s -glms_vec4_maxadd(vec4s a, vec4s b) -{ +glms_vec4_maxadd(vec4s a, vec4s b) { vec4s r; glm_vec4_maxadd(a.raw, b.raw, r.raw); return r; @@ -208,8 +191,7 @@ glms_vec4_maxadd(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_minadd(vec4s a, vec4s b) -{ +glms_vec4_minadd(vec4s a, vec4s b) { vec4s r; glm_vec4_minadd(a.raw, b.raw, r.raw); return r; @@ -217,39 +199,34 @@ glms_vec4_minadd(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_negate(vec4s v) -{ +glms_vec4_negate(vec4s v) { glm_vec4_negate(v.raw); return v; } CGLM_INLINE vec4s -glms_vec4_inv(vec4s v) -{ +glms_vec4_inv(vec4s v) { glm_vec4_inv(v.raw); return v; } CGLM_INLINE vec4s -glms_vec4_normalize(vec4s v) -{ +glms_vec4_normalize(vec4s v) { glm_vec4_normalize(v.raw); return v; } CGLM_INLINE float -glms_vec4_distance(vec4s a, vec4s b) -{ +glms_vec4_distance(vec4s a, vec4s b) { return glm_vec4_distance(a.raw, b.raw); } CGLM_INLINE vec4s -glms_vec4_maxv(vec4s a, vec4s b) -{ +glms_vec4_maxv(vec4s a, vec4s b) { vec4s r; glm_vec4_maxv(a.raw, b.raw, r.raw); return r; @@ -257,8 +234,7 @@ glms_vec4_maxv(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_minv(vec4s a, vec4s b) -{ +glms_vec4_minv(vec4s a, vec4s b) { vec4s r; glm_vec4_minv(a.raw, b.raw, r.raw); return r; @@ -266,16 +242,14 @@ glms_vec4_minv(vec4s a, vec4s b) CGLM_INLINE vec4s -glms_vec4_clamp(vec4s v, float minVal, float maxVal) -{ +glms_vec4_clamp(vec4s v, float minVal, float maxVal) { glm_vec4_clamp(v.raw, minVal, maxVal); return v; } CGLM_INLINE vec4s -glms_vec4_lerp(vec4s from, vec4s to, float t) -{ +glms_vec4_lerp(vec4s from, vec4s to, float t) { vec4s r; glm_vec4_lerp(from.raw, to.raw, t, r.raw); return r; diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h new file mode 100644 index 0000000..27e04fe --- /dev/null +++ b/include/cglm/types-struct.h @@ -0,0 +1,36 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_types_struct_h +#define cglm_types_struct_h + +#include "types.h" + +typedef union CGLM_ALIGN_IF(16) vec3s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float x; + float y; + float z; + }; +#endif + vec3 raw; +} vec3s; + +typedef union CGLM_ALIGN_IF(16) vec4s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float x; + float y; + float z; + float w; + }; +#endif + vec4 raw; +} vec4s; + +#endif /* cglm_types_struct_h */ diff --git a/include/cglm/types.h b/include/cglm/types.h index 69c2885..1190b73 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -32,7 +32,6 @@ # define CGLM_ALIGN_MAT CGLM_ALIGN(16) #endif -// array types typedef float vec2[2]; typedef float vec3[3]; typedef int ivec3[3]; @@ -46,30 +45,6 @@ typedef CGLM_ALIGN_IF(32) vec4 mat4[4]; typedef CGLM_ALIGN_IF(16) vec4 mat4[4]; #endif -// struct types -typedef union CGLM_ALIGN_IF(16) vec3s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - float x; - float y; - float z; - }; -#endif - vec3 raw; -} vec3s; - -typedef union CGLM_ALIGN_IF(16) vec4s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - float x; - float y; - float z; - float w; - }; -#endif - vec4 raw; -} vec4s; - #define GLM_E 2.71828182845904523536028747135266250 /* e */ #define GLM_LOG2E 1.44269504088896340735992468100189214 /* log2(e) */ #define GLM_LOG10E 0.434294481903251827651128918916605082 /* log10(e) */ diff --git a/test/src/test_common.c b/test/src/test_common.c index 94331f4..f835235 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -47,6 +47,14 @@ test_rand_vec3(vec3 dest) { dest[2] = drand48(); } +vec3s +test_rand_vec3s() +{ + vec3s r; + test_rand_vec3(r.raw); + return r; +} + void test_rand_vec4(vec4 dest) { srand((unsigned int)time(NULL)); @@ -127,6 +135,12 @@ test_assert_vec3_eq(vec3 v1, vec3 v2) { assert_true(fabsf(v1[2] - v2[2]) <= 0.000009); } +void +test_assert_vec3s_eq(vec3s v1, vec3s v2) +{ + test_assert_vec3_eq(v1.raw, v2.raw); +} + void test_assert_vec4_eq(vec4 v1, vec4 v2) { assert_true(fabsf(v1[0] - v2[0]) <= 0.000009); /* rounding errors */ diff --git a/test/src/test_common.h b/test/src/test_common.h index c6a2dc4..51d1826 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -44,6 +44,9 @@ test_assert_mat3_eq(mat3 m1, mat3 m2); void test_assert_vec3_eq(vec3 v1, vec3 v2); +void +test_assert_vec3s_eq(vec3s v1, vec3s v2); + void test_assert_vec4_eq(vec4 v1, vec4 v2); @@ -59,6 +62,9 @@ test_assert_quat_eq_abs(versor v1, versor v2); void test_rand_vec3(vec3 dest); +vec3s +test_rand_vec3s(); + void test_rand_vec4(vec4 dest); diff --git a/test/src/test_vec3.c b/test/src/test_vec3.c index 6cfc593..4f1f2d1 100644 --- a/test/src/test_vec3.c +++ b/test/src/test_vec3.c @@ -12,6 +12,7 @@ test_vec3(void **state) { mat3 rot1m3; mat4 rot1; vec3 v, v1, v2; + vec3s vs1, vs2, vs3, vs4; /* test zero */ glm_vec3_zero(v); @@ -75,4 +76,12 @@ test_vec3(void **state) { test_assert_vec3_eq(v1, v2); test_assert_vec3_eq(v1, GLM_ZUP); + + /* structs */ + vs1 = test_rand_vec3s(); + vs2 = test_rand_vec3s(); + + vs3 = glms_vec3_add(vs1, vs2); + vs4 = glms_vec3_maxv(vs1, vs3); + test_assert_vec3s_eq(vs3, vs4); } From 892a7c7dcea88fa3e36f0929376f8b03caa43f7b Mon Sep 17 00:00:00 2001 From: acoto87 Date: Tue, 30 Apr 2019 22:08:17 -0500 Subject: [PATCH 05/11] - add mat3, mat4, sphere.h --- include/cglm/cglms.h | 3 ++ include/cglm/structs/mat3.h | 57 +++++++++++++++++++++ include/cglm/structs/mat4.h | 80 ++++++++++++++++++++++++++++++ include/cglm/structs/sphere.h | 93 +++++++++++++++++++++++++++++++++++ include/cglm/structs/vec4.h | 7 --- 5 files changed, 233 insertions(+), 7 deletions(-) create mode 100644 include/cglm/structs/mat3.h create mode 100644 include/cglm/structs/mat4.h create mode 100644 include/cglm/structs/sphere.h diff --git a/include/cglm/cglms.h b/include/cglm/cglms.h index 9f00dc8..67dbfa4 100644 --- a/include/cglm/cglms.h +++ b/include/cglm/cglms.h @@ -15,6 +15,9 @@ extern "C" { #include "types-struct.h" #include "structs/vec3.h" #include "structs/vec4.h" +#include "structs/mat3.h" +#include "structs/mat4.h" +#include "structs/sphere.h" #ifdef __cplusplus } diff --git a/include/cglm/structs/mat3.h b/include/cglm/structs/mat3.h new file mode 100644 index 0000000..c1d8e48 --- /dev/null +++ b/include/cglm/structs/mat3.h @@ -0,0 +1,57 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE vec3s glm_mat3_mulv(mat3 m, vec3s v); + CGLM_INLINE float glm_mat3_rmc(vec3s r, mat3 m, vec3s c); + */ + +#ifndef cglm_mat3s_h +#define cglm_mat3s_h + +#include "../common.h" +#include "../types-struct.h" +#include "../mat3.h" +#include "vec3.h" + +/*! + * @brief multiply mat3 with vec3 (column vector) and store in dest vector + * + * @param[in] m mat3 (left) + * @param[in] v vec3 (right, column vector) + * returns vec3 (result, column vector) + */ +CGLM_INLINE +vec3s +glms_mat3_mulv(mat3 m, vec3s v) { + vec3s r; + glm_mat3_mulv(m, v.raw, r.raw); + return r; +} + +/*! + * @brief helper for R (row vector) * M (matrix) * C (column vector) + * + * rmc stands for Row * Matrix * Column + * + * the result is scalar because R * M = Matrix1x3 (row vector), + * then Matrix1x3 * Vec3 (column vector) = Matrix1x1 (Scalar) + * + * @param[in] r row vector or matrix1x3 + * @param[in] m matrix3x3 + * @param[in] c column vector or matrix3x1 + * + * @return scalar value e.g. Matrix1x1 + */ +CGLM_INLINE +float +glms_mat3_rmc(vec3s r, mat3 m, vec3s c) { + return glm_mat3_rmc(r.raw, m, c.raw); +} + +#endif /* cglm_mat3s_h */ diff --git a/include/cglm/structs/mat4.h b/include/cglm/structs/mat4.h new file mode 100644 index 0000000..fc785d7 --- /dev/null +++ b/include/cglm/structs/mat4.h @@ -0,0 +1,80 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/*! + * Most of functions in this header are optimized manually with SIMD + * if available. You dont need to call/incude SIMD headers manually + */ + +/* + Functions: + CGLM_INLINE void glm_mat4_mulv(mat4 m, vec4 v, vec4 dest); + CGLM_INLINE void glm_mat4_mulv3(mat4 m, vec3 v, vec3 dest); + CGLM_INLINE float glm_mat4_rmc(vec4 r, mat4 m, vec4 c); + */ + +#ifndef cglm_mats_h +#define cglm_mats_h + +#include "../common.h" +#include "../types-struct.h" +#include "../mat4.h" +#include "vec4.h" +#include "vec3.h" + +/*! + * @brief multiply mat4 with vec4 (column vector) and store in dest vector + * + * @param[in] m mat4 (left) + * @param[in] v vec4 (right, column vector) + * returns vec4 (result, column vector) + */ +CGLM_INLINE +vec4s +glms_mat4_mulv(mat4 m, vec4s v) { + vec4s r; + glm_mat4_mulv(m, v.raw, r.raw); + return r; +} + +/*! + * @brief multiply vector with mat4 + * + * @param[in] m mat4(affine transform) + * @param[in] v vec3 + * @param[in] last 4th item to make it vec4 + * returns result vector (vec3) + */ +CGLM_INLINE +vec3s +glms_mat4_mulv3(mat4 m, vec3s v, float last) { + vec3s r; + glm_mat4_mulv3(m, v.raw, last, r.raw); + return r; +} + +/*! + * @brief helper for R (row vector) * M (matrix) * C (column vector) + * + * rmc stands for Row * Matrix * Column + * + * the result is scalar because R * M = Matrix1x4 (row vector), + * then Matrix1x4 * Vec4 (column vector) = Matrix1x1 (Scalar) + * + * @param[in] r row vector or matrix1x4 + * @param[in] m matrix4x4 + * @param[in] c column vector or matrix4x1 + * + * @return scalar value e.g. B(s) + */ +CGLM_INLINE +float +glms_mat4_rmc(vec4s r, mat4 m, vec4s c) { + return glm_mat4_rmc(r.raw, m, c.raw); +} + +#endif /* cglm_mats_h */ diff --git a/include/cglm/structs/sphere.h b/include/cglm/structs/sphere.h new file mode 100644 index 0000000..b336c3b --- /dev/null +++ b/include/cglm/structs/sphere.h @@ -0,0 +1,93 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_spheres_h +#define cglm_spheres_h + +#include "../common.h" +#include "../types-struct.h" +#include "../mat4.h" +#include "../sphere.h" + +/* + Sphere Representation in cglm: [center.x, center.y, center.z, radii] + + You could use this representation or you can convert it to vec4 before call + any function + */ + +/*! + * @brief helper for getting sphere radius + * + * @param[in] s sphere + * + * @return returns radii + */ +CGLM_INLINE +float +glms_sphere_radii(vec4s s) { + return glm_sphere_radii(s.raw); +} + +/*! + * @brief apply transform to sphere, it is just wrapper for glm_mat4_mulv3 + * + * @param[in] s sphere + * @param[in] m transform matrix + * @returns transformed sphere + */ +CGLM_INLINE +vec4s +glms_sphere_transform(vec4s s, mat4 m) { + vec4s r; + glm_sphere_transform(s.raw, m, r.raw); + return r; +} + +/*! + * @brief merges two spheres and creates a new one + * + * two sphere must be in same space, for instance if one in world space then + * the other must be in world space too, not in local space. + * + * @param[in] s1 sphere 1 + * @param[in] s2 sphere 2 + * returns merged/extended sphere + */ +CGLM_INLINE +vec4s +glms_sphere_merge(vec4s s1, vec4s s2) { + vec4s r; + glm_sphere_merge(s1.raw, s2.raw, r.raw); + return r; +} + +/*! + * @brief check if two sphere intersects + * + * @param[in] s1 sphere + * @param[in] s2 other sphere + */ +CGLM_INLINE +bool +glms_sphere_sphere(vec4s s1, vec4s s2) { + return glm_sphere_sphere(s1.raw, s2.raw); +} + +/*! + * @brief check if sphere intersects with point + * + * @param[in] s sphere + * @param[in] point point + */ +CGLM_INLINE +bool +glms_sphere_point(vec4s s, vec3s point) { + return glm_sphere_point(s.raw, point.raw); +} + +#endif /* cglm_spheres_h */ diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index c0a79c3..c8ec509 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -8,10 +8,6 @@ #ifndef cglm_vec4s_h #define cglm_vec4s_h -#ifdef __cplusplus -extern "C" { -#endif - #include "../common.h" #include "../types-struct.h" #include "../util.h" @@ -255,7 +251,4 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) { return r; } -#ifdef __cplusplus -} -#endif #endif /* cglm_vec4s_h */ From 3ff902de9ccaff2a6d8660ff2a5a8254bf9bb3c3 Mon Sep 17 00:00:00 2001 From: acoto87 Date: Fri, 3 May 2019 22:48:13 -0500 Subject: [PATCH 06/11] add mat3 and mat4 implementation --- include/cglm/structs/mat3.h | 263 +++++++++++++++++++++- include/cglm/structs/mat4.h | 427 ++++++++++++++++++++++++++++++++++-- include/cglm/types-struct.h | 60 ++++- 3 files changed, 728 insertions(+), 22 deletions(-) diff --git a/include/cglm/structs/mat3.h b/include/cglm/structs/mat3.h index c1d8e48..63fe7c6 100644 --- a/include/cglm/structs/mat3.h +++ b/include/cglm/structs/mat3.h @@ -6,9 +6,29 @@ */ /* + Macros: + GLMS_MAT3_IDENTITY_INIT + GLMS_MAT3_ZERO_INIT + GLMS_MAT3_IDENTITY + GLMS_MAT3_ZERO + Functions: - CGLM_INLINE vec3s glm_mat3_mulv(mat3 m, vec3s v); - CGLM_INLINE float glm_mat3_rmc(vec3s r, mat3 m, vec3s c); + CGLM_INLINE mat3s glms_mat3_copy(mat3s mat); + CGLM_INLINE mat3s glms_mat3_identity(); + CGLM_INLINE void glms_mat3_identity_array(mat3s * __restrict mat, size_t count); + CGLM_INLINE mat3s glms_mat3_zero(); + CGLM_INLINE mat3s glms_mat3_mul(mat3s m1, mat3s m2); + CGLM_INLINE mat3s glms_mat3_transpose_to(mat3s m); + CGLM_INLINE ma3s glms_mat3_transpose(mat3s m); + CGLM_INLINE vec3s glms_mat3_mulv(mat3s m, vec3s v); + CGLM_INLINE float glms_mat3_trace(mat3s m); + CGLM_INLINE versor glms_mat3_quat(mat3s m); + CGLM_INLINE mat3s glms_mat3_scale(mat3s m, float s); + CGLM_INLINE float glms_mat3_det(mat3s mat); + CGLM_INLINE mat3s glms_mat3_inv(mat3s mat); + CGLM_INLINE mat3s glms_mat3_swap_col(mat3s mat, int col1, int col2); + CGLM_INLINE mat3s glms_mat3_swap_row(mat3s mat, int row1, int row2); + CGLM_INLINE float glms_mat3_rmc(vec3s r, mat3s m, vec3s c); */ #ifndef cglm_mat3s_h @@ -19,21 +39,250 @@ #include "../mat3.h" #include "vec3.h" +#define GLMS_MAT3_IDENTITY_INIT {1.0f, 0.0f, 0.0f, \ + 0.0f, 1.0f, 0.0f, \ + 0.0f, 0.0f, 1.0f} +#define GLMS_MAT3_ZERO_INIT {0.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f} + +/* for C only */ +#define GLMS_MAT3_IDENTITY ((mat3s)GLMS_MAT3_IDENTITY_INIT) +#define GLMS_MAT3_ZERO ((mat3s)GLMS_MAT3_ZERO_INIT) + +/*! + * @brief copy all members of [mat] to [dest] + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat3s +glms_mat3_copy(mat3s mat) { + mat3s r; + glm_mat3_copy(mat.raw, r.raw); + return r; +} + +/*! + * @brief make given matrix identity. It is identical with below, + * but it is more easy to do that with this func especially for members + * e.g. glm_mat3_identity(aStruct->aMatrix); + * + * @code + * glm_mat3_copy(GLM_MAT3_IDENTITY, mat); // C only + * + * // or + * mat3 mat = GLM_MAT3_IDENTITY_INIT; + * @endcode + * + * @returns destination + */ +CGLM_INLINE +mat3s +glms_mat3_identity() { + mat3s r; + glm_mat3_identity(r.raw); + return r; +} + +/*! + * @brief make given matrix array's each element identity matrix + * + * @param[in, out] mat matrix array (must be aligned (16/32) + * if alignment is not disabled) + * + * @param[in] count count of matrices + */ +CGLM_INLINE +void +glms_mat3_identity_array(mat3s * __restrict mat, size_t count) { + CGLM_ALIGN_MAT mat3s t = GLMS_MAT3_IDENTITY_INIT; + size_t i; + + for (i = 0; i < count; i++) { + glm_mat3_copy(t.raw, mat[i].raw); + } +} + +/*! + * @brief make given matrix zero. + * + * @returns matrix + */ +CGLM_INLINE +mat3s +glms_mat3_zero() { + mat3s r; + glm_mat3_zero(r.raw); + return r; +} + +/*! + * @brief multiply m1 and m2 to dest + * + * m1, m2 and dest matrices can be same matrix, it is possible to write this: + * + * @code + * mat3 m = GLM_MAT3_IDENTITY_INIT; + * glm_mat3_mul(m, m, m); + * @endcode + * + * @param[in] m1 left matrix + * @param[in] m2 right matrix + * @returns destination matrix + */ +CGLM_INLINE +mat3s +glms_mat3_mul(mat3s m1, mat3s m2) { + mat3s r; + glm_mat3_mul(m1.raw, m2.raw, r.raw); + return r; +} + +/*! + * @brief transpose mat3 and store in dest + * + * source matrix will not be transposed unless dest is m + * + * @param[in] m matrix + * @param[out] dest result + */ +CGLM_INLINE +mat3s +glms_mat3_transpose_to(mat3s m) { + mat3s r; + glm_mat3_transpose_to(m.raw, r.raw); + return r; +} + +/*! + * @brief tranpose mat3 and store result in same matrix + * + * @param[in, out] m source and dest + */ +CGLM_INLINE +mat3s +glms_mat3_transpose(mat3s m) { + glm_mat3_transpose(m.raw); + return m; +} + /*! * @brief multiply mat3 with vec3 (column vector) and store in dest vector * * @param[in] m mat3 (left) * @param[in] v vec3 (right, column vector) - * returns vec3 (result, column vector) + * @returns vec3 (result, column vector) */ CGLM_INLINE vec3s -glms_mat3_mulv(mat3 m, vec3s v) { +glms_mat3_mulv(mat3s m, vec3s v) { vec3s r; - glm_mat3_mulv(m, v.raw, r.raw); + glm_mat3_mulv(m.raw, v.raw, r.raw); return r; } +/*! + * @brief trace of matrix + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glms_mat3_trace(mat3s m) { + return glm_mat3_trace(m.raw); +} + +/*! + * @brief convert mat3 to quaternion + * + * @param[in] m rotation matrix + * @returns destination quaternion + */ +CGLM_INLINE +versors +glms_mat3_quat(mat3s m) { + versors r; + glm_mat3_quat(m.raw, r.raw); + return r; +} + +/*! + * @brief scale (multiply with scalar) matrix + * + * multiply matrix with scalar + * + * @param[in] m matrix + * @param[in] s scalar + * @returns scaled matrix + */ +CGLM_INLINE +mat3s +glms_mat3_scale(mat3s m, float s) { + glm_mat3_scale(m.raw, s); + return m; +} + +/*! + * @brief mat3 determinant + * + * @param[in] mat matrix + * + * @return determinant + */ +CGLM_INLINE +float +glms_mat3_det(mat3s mat) { + return glm_mat3_det(mat.raw); +} + +/*! + * @brief inverse mat3 and store in dest + * + * @param[in] mat matrix + * @returns inverse matrix + */ +CGLM_INLINE +mat3s +glms_mat3_inv(mat3s mat) { + mat3s r; + glm_mat3_inv(mat.raw, r.raw); + return r; +} + +/*! + * @brief swap two matrix columns + * + * @param[in] mat matrix + * @param[in] col1 col1 + * @param[in] col2 col2 + * @returns matrix + */ +CGLM_INLINE +mat3s +glms_mat3_swap_col(mat3s mat, int col1, int col2) { + glm_mat3_swap_col(mat.raw, col1, col2); + return mat; +} + +/*! + * @brief swap two matrix rows + * + * @param[in] mat matrix + * @param[in] row1 row1 + * @param[in] row2 row2 + * @returns matrix + */ +CGLM_INLINE +mat3s +glms_mat3_swap_row(mat3s mat, int row1, int row2) { + glm_mat3_swap_row(mat.raw, row1, row2); + return mat; +} + /*! * @brief helper for R (row vector) * M (matrix) * C (column vector) * @@ -50,8 +299,8 @@ glms_mat3_mulv(mat3 m, vec3s v) { */ CGLM_INLINE float -glms_mat3_rmc(vec3s r, mat3 m, vec3s c) { - return glm_mat3_rmc(r.raw, m, c.raw); +glms_mat3_rmc(vec3s r, mat3s m, vec3s c) { + return glm_mat3_rmc(r.raw, m.raw, c.raw); } #endif /* cglm_mat3s_h */ diff --git a/include/cglm/structs/mat4.h b/include/cglm/structs/mat4.h index fc785d7..a61f18d 100644 --- a/include/cglm/structs/mat4.h +++ b/include/cglm/structs/mat4.h @@ -11,14 +11,42 @@ */ /* + Macros: + GLMS_MAT4_IDENTITY_INIT + GLMS_MAT4_ZERO_INIT + GLMS_MAT4_IDENTITY + GLMS_MAT4_ZERO + Functions: - CGLM_INLINE void glm_mat4_mulv(mat4 m, vec4 v, vec4 dest); - CGLM_INLINE void glm_mat4_mulv3(mat4 m, vec3 v, vec3 dest); - CGLM_INLINE float glm_mat4_rmc(vec4 r, mat4 m, vec4 c); + CGLM_INLINE mat4s glms_mat4_ucopy(mat4s mat); + CGLM_INLINE mat4s glms_mat4_copy(mat4s mat); + CGLM_INLINE mat4s glms_mat4_identity(); + CGLM_INLINE void glms_mat4_identity_array(mat4s * __restrict mat, size_t count); + CGLM_INLINE mat4s glms_mat4_zero(); + CGLM_INLINE mat3s glms_mat4_pick3(mat4s mat); + CGLM_INLINE mat3s glms_mat4_pick3t(mat4s mat); + CGLM_INLINE mat4s glms_mat4_ins3(mat3s mat); + CGLM_INLINE mat4s glms_mat4_mul(mat4s m1, mat4s m2); + CGLM_INLINE mat4s glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len); + CGLM_INLINE vec4s glms_mat4_mulv(mat4s m, vec4s v); + CGLM_INLINE float glms_mat4_trace(mat4s m); + CGLM_INLINE float glms_mat4_trace3(mat4s m); + CGLM_INLINE versors glms_mat4_quat(mat4s m); + CGLM_INLINE vec3s glms_mat4_mulv3(mat4s m, vec3s v, float last); + CGLM_INLINE mat4s glms_mat4_transpose_to(mat4s m); + CGLM_INLINE mat4s glms_mat4_transpose(mat4s m); + CGLM_INLINE mat4s glms_mat4_scale_p(mat4s m, float s); + CGLM_INLINE mat4s glms_mat4_scale(mat4s m, float s); + CGLM_INLINE float glms_mat4_det(mat4s mat); + CGLM_INLINE mat4s glms_mat4_inv(mat4s mat); + CGLM_INLINE mat4s glms_mat4_inv_fast(mat4s mat); + CGLM_INLINE mat4s glms_mat4_swap_col(mat4s mat, int col1, int col2); + CGLM_INLINE mat4s glms_mat4_swap_row(mat4s mat, int row1, int row2); + CGLM_INLINE float glms_mat4_rmc(vec4s r, mat4s m, vec4s c); */ -#ifndef cglm_mats_h -#define cglm_mats_h +#ifndef cglm_mat4s_h +#define cglm_mat4s_h #include "../common.h" #include "../types-struct.h" @@ -26,18 +54,250 @@ #include "vec4.h" #include "vec3.h" +#define GLMS_MAT4_IDENTITY_INIT {{1.0f, 0.0f, 0.0f, 0.0f}, \ + {0.0f, 1.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 1.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f, 1.0f}} + +#define GLMS_MAT4_ZERO_INIT {{0.0f, 0.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f, 0.0f}} + +/* for C only */ +#define GLMS_MAT4_IDENTITY ((mat4)GLMS_MAT4_IDENTITY_INIT) +#define GLMS_MAT4_ZERO ((mat4)GLMS_MAT4_ZERO_INIT) + +/*! + * @brief copy all members of [mat] to [dest] + * + * matrix may not be aligned, u stands for unaligned, this may be useful when + * copying a matrix from external source e.g. asset importer... + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat4s +glms_mat4_ucopy(mat4s mat) { + mat4s r; + glm_mat4_ucopy(mat.raw, r.raw); + return r; +} + +/*! + * @brief copy all members of [mat] to [dest] + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat4s +glms_mat4_copy(mat4s mat) { + mat4s r; + glm_mat4_copy(mat.raw, r.raw); + return r; +} + +/*! + * @brief make given matrix identity. It is identical with below, + * but it is more easy to do that with this func especially for members + * e.g. glm_mat4_identity(aStruct->aMatrix); + * + * @code + * glm_mat4_copy(GLM_MAT4_IDENTITY, mat); // C only + * + * // or + * mat4 mat = GLM_MAT4_IDENTITY_INIT; + * @endcode + * + * @retuns destination + */ +CGLM_INLINE +mat4s +glms_mat4_identity() { + mat4s r; + glm_mat4_identity(r.raw); + return r; +} + +/*! + * @brief make given matrix array's each element identity matrix + * + * @param[in, out] mat matrix array (must be aligned (16/32) + * if alignment is not disabled) + * + * @param[in] count count of matrices + */ +CGLM_INLINE +void +glms_mat4_identity_array(mat4s * __restrict mat, size_t count) { + CGLM_ALIGN_MAT mat4 t = GLM_MAT4_IDENTITY_INIT; + size_t i; + + for (i = 0; i < count; i++) { + glm_mat4_copy(t.raw, mat[i].raw); + } +} + +/*! + * @brief make given matrix zero. + * + * @returns matrix + */ +CGLM_INLINE +mat4s +glms_mat4_zero() { + mat4s r; + glm_mat4_zero(r.raw); + return r; +} + +/*! + * @brief copy upper-left of mat4 to mat3 + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat3s +glms_mat4_pick3(mat4s mat) { + mat3s r; + glm_mat4_pick3(mat.raw, r.raw); + return r; +} + +/*! + * @brief copy upper-left of mat4 to mat3 (transposed) + * + * the postfix t stands for transpose + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat3s +glms_mat4_pick3t(mat4s mat) { + mat3s r; + glm_mat4_pick3t(mat.raw, r.raw); + return r; +} + +/*! + * @brief copy mat3 to mat4's upper-left + * + * @param[in] mat source + * @returns destination + */ +CGLM_INLINE +mat4s +glms_mat4_ins3(mat3s mat) { + mat4s r; + glm_mat4_ins3(mat.raw, r.raw); + return r; +} + +/*! + * @brief multiply m1 and m2 to dest + * + * m1, m2 and dest matrices can be same matrix, it is possible to write this: + * + * @code + * mat4 m = GLM_MAT4_IDENTITY_INIT; + * glm_mat4_mul(m, m, m); + * @endcode + * + * @param[in] m1 left matrix + * @param[in] m2 right matrix + * @returns destination matrix + */ +CGLM_INLINE +mat4s +glms_mat4_mul(mat4s m1, mat4s m2) { + mat4s r; + glm_mat4_mul(m1.raw, m2.raw, r.raw); + return r; +} + +/*! + * @brief mupliply N mat4 matrices and store result in dest + * + * this function lets you multiply multiple (more than two or more...) matrices + *

multiplication will be done in loop, this may reduce instructions + * size but if len is too small then compiler may unroll whole loop, + * usage: + * @code + * mat m1, m2, m3, m4, res; + * + * glm_mat4_mulN((mat4 *[]){&m1, &m2, &m3, &m4}, 4, res); + * @endcode + * + * @warning matrices parameter is pointer array not mat4 array! + * + * @param[in] matrices mat4 * array + * @param[in] len matrices count + * @returns result + */ +CGLM_INLINE +mat4s +glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len) { + mat4s r; + glm_mat4_mulN(matrices, len, r.raw); + return r; +} + /*! * @brief multiply mat4 with vec4 (column vector) and store in dest vector * * @param[in] m mat4 (left) * @param[in] v vec4 (right, column vector) - * returns vec4 (result, column vector) + * @returns vec4 (result, column vector) */ CGLM_INLINE vec4s -glms_mat4_mulv(mat4 m, vec4s v) { +glms_mat4_mulv(mat4s m, vec4s v) { vec4s r; - glm_mat4_mulv(m, v.raw, r.raw); + glm_mat4_mulv(m.raw, v.raw, r.raw); + return r; +} + +/*! + * @brief trace of matrix + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glms_mat4_trace(mat4s m) { + return glm_mat4_trace(m.raw); +} + +/*! + * @brief trace of matrix (rotation part) + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glms_mat4_trace3(mat4s m) { + return glm_mat4_trace3(m.raw); +} + +/*! + * @brief convert mat4's rotation part to quaternion + * + * @param[in] m affine matrix + * @returns destination quaternion + */ +CGLM_INLINE +versors +glms_mat4_quat(mat4s m) { + versors r; + glm_mat4_quat(m.raw, r.raw); return r; } @@ -47,16 +307,154 @@ glms_mat4_mulv(mat4 m, vec4s v) { * @param[in] m mat4(affine transform) * @param[in] v vec3 * @param[in] last 4th item to make it vec4 - * returns result vector (vec3) + * @returns result vector (vec3) */ CGLM_INLINE vec3s -glms_mat4_mulv3(mat4 m, vec3s v, float last) { +glms_mat4_mulv3(mat4s m, vec3s v, float last) { vec3s r; - glm_mat4_mulv3(m, v.raw, last, r.raw); + glm_mat4_mulv3(m.raw, v.raw, last, r.raw); return r; } +/*! + * @brief transpose mat4 and store in dest + * + * source matrix will not be transposed unless dest is m + * + * @param[in] m matrix + * @returns result + */ +CGLM_INLINE +mat4s +glms_mat4_transpose_to(mat4s m) { + mat4s r; + glm_mat4_transpose_to(m.raw, r.raw); + return r; +} + +/*! + * @brief tranpose mat4 and store result in same matrix + * + * @param[in] m source + * @returns result + */ +CGLM_INLINE +mat4s +glms_mat4_transpose(mat4s m) { + glm_mat4_transpose(m.raw); + return m; +} + +/*! + * @brief scale (multiply with scalar) matrix without simd optimization + * + * multiply matrix with scalar + * + * @param[in] m matrix + * @param[in] s scalar + * @returns matrix + */ +CGLM_INLINE +mat4s +glms_mat4_scale_p(mat4s m, float s) { + glm_mat4_scale_p(m.raw, s); + return m; +} + +/*! + * @brief scale (multiply with scalar) matrix + * + * multiply matrix with scalar + * + * @param[in] m matrix + * @param[in] s scalar + * @returns matrix + */ +CGLM_INLINE +mat4s +glms_mat4_scale(mat4s m, float s) { + glm_mat4_scale(m.raw, s); + return m; +} + +/*! + * @brief mat4 determinant + * + * @param[in] mat matrix + * + * @return determinant + */ +CGLM_INLINE +float +glms_mat4_det(mat4s mat) { + return glm_mat4_det(mat.raw); +} + +/*! + * @brief inverse mat4 and store in dest + * + * @param[in] mat matrix + * @returns inverse matrix + */ +CGLM_INLINE +mat4s +glms_mat4_inv(mat4s mat) { + mat4s r; + glm_mat4_inv(mat.raw, r.raw); + return r; +} + +/*! + * @brief inverse mat4 and store in dest + * + * this func uses reciprocal approximation without extra corrections + * e.g Newton-Raphson. this should work faster than normal, + * to get more precise use glm_mat4_inv version. + * + * NOTE: You will lose precision, glm_mat4_inv is more accurate + * + * @param[in] mat matrix + * @returns inverse matrix + */ +CGLM_INLINE +mat4s +glms_mat4_inv_fast(mat4s mat) { + mat4s r; + glm_mat4_inv_fast(mat.raw, r.raw); + return r; +} + +/*! + * @brief swap two matrix columns + * + * @param[in] mat matrix + * @param[in] col1 col1 + * @param[in] col2 col2 + * @returns matrix + */ +CGLM_INLINE +mat4s +glms_mat4_swap_col(mat4s mat, int col1, int col2) { + glm_mat4_swap_col(mat.raw, col1, col2); + return mat; +} + +/*! + * @brief swap two matrix rows + * + * @param[in] mat matrix + * @param[in] row1 row1 + * @param[in] row2 row2 + * @returns matrix + */ +CGLM_INLINE +mat4s +glms_mat4_swap_row(mat4s mat, int row1, int row2) { + glm_mat4_swap_row(mat.raw, row1, row2); + return mat; +} + /*! * @brief helper for R (row vector) * M (matrix) * C (column vector) * @@ -73,8 +471,9 @@ glms_mat4_mulv3(mat4 m, vec3s v, float last) { */ CGLM_INLINE float -glms_mat4_rmc(vec4s r, mat4 m, vec4s c) { - return glm_mat4_rmc(r.raw, m, c.raw); +glms_mat4_rmc(vec4s r, mat4s m, vec4s c) { + return glm_mat4_rmc(r.raw, m.raw, c.raw); } -#endif /* cglm_mats_h */ +#endif /* cglm_mat4s_h */ + \ No newline at end of file diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h index 27e04fe..13431aa 100644 --- a/include/cglm/types-struct.h +++ b/include/cglm/types-struct.h @@ -10,7 +10,7 @@ #include "types.h" -typedef union CGLM_ALIGN_IF(16) vec3s { +typedef union vec3s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { float x; @@ -33,4 +33,62 @@ typedef union CGLM_ALIGN_IF(16) vec4s { vec4 raw; } vec4s; +typedef vec4s versors; + +typedef union mat3s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float m00; + float m01; + float m02; + float m10; + float m11; + float m12; + float m20; + float m21; + float m22; + }; + struct { + vec3s col0; + vec3s col1; + vec3s col2; + }; +#endif + mat3 raw; +} mat3s; + +#ifdef __AVX__ +typedef union CGLM_ALIGN_IF(32) mat4s { +#else +typedef union CGLM_ALIGN_IF(16) mat4s { +#endif +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + float m00; + float m01; + float m02; + float m03; + float m10; + float m11; + float m12; + float m13; + float m20; + float m21; + float m22; + float m23; + float m30; + float m31; + float m32; + float m33; + }; + struct { + vec4s col0; + vec4s col1; + vec4s col2; + vec4s col3; + }; +#endif + mat4 raw; +} mat4s; + #endif /* cglm_types_struct_h */ From bc1969ab75b47d7dac9e11f387a195b6fcaa41e1 Mon Sep 17 00:00:00 2001 From: acoto87 Date: Tue, 7 May 2019 16:16:00 -0500 Subject: [PATCH 07/11] - Changes in `mat3s` and `mat4s` types. - Added `ivec3s` type - Struct implementation of: affine.h, box.h, color.h, curve.h, frutum.h, io.h, plane.h, project.h - Deleted `glms_mat3_transpose_to` and `glms_mat4_transpose_to` - Bug fixes in mat4.h --- include/cglm/frustum.h | 2 +- include/cglm/plane.h | 2 +- include/cglm/structs/affine.h | 343 +++++++++++++++++++++++++++++++++ include/cglm/structs/box.h | 285 +++++++++++++++++++++++++++ include/cglm/structs/color.h | 27 +++ include/cglm/structs/curve.h | 40 ++++ include/cglm/structs/frustum.h | 240 +++++++++++++++++++++++ include/cglm/structs/io.h | 100 ++++++++++ include/cglm/structs/mat3.h | 17 -- include/cglm/structs/mat4.h | 52 ++--- include/cglm/structs/plane.h | 40 ++++ include/cglm/structs/project.h | 104 ++++++++++ include/cglm/structs/sphere.h | 2 +- include/cglm/types-struct.h | 51 ++--- 14 files changed, 1223 insertions(+), 82 deletions(-) create mode 100644 include/cglm/structs/affine.h create mode 100644 include/cglm/structs/box.h create mode 100644 include/cglm/structs/color.h create mode 100644 include/cglm/structs/curve.h create mode 100644 include/cglm/structs/frustum.h create mode 100644 include/cglm/structs/io.h create mode 100644 include/cglm/structs/plane.h create mode 100644 include/cglm/structs/project.h diff --git a/include/cglm/frustum.h b/include/cglm/frustum.h index 78b39d2..5aa3c17 100644 --- a/include/cglm/frustum.h +++ b/include/cglm/frustum.h @@ -65,7 +65,7 @@ * Exracted planes order: [left, right, bottom, top, near, far] * * @param[in] m matrix (see brief) - * @param[out] dest exracted view frustum planes (see brief) + * @param[out] dest extracted view frustum planes (see brief) */ CGLM_INLINE void diff --git a/include/cglm/plane.h b/include/cglm/plane.h index 48fe879..7a5291d 100644 --- a/include/cglm/plane.h +++ b/include/cglm/plane.h @@ -25,7 +25,7 @@ /*! * @brief normalizes a plane * - * @param[in, out] plane pnale to normalize + * @param[in, out] plane plane to normalize */ CGLM_INLINE void diff --git a/include/cglm/structs/affine.h b/include/cglm/structs/affine.h new file mode 100644 index 0000000..6f8be48 --- /dev/null +++ b/include/cglm/structs/affine.h @@ -0,0 +1,343 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE mat4s glms_translate(mat4s m, vec3s v); + CGLM_INLINE mat4s glms_translate_x(mat4s m, float x); + CGLM_INLINE mat4s glms_translate_y(mat4s m, float y); + CGLM_INLINE mat4s glms_translate_z(mat4s m, float z); + CGLM_INLINE mat4s glms_translate_make(vec3s v); + CGLM_INLINE mat4s glms_scale_to(mat4s m, vec3s v); + CGLM_INLINE mat4s glms_scale_make(vec3s v); + CGLM_INLINE mat4s glms_scale(mat4s m, vec3s v); + CGLM_INLINE mat4s glms_scale_uni(mat4s m, float s); + CGLM_INLINE mat4s glmx_rotate_x(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_y(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_z(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_make(float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate(mat4s m, float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis); + CGLM_INLINE vec3s glms_decompose_scalev(mat4s m); + CGLM_INLINE bool glms_uniscaled(mat4s m); + CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s r, vec3s s); + CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s r, vec3s s); + */ + +#ifndef cglm_affines_h +#define cglm_affines_h + +#include "../common.h" +#include "../types-struct.h" +#include "../affine.h" +#include "vec3.h" +#include "vec4.h" +#include "mat4.h" + +CGLM_INLINE +mat4s +glms_mat4_mul(mat4s m1, mat4s m2); + +/*! + * @brief translate existing transform matrix by v vector + * and stores result in same matrix + * + * @param[in] m affine transfrom + * @param[in] v translate vector [x, y, z] + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_translate(mat4s m, vec3s v) { + glm_translate(m.raw, v.raw); + return m; +} + +/*! + * @brief translate existing transform matrix by x factor + * + * @param[in] m affine transfrom + * @param[in] x x factor + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_translate_x(mat4s m, float x) { + glm_translate_x(m.raw, x); + return m; +} + +/*! + * @brief translate existing transform matrix by y factor + * + * @param[in] m affine transfrom + * @param[in] y y factor + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_translate_y(mat4s m, float y) { + glm_translate_y(m.raw, y); + return m; +} + +/*! + * @brief translate existing transform matrix by z factor + * + * @param[in] m affine transfrom + * @param[in] z z factor + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_translate_z(mat4s m, float z) { + glm_translate_z(m.raw, z); + return m; +} + +/*! + * @brief creates NEW translate transform matrix by v vector + * + * @param[in] v translate vector [x, y, z] + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_translate_make(vec3s v) { + mat4s m; + glm_translate_make(m.raw, v.raw); + return m; +} + +/*! + * @brief creates NEW scale matrix by v vector + * + * @param[out] m affine transfrom + * @param[in] v scale vector [x, y, z] + */ +CGLM_INLINE +mat4s +glms_scale_make(vec3s v) { + mat4s m; + glm_scale_make(m.raw, v.raw); + return m; +} + +/*! + * @brief scales existing transform matrix by v vector + * and stores result in same matrix + * + * @param[in] m affine transfrom + * @param[in] v scale vector [x, y, z] + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_scale(mat4s m, vec3s v) { + mat4s r; + glm_scale_to(m.raw, v.raw, r.raw); + return r; +} + +/*! + * @brief applies uniform scale to existing transform matrix v = [s, s, s] + * and stores result in same matrix + * + * @param[in] m affine transfrom + * @param[in] s scale factor + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_scale_uni(mat4s m, float s) { + glm_scale_uni(m.raw, s); + return m; +} + +/*! + * @brief rotate existing transform matrix around X axis by angle + * and store result in dest + * + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix + */ +CGLM_INLINE +mat4s +glmx_rotate_x(mat4s m, float angle) { + mat4s r; + glm_rotate_x(m.raw, angle, r.raw); + return r; +} + +/*! + * @brief rotate existing transform matrix around Y axis by angle + * and store result in dest + * + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix + */ +CGLM_INLINE +mat4s +glms_rotate_y(mat4s m, float angle) { + mat4s r; + glm_rotate_y(m.raw, angle, r.raw); + return r; +} + +/*! + * @brief rotate existing transform matrix around Z axis by angle + * and store result in dest + * + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix + */ +CGLM_INLINE +mat4s +glms_rotate_z(mat4s m, float angle) { + mat4s r; + glm_rotate_z(m.raw, angle, r.raw); + return r; +} + +/*! + * @brief creates NEW rotation matrix by angle and axis + * + * axis will be normalized so you don't need to normalize it + * + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_rotate_make(float angle, vec3s axis) { + mat4s m; + glm_rotate_make(m.raw, angle, axis.raw); + return m; +} + +/*! + * @brief rotate existing transform matrix around given axis by angle + * + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_rotate(mat4s m, float angle, vec3s axis) { + glm_rotate(m.raw, angle, axis.raw); + return m; +} + +/*! + * @brief rotate existing transform + * around given axis by angle at given pivot point (rotation center) + * + * @param[in] m affine transfrom + * @param[in] pivot rotation center + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis) { + glm_rotate_at(m.raw, pivot.raw, angle, axis.raw); + return m; +} + +/*! + * @brief creates NEW rotation matrix by angle and axis at given point + * + * this creates rotation matrix, it assumes you don't have a matrix + * + * this should work faster than glm_rotate_at because it reduces + * one glm_translate. + * + * @param[in] m affine transfrom + * @param[in] pivot rotation center + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom + */ +CGLM_INLINE +mat4s +glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis) { + glm_rotate_atm(m.raw, pivot.raw, angle, axis.raw); + return m; +} + +/*! + * @brief decompose scale vector + * + * @param[in] m affine transform + * @returns scale vector (Sx, Sy, Sz) + */ +CGLM_INLINE +vec3s +glms_decompose_scalev(mat4s m) { + mat3s m3x3; + vec3s s; + + m3x3 = glms_mat4_pick3(m); + + s.x = glms_vec3_norm(m3x3.col[0]); + s.y = glms_vec3_norm(m3x3.col[1]); + s.z = glms_vec3_norm(m3x3.col[2]); + return s; +} + +/*! + * @brief returns true if matrix is uniform scaled. This is helpful for + * creating normal matrix. + * + * @param[in] m m + * + * @return boolean + */ +CGLM_INLINE +bool +glms_uniscaled(mat4s m) { + return glm_uniscaled(m.raw); +} + +/*! + * @brief decompose rotation matrix (mat4) and scale vector [Sx, Sy, Sz] + * DON'T pass projected matrix here + * + * @param[in] m affine transform + * @param[out] r rotation matrix + * @param[out] s scale matrix + */ +CGLM_INLINE +void +glms_decompose_rs(mat4s m, mat4s r, vec3s s) { + glm_decompose_rs(m.raw, r.raw, s.raw); +} + +/*! + * @brief decompose affine transform, TODO: extract shear factors. + * DON'T pass projected matrix here + * + * @param[in] m affine transfrom + * @param[out] t translation vector + * @param[out] r rotation matrix (mat4) + * @param[out] s scaling vector [X, Y, Z] + */ +CGLM_INLINE +void +glms_decompose(mat4s m, vec4s t, mat4s r, vec3s s) { + glm_decompose(m.raw, t.raw, r.raw, s.raw); +} + +#endif /* cglm_affines_h */ diff --git a/include/cglm/structs/box.h b/include/cglm/structs/box.h new file mode 100644 index 0000000..e9c813f --- /dev/null +++ b/include/cglm/structs/box.h @@ -0,0 +1,285 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_boxs_h +#define cglm_boxs_h + +#include "../common.h" +#include "../types-struct.h" +#include "../box.h" +#include "vec3.h" +#include "vec4.h" +#include "mat4.h" + +/*! + * @brief apply transform to Axis-Aligned Bounding Box + * + * @param[in] box bounding box + * @param[in] m transform matrix + * @param[out] dest transformed bounding box + */ +CGLM_INLINE +void +glms_aabb_transform(vec3s box[2], mat4s m, vec3s dest[2]) { + vec3s v[2], xa, xb, ya, yb, za, zb; + mat3s m3x3; + + m3x3 = glms_mat4_pick3(m); + + xa = glms_vec3_scale(m3x3.col[0], box[0].x); + xb = glms_vec3_scale(m3x3.col[0], box[1].x); + + ya = glms_vec3_scale(m3x3.col[1], box[0].y); + yb = glms_vec3_scale(m3x3.col[1], box[1].y); + + za = glms_vec3_scale(m3x3.col[2], box[0].z); + zb = glms_vec3_scale(m3x3.col[2], box[1].z); + + /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */ + v[0] = m3x3.col[3]; + v[0] = glms_vec3_minadd(xa, xb); + v[0] = glms_vec3_minadd(ya, yb); + v[0] = glms_vec3_minadd(za, zb); + + /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ + v[1] = m3x3.col[3]; + v[1] = glms_vec3_maxadd(xa, xb); + v[1] = glms_vec3_maxadd(ya, yb); + v[1] = glms_vec3_maxadd(za, zb); + + dest[0] = glms_vec3_copy(v[0]); + dest[1] = glms_vec3_copy(v[1]); +} + +/*! + * @brief merges two AABB bounding box and creates new one + * + * two box must be in same space, if one of box is in different space then + * you should consider to convert it's space by glm_box_space + * + * @param[in] box1 bounding box 1 + * @param[in] box2 bounding box 2 + * @param[out] dest merged bounding box + */ +CGLM_INLINE +void +glms_aabb_merge(vec3s box1[2], vec3s box2[2], vec3s dest[2]) { + dest[0].x = glm_min(box1[0].x, box2[0].x); + dest[0].y = glm_min(box1[0].y, box2[0].y); + dest[0].z = glm_min(box1[0].z, box2[0].z); + + dest[1].x = glm_max(box1[1].x, box2[1].x); + dest[1].y = glm_max(box1[1].y, box2[1].y); + dest[1].z = glm_max(box1[1].z, box2[1].z); +} + +/*! + * @brief crops a bounding box with another one. + * + * this could be useful for gettng a bbox which fits with view frustum and + * object bounding boxes. In this case you crop view frustum box with objects + * box + * + * @param[in] box bounding box 1 + * @param[in] cropBox crop box + * @param[out] dest cropped bounding box + */ +CGLM_INLINE +void +glms_aabb_crop(vec3s box[2], vec3s cropBox[2], vec3s dest[2]) { + dest[0].x = glm_max(box[0].x, cropBox[0].x); + dest[0].y = glm_max(box[0].y, cropBox[0].y); + dest[0].z = glm_max(box[0].z, cropBox[0].z); + + dest[1].x = glm_min(box[1].x, cropBox[1].x); + dest[1].y = glm_min(box[1].y, cropBox[1].y); + dest[1].z = glm_min(box[1].z, cropBox[1].z); +} + +/*! + * @brief crops a bounding box with another one. + * + * this could be useful for gettng a bbox which fits with view frustum and + * object bounding boxes. In this case you crop view frustum box with objects + * box + * + * @param[in] box bounding box + * @param[in] cropBox crop box + * @param[in] clampBox miniumum box + * @param[out] dest cropped bounding box + */ +CGLM_INLINE +void +glms_aabb_crop_until(vec3s box[2], + vec3s cropBox[2], + vec3s clampBox[2], + vec3s dest[2]) { + glms_aabb_crop(box, cropBox, dest); + glms_aabb_merge(clampBox, dest, dest); +} + +/*! + * @brief check if AABB intersects with frustum planes + * + * this could be useful for frustum culling using AABB. + * + * OPTIMIZATION HINT: + * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR + * then this method should run even faster because it would only use two + * planes if object is not inside the two planes + * fortunately cglm extracts planes as this order! just pass what you got! + * + * @param[in] box bounding box + * @param[in] planes frustum planes + */ +CGLM_INLINE +bool +glms_aabb_frustum(vec3s box[2], vec4s planes[6]) { + vec4s p; + float dp; + int i; + + for (i = 0; i < 6; i++) { + p = planes[i]; + dp = p.x * box[p.x > 0.0f].x + + p.y * box[p.y > 0.0f].y + + p.z * box[p.z > 0.0f].z; + + if (dp < -p.w) + return false; + } + + return true; +} + +/*! + * @brief invalidate AABB min and max values + * + * @param[in, out] box bounding box + */ +CGLM_INLINE +void +glms_aabb_invalidate(vec3s box[2]) { + box[0] = glms_vec3_broadcast(FLT_MAX); + box[1] = glms_vec3_broadcast(-FLT_MAX); +} + +/*! + * @brief check if AABB is valid or not + * + * @param[in] box bounding box + */ +CGLM_INLINE +bool +glms_aabb_isvalid(vec3s box[2]) { + return glms_vec3_max(box[0]) != FLT_MAX && + glms_vec3_min(box[1]) != -FLT_MAX; +} + +/*! + * @brief distance between of min and max + * + * @param[in] box bounding box + */ +CGLM_INLINE +float +glms_aabb_size(vec3s box[2]) { + return glms_vec3_distance(box[0], box[1]); +} + +/*! + * @brief radius of sphere which surrounds AABB + * + * @param[in] box bounding box + */ +CGLM_INLINE +float +glms_aabb_radius(vec3s box[2]) { + return glms_aabb_size(box) * 0.5f; +} + +/*! + * @brief computes center point of AABB + * + * @param[in] box bounding box + * @param[out] dest center of bounding box + */ +CGLM_INLINE +vec3s +glms_aabb_center(vec3s box[2]) { + return glms_vec3_center(box[0], box[1]); +} + +/*! + * @brief check if two AABB intersects + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glms_aabb_aabb(vec3s box[2], vec3s other[2]) { + return (box[0].x <= other[1].x && box[1].x >= other[0].x) + && (box[0].y <= other[1].y && box[1].y >= other[0].y) + && (box[0].z <= other[1].z && box[1].z >= other[0].z); +} + +/*! + * @brief check if AABB intersects with sphere + * + * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c + * Solid Box - Solid Sphere test. + * + * @param[in] box solid bounding box + * @param[in] s solid sphere + */ +CGLM_INLINE +bool +glms_aabb_sphere(vec3s box[2], vec4 s) { + float dmin; + int a, b, c; + + a = s[0] >= box[0].x; + b = s[1] >= box[0].y; + c = s[2] >= box[0].z; + + dmin = glm_pow2(s[0] - box[a].x) + + glm_pow2(s[1] - box[b].y) + + glm_pow2(s[2] - box[c].z); + + return dmin <= glm_pow2(s[3]); +} + +/*! + * @brief check if point is inside of AABB + * + * @param[in] box bounding box + * @param[in] point point + */ +CGLM_INLINE +bool +glms_aabb_point(vec3s box[2], vec3s point) { + return (point.x >= box[0].x && point.x <= box[1].x) + && (point.y >= box[0].y && point.y <= box[1].y) + && (point.z >= box[0].z && point.z <= box[1].z); +} + +/*! + * @brief check if AABB contains other AABB + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glms_aabb_contains(vec3s box[2], vec3s other[2]) { + return (box[0].x <= other[0].x && box[1].x >= other[1].x) + && (box[0].y <= other[0].y && box[1].y >= other[1].y) + && (box[0].z <= other[0].z && box[1].z >= other[1].z); +} + +#endif /* cglm_boxs_h */ diff --git a/include/cglm/structs/color.h b/include/cglm/structs/color.h new file mode 100644 index 0000000..62cf5a8 --- /dev/null +++ b/include/cglm/structs/color.h @@ -0,0 +1,27 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_colors_h +#define cglm_colors_h + +#include "../common.h" +#include "../types-struct.h" +#include "../color.h" +#include "vec3.h" + +/*! + * @brief averages the color channels into one value + * + * @param[in] rgb RGB color + */ +CGLM_INLINE +float +glms_luminance(vec3s rgb) { + return glm_luminance(rgb.raw); +} + +#endif /* cglm_colors_h */ diff --git a/include/cglm/structs/curve.h b/include/cglm/structs/curve.h new file mode 100644 index 0000000..724b0be --- /dev/null +++ b/include/cglm/structs/curve.h @@ -0,0 +1,40 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_curves_h +#define cglm_curves_h + +#include "../common.h" +#include "../types-struct.h" +#include "../curve.h" +#include "vec4.h" +#include "mat4.h" + +/*! + * @brief helper function to calculate S*M*C multiplication for curves + * + * This function does not encourage you to use SMC, + * instead it is a helper if you use SMC. + * + * if you want to specify S as vector then use more generic glm_mat4_rmc() func. + * + * Example usage: + * B(s) = glm_smc(s, GLM_BEZIER_MAT, (vec4){p0, c0, c1, p1}) + * + * @param[in] s parameter between 0 and 1 (this will be [s3, s2, s, 1]) + * @param[in] m basis matrix + * @param[in] c position/control vector + * + * @return B(s) + */ +CGLM_INLINE +float +glms_smc(float s, mat4s m, vec4s c) { + return glm_smc(s, m.raw, c.raw); +} + +#endif /* cglm_curves_h */ diff --git a/include/cglm/structs/frustum.h b/include/cglm/structs/frustum.h new file mode 100644 index 0000000..07340c8 --- /dev/null +++ b/include/cglm/structs/frustum.h @@ -0,0 +1,240 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_frustums_h +#define cglm_frustums_h + +#include "../common.h" +#include "../types-struct.h" +#include "../frustum.h" +#include "plane.h" +#include "vec3.h" +#include "vec4.h" +#include "mat4.h" + +/* you can override clip space coords + but you have to provide all with same name + e.g.: define GLM_CSCOORD_LBN {0.0f, 0.0f, 1.0f, 1.0f} */ +#ifndef GLM_CUSTOM_CLIPSPACE + +/* near */ +#define GLMS_CSCOORD_LBN {-1.0f, -1.0f, -1.0f, 1.0f} +#define GLMS_CSCOORD_LTN {-1.0f, 1.0f, -1.0f, 1.0f} +#define GLMS_CSCOORD_RTN { 1.0f, 1.0f, -1.0f, 1.0f} +#define GLMS_CSCOORD_RBN { 1.0f, -1.0f, -1.0f, 1.0f} + +/* far */ +#define GLMS_CSCOORD_LBF {-1.0f, -1.0f, 1.0f, 1.0f} +#define GLMS_CSCOORD_LTF {-1.0f, 1.0f, 1.0f, 1.0f} +#define GLMS_CSCOORD_RTF { 1.0f, 1.0f, 1.0f, 1.0f} +#define GLMS_CSCOORD_RBF { 1.0f, -1.0f, 1.0f, 1.0f} + +#endif + +/*! + * @brief extracts view frustum planes + * + * planes' space: + * 1- if m = proj: View Space + * 2- if m = viewProj: World Space + * 3- if m = MVP: Object Space + * + * You probably want to extract planes in world space so use viewProj as m + * Computing viewProj: + * glm_mat4_mul(proj, view, viewProj); + * + * Exracted planes order: [left, right, bottom, top, near, far] + * + * @param[in] m matrix (see brief) + * @param[out] dest extracted view frustum planes (see brief) + */ +CGLM_INLINE +void +glms_frustum_planes(mat4s m, vec4s dest[6]) { + mat4s t; + + t = glms_mat4_transpose(m); + + dest[0] = glms_vec4_add(t.col[3], t.col[0]); /* left */ + dest[1] = glms_vec4_sub(t.col[3], t.col[0]); /* right */ + dest[2] = glms_vec4_add(t.col[3], t.col[1]); /* bottom */ + dest[3] = glms_vec4_sub(t.col[3], t.col[1]); /* top */ + dest[4] = glms_vec4_add(t.col[3], t.col[2]); /* near */ + dest[5] = glms_vec4_sub(t.col[3], t.col[2]); /* far */ + + dest[0] = glms_plane_normalize(dest[0]); + dest[1] = glms_plane_normalize(dest[1]); + dest[2] = glms_plane_normalize(dest[2]); + dest[3] = glms_plane_normalize(dest[3]); + dest[4] = glms_plane_normalize(dest[4]); + dest[5] = glms_plane_normalize(dest[5]); +} + +/*! + * @brief extracts view frustum corners using clip-space coordinates + * + * corners' space: + * 1- if m = invViewProj: World Space + * 2- if m = invMVP: Object Space + * + * You probably want to extract corners in world space so use invViewProj + * Computing invViewProj: + * glm_mat4_mul(proj, view, viewProj); + * ... + * glm_mat4_inv(viewProj, invViewProj); + * + * if you have a near coord at i index, you can get it's far coord by i + 4 + * + * Find center coordinates: + * for (j = 0; j < 4; j++) { + * glm_vec3_center(corners[i], corners[i + 4], centerCorners[i]); + * } + * + * @param[in] invMat matrix (see brief) + * @param[out] dest exracted view frustum corners (see brief) + */ +CGLM_INLINE +void +glms_frustum_corners(mat4s invMat, vec4s dest[8]) { + vec4s c[8]; + + /* indexOf(nearCoord) = indexOf(farCoord) + 4 */ + vec4s csCoords[8] = { + GLMS_CSCOORD_LBN, + GLMS_CSCOORD_LTN, + GLMS_CSCOORD_RTN, + GLMS_CSCOORD_RBN, + + GLMS_CSCOORD_LBF, + GLMS_CSCOORD_LTF, + GLMS_CSCOORD_RTF, + GLMS_CSCOORD_RBF + }; + + c[0] = glms_mat4_mulv(invMat, csCoords[0]); + c[1] = glms_mat4_mulv(invMat, csCoords[1]); + c[2] = glms_mat4_mulv(invMat, csCoords[2]); + c[3] = glms_mat4_mulv(invMat, csCoords[3]); + c[4] = glms_mat4_mulv(invMat, csCoords[4]); + c[5] = glms_mat4_mulv(invMat, csCoords[5]); + c[6] = glms_mat4_mulv(invMat, csCoords[6]); + c[7] = glms_mat4_mulv(invMat, csCoords[7]); + + dest[0] = glms_vec4_scale(c[0], 1.0f / c[0].z); + dest[1] = glms_vec4_scale(c[1], 1.0f / c[1].z); + dest[2] = glms_vec4_scale(c[2], 1.0f / c[2].z); + dest[3] = glms_vec4_scale(c[3], 1.0f / c[3].z); + dest[4] = glms_vec4_scale(c[4], 1.0f / c[4].z); + dest[5] = glms_vec4_scale(c[5], 1.0f / c[5].z); + dest[6] = glms_vec4_scale(c[6], 1.0f / c[6].z); + dest[7] = glms_vec4_scale(c[7], 1.0f / c[7].z); +} + +/*! + * @brief finds center of view frustum + * + * @param[in] corners view frustum corners + * @returns view frustum center + */ +CGLM_INLINE +vec4s +glms_frustum_center(vec4s corners[8]) { + vec4s center; + + center = glms_vec4_copy(corners[0]); + + center = glms_vec4_add(corners[1], center); + center = glms_vec4_add(corners[2], center); + center = glms_vec4_add(corners[3], center); + center = glms_vec4_add(corners[4], center); + center = glms_vec4_add(corners[5], center); + center = glms_vec4_add(corners[6], center); + center = glms_vec4_add(corners[7], center); + + return glms_vec4_scale(center, 0.125f); +} + +/*! + * @brief finds bounding box of frustum relative to given matrix e.g. view mat + * + * @param[in] corners view frustum corners + * @param[in] m matrix to convert existing conners + * @param[out] box bounding box as array [min, max] + */ +CGLM_INLINE +void +glms_frustum_box(vec4s corners[8], mat4s m, vec3s box[2]) { + vec4s v; + vec3s min, max; + int i; + + min = glms_vec3_broadcast(FLT_MAX); + max = glms_vec3_broadcast(-FLT_MAX); + + for (i = 0; i < 8; i++) { + v = glms_mat4_mulv(m, corners[i]); + + min.x = glm_min(min.x, v.x); + min.y = glm_min(min.y, v.y); + min.z = glm_min(min.z, v.z); + + max.x = glm_max(max.x, v.x); + max.y = glm_max(max.y, v.y); + max.z = glm_max(max.z, v.z); + } + + box[0] = glms_vec3_copy(min); + box[1] = glms_vec3_copy(max); +} + +/*! + * @brief finds planes corners which is between near and far planes (parallel) + * + * this will be helpful if you want to split a frustum e.g. CSM/PSSM. This will + * find planes' corners but you will need to one more plane. + * Actually you have it, it is near, far or created previously with this func ;) + * + * @param[in] corners view frustum corners + * @param[in] splitDist split distance + * @param[in] farDist far distance (zFar) + * @param[out] planeCorners plane corners [LB, LT, RT, RB] + */ +CGLM_INLINE +void +glms_frustum_corners_at(vec4s corners[8], + float splitDist, + float farDist, + vec4s planeCorners[4]) { + vec4s corner; + float dist, sc; + + /* because distance and scale is same for all */ + dist = glms_vec4_distance(corners[GLM_RTF], corners[GLM_RTN]); + sc = dist * (splitDist / farDist); + + /* left bottom */ + corner = glms_vec4_sub(corners[GLM_LBF], corners[GLM_LBN]); + corner = glms_vec4_scale_as(corner, sc); + planeCorners[0] = glms_vec4_add(corners[GLM_LBN], corner); + + /* left top */ + corner = glms_vec4_sub(corners[GLM_LTF], corners[GLM_LTN]); + corner = glms_vec4_scale_as(corner, sc); + planeCorners[1] = glms_vec4_add(corners[GLM_LTN], corner); + + /* right top */ + corner = glms_vec4_sub(corners[GLM_RTF], corners[GLM_RTN]); + corner = glms_vec4_scale_as(corner, sc); + planeCorners[2] = glms_vec4_add(corners[GLM_RTN], corner); + + /* right bottom */ + corner = glms_vec4_sub(corners[GLM_RBF], corners[GLM_RBN]); + corner = glms_vec4_scale_as(corner, sc); + planeCorners[3] = glms_vec4_add(corners[GLM_RBN], corner); +} + +#endif /* cglm_frustums_h */ diff --git a/include/cglm/structs/io.h b/include/cglm/structs/io.h new file mode 100644 index 0000000..c209a47 --- /dev/null +++ b/include/cglm/structs/io.h @@ -0,0 +1,100 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE void glm_mat4_print(mat4 matrix, FILE *ostream); + CGLM_INLINE void glm_mat3_print(mat3 matrix, FILE *ostream); + CGLM_INLINE void glm_vec4_print(vec4 vec, FILE *ostream); + CGLM_INLINE void glm_vec3_print(vec3 vec, FILE *ostream); + CGLM_INLINE void glm_ivec3_print(ivec3 vec, FILE *ostream); + CGLM_INLINE void glm_versor_print(versor vec, FILE *ostream); + */ + +#ifndef cglm_ios_h +#define cglm_ios_h + +#include "../common.h" +#include "../io.h" +#include "mat4.h" + +#include +#include + +CGLM_INLINE +void +glms_mat4_print(mat4s matrix, + FILE * __restrict ostream) { + + glm_mat4_print(matrix.raw, ostream); +} + +CGLM_INLINE +void +glms_mat3_print(mat3s matrix, + FILE * __restrict ostream) { + glm_mat3_print(matrix.raw, ostream); +} + +CGLM_INLINE +void +glms_vec4_print(vec4s vec, + FILE * __restrict ostream) { + glm_vec4_print(vec.raw, ostream); +} + +CGLM_INLINE +void +glms_vec3_print(vec3s vec, + FILE * __restrict ostream) { + glm_vec3_print(vec.raw, ostream); +} + +CGLM_INLINE +void +glms_ivec3_print(ivec3s vec, + FILE * __restrict ostream) { + glm_ivec3_print(vec.raw, ostream); +} + +CGLM_INLINE +void +glms_versor_print(versors vec, + FILE * __restrict ostream) { + glm_versor_print(vec.raw, ostream); +} + +CGLM_INLINE +void +glms_aabb_print(vec3s bbox[2], + const char * __restrict tag, + FILE * __restrict ostream) { + int i, j; + +#define m 3 + + fprintf(ostream, "AABB (%s):\n", tag ? tag : "float"); + + for (i = 0; i < 2; i++) { + fprintf(ostream, "\t|"); + + for (j = 0; j < m; j++) { + fprintf(ostream, "%0.4f", bbox[i].raw[j]); + + if (j != m - 1) + fprintf(ostream, "\t"); + } + + fprintf(ostream, "|\n"); + } + + fprintf(ostream, "\n"); + +#undef m +} + +#endif /* cglm_ios_h */ diff --git a/include/cglm/structs/mat3.h b/include/cglm/structs/mat3.h index 63fe7c6..8072746 100644 --- a/include/cglm/structs/mat3.h +++ b/include/cglm/structs/mat3.h @@ -18,7 +18,6 @@ CGLM_INLINE void glms_mat3_identity_array(mat3s * __restrict mat, size_t count); CGLM_INLINE mat3s glms_mat3_zero(); CGLM_INLINE mat3s glms_mat3_mul(mat3s m1, mat3s m2); - CGLM_INLINE mat3s glms_mat3_transpose_to(mat3s m); CGLM_INLINE ma3s glms_mat3_transpose(mat3s m); CGLM_INLINE vec3s glms_mat3_mulv(mat3s m, vec3s v); CGLM_INLINE float glms_mat3_trace(mat3s m); @@ -140,22 +139,6 @@ glms_mat3_mul(mat3s m1, mat3s m2) { return r; } -/*! - * @brief transpose mat3 and store in dest - * - * source matrix will not be transposed unless dest is m - * - * @param[in] m matrix - * @param[out] dest result - */ -CGLM_INLINE -mat3s -glms_mat3_transpose_to(mat3s m) { - mat3s r; - glm_mat3_transpose_to(m.raw, r.raw); - return r; -} - /*! * @brief tranpose mat3 and store result in same matrix * diff --git a/include/cglm/structs/mat4.h b/include/cglm/structs/mat4.h index a61f18d..72686cd 100644 --- a/include/cglm/structs/mat4.h +++ b/include/cglm/structs/mat4.h @@ -33,7 +33,6 @@ CGLM_INLINE float glms_mat4_trace3(mat4s m); CGLM_INLINE versors glms_mat4_quat(mat4s m); CGLM_INLINE vec3s glms_mat4_mulv3(mat4s m, vec3s v, float last); - CGLM_INLINE mat4s glms_mat4_transpose_to(mat4s m); CGLM_INLINE mat4s glms_mat4_transpose(mat4s m); CGLM_INLINE mat4s glms_mat4_scale_p(mat4s m, float s); CGLM_INLINE mat4s glms_mat4_scale(mat4s m, float s); @@ -54,19 +53,19 @@ #include "vec4.h" #include "vec3.h" -#define GLMS_MAT4_IDENTITY_INIT {{1.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 1.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 1.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 1.0f}} +#define GLMS_MAT4_IDENTITY_INIT {1.0f, 0.0f, 0.0f, 0.0f, \ + 0.0f, 1.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 1.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f, 1.0f} -#define GLMS_MAT4_ZERO_INIT {{0.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 0.0f}} +#define GLMS_MAT4_ZERO_INIT {0.0f, 0.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f, 0.0f, \ + 0.0f, 0.0f, 0.0f, 0.0f} /* for C only */ -#define GLMS_MAT4_IDENTITY ((mat4)GLMS_MAT4_IDENTITY_INIT) -#define GLMS_MAT4_ZERO ((mat4)GLMS_MAT4_ZERO_INIT) +#define GLMS_MAT4_IDENTITY ((mat4s)GLMS_MAT4_IDENTITY_INIT) +#define GLMS_MAT4_ZERO ((mat4s)GLMS_MAT4_ZERO_INIT) /*! * @brief copy all members of [mat] to [dest] @@ -132,7 +131,7 @@ glms_mat4_identity() { CGLM_INLINE void glms_mat4_identity_array(mat4s * __restrict mat, size_t count) { - CGLM_ALIGN_MAT mat4 t = GLM_MAT4_IDENTITY_INIT; + CGLM_ALIGN_MAT mat4s t = GLMS_MAT4_IDENTITY_INIT; size_t i; for (i = 0; i < count; i++) { @@ -229,20 +228,25 @@ glms_mat4_mul(mat4s m1, mat4s m2) { * @code * mat m1, m2, m3, m4, res; * - * glm_mat4_mulN((mat4 *[]){&m1, &m2, &m3, &m4}, 4, res); + * res = glm_mat4_mulN((mat4 *[]){&m1, &m2, &m3, &m4}, 4); * @endcode * * @warning matrices parameter is pointer array not mat4 array! * * @param[in] matrices mat4 * array * @param[in] len matrices count - * @returns result + * @returns result matrix */ CGLM_INLINE mat4s glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len) { - mat4s r; - glm_mat4_mulN(matrices, len, r.raw); + CGLM_ALIGN_MAT mat4s r = GLMS_MAT4_IDENTITY_INIT; + uint32_t i; + + for (i = 0; i < len; i++) { + r = glms_mat4_mul(r, *matrices[i]); + } + return r; } @@ -317,22 +321,6 @@ glms_mat4_mulv3(mat4s m, vec3s v, float last) { return r; } -/*! - * @brief transpose mat4 and store in dest - * - * source matrix will not be transposed unless dest is m - * - * @param[in] m matrix - * @returns result - */ -CGLM_INLINE -mat4s -glms_mat4_transpose_to(mat4s m) { - mat4s r; - glm_mat4_transpose_to(m.raw, r.raw); - return r; -} - /*! * @brief tranpose mat4 and store result in same matrix * diff --git a/include/cglm/structs/plane.h b/include/cglm/structs/plane.h new file mode 100644 index 0000000..6e23482 --- /dev/null +++ b/include/cglm/structs/plane.h @@ -0,0 +1,40 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_planes_h +#define cglm_planes_h + +#include "../common.h" +#include "../types-struct.h" +#include "../plane.h" +#include "vec4.h" + +/* + Plane equation: Ax + By + Cz + D = 0; + + It stored in vec4 as [A, B, C, D]. (A, B, C) is normal and D is distance +*/ + +/* + Functions: + CGLM_INLINE vec4s glms_plane_normalize(vec4s plane); + */ + +/*! + * @brief normalizes a plane + * + * @param[in] plane plane to normalize + * @returns normalized plane + */ +CGLM_INLINE +vec4s +glms_plane_normalize(vec4s plane) { + glm_plane_normalize(plane.raw); + return plane; +} + +#endif /* cglm_planes_h */ diff --git a/include/cglm/structs/project.h b/include/cglm/structs/project.h new file mode 100644 index 0000000..1cbb137 --- /dev/null +++ b/include/cglm/structs/project.h @@ -0,0 +1,104 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_projects_h +#define cglm_projects_h + +#include "../common.h" +#include "../types-struct.h" +#include "../project.h" +#include "vec3.h" +#include "vec4.h" +#include "mat4.h" + +/*! + * @brief maps the specified viewport coordinates into specified space [1] + * the matrix should contain projection matrix. + * + * if you don't have ( and don't want to have ) an inverse matrix then use + * glm_unproject version. You may use existing inverse of matrix in somewhere + * else, this is why glm_unprojecti exists to save save inversion cost + * + * [1] space: + * 1- if m = invProj: View Space + * 2- if m = invViewProj: World Space + * 3- if m = invMVP: Object Space + * + * You probably want to map the coordinates into object space + * so use invMVP as m + * + * Computing viewProj: + * glm_mat4_mul(proj, view, viewProj); + * glm_mat4_mul(viewProj, model, MVP); + * glm_mat4_inv(viewProj, invMVP); + * + * @param[in] pos point/position in viewport coordinates + * @param[in] invMat matrix (see brief) + * @param[in] vp viewport as [x, y, width, height] + * @returns unprojected coordinates + */ +CGLM_INLINE +vec3s +glms_unprojecti(vec3s pos, mat4s invMat, vec4s vp) { + vec3s r; + glm_unprojecti(pos.raw, invMat.raw, vp.raw, r.raw); + return r; +} + +/*! + * @brief maps the specified viewport coordinates into specified space [1] + * the matrix should contain projection matrix. + * + * this is same as glm_unprojecti except this function get inverse matrix for + * you. + * + * [1] space: + * 1- if m = proj: View Space + * 2- if m = viewProj: World Space + * 3- if m = MVP: Object Space + * + * You probably want to map the coordinates into object space + * so use MVP as m + * + * Computing viewProj and MVP: + * glm_mat4_mul(proj, view, viewProj); + * glm_mat4_mul(viewProj, model, MVP); + * + * @param[in] pos point/position in viewport coordinates + * @param[in] m matrix (see brief) + * @param[in] vp viewport as [x, y, width, height] + * @returns unprojected coordinates + */ +CGLM_INLINE +vec3s +glms_unproject(vec3s pos, mat4s m, vec4s vp) { + vec3s r; + glm_unproject(pos.raw, m.raw, vp.raw, r.raw); + return r; +} + +/*! + * @brief map object coordinates to window coordinates + * + * Computing MVP: + * glm_mat4_mul(proj, view, viewProj); + * glm_mat4_mul(viewProj, model, MVP); + * + * @param[in] pos object coordinates + * @param[in] m MVP matrix + * @param[in] vp viewport as [x, y, width, height] + * @param[out] dest projected coordinates + */ +CGLM_INLINE +vec3s +glms_project(vec3s pos, mat4s m, vec4s vp) { + vec3s r; + glm_project(pos.raw, m.raw, vp.raw, r.raw); + return r; +} + +#endif /* cglm_projects_h */ diff --git a/include/cglm/structs/sphere.h b/include/cglm/structs/sphere.h index b336c3b..164e8bc 100644 --- a/include/cglm/structs/sphere.h +++ b/include/cglm/structs/sphere.h @@ -10,8 +10,8 @@ #include "../common.h" #include "../types-struct.h" -#include "../mat4.h" #include "../sphere.h" +#include "mat4.h" /* Sphere Representation in cglm: [center.x, center.y, center.z, radii] diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h index 13431aa..2d54aa4 100644 --- a/include/cglm/types-struct.h +++ b/include/cglm/types-struct.h @@ -21,6 +21,17 @@ typedef union vec3s { vec3 raw; } vec3s; +typedef union ivec3s { +#ifndef CGLM_NO_ANONYMOUS_STRUCT + struct { + int x; + int y; + int z; + }; +#endif + ivec3 raw; +} ivec3s; + typedef union CGLM_ALIGN_IF(16) vec4s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { @@ -38,15 +49,9 @@ typedef vec4s versors; typedef union mat3s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { - float m00; - float m01; - float m02; - float m10; - float m11; - float m12; - float m20; - float m21; - float m22; + float m00, m01, m02; + float m10, m11, m12; + float m20, m21, m22; }; struct { vec3s col0; @@ -54,32 +59,17 @@ typedef union mat3s { vec3s col2; }; #endif + vec3s col[3]; mat3 raw; } mat3s; -#ifdef __AVX__ -typedef union CGLM_ALIGN_IF(32) mat4s { -#else -typedef union CGLM_ALIGN_IF(16) mat4s { -#endif +typedef union CGLM_ALIGN_MAT mat4s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { - float m00; - float m01; - float m02; - float m03; - float m10; - float m11; - float m12; - float m13; - float m20; - float m21; - float m22; - float m23; - float m30; - float m31; - float m32; - float m33; + float m00, m01, m02, m03; + float m10, m11, m12, m13; + float m20, m21, m22, m23; + float m30, m31, m32, m33; }; struct { vec4s col0; @@ -88,6 +78,7 @@ typedef union CGLM_ALIGN_IF(16) mat4s { vec4s col3; }; #endif + vec4s col[4]; mat4 raw; } mat4s; From 1d1bf8e91a63d8d4c6d269f4ad7526e80df366dc Mon Sep 17 00:00:00 2001 From: acoto87 Date: Thu, 16 May 2019 17:03:55 -0500 Subject: [PATCH 08/11] - Change the approach implementation of several functions - Added `glms_vec4_pack` and `glms_vec4_unpack` to pack and unpack arrays of `vec4s`. - Added `glms_vec3_pack` and `glms_vec3_unpack` to pack and unpack arrays of `vec3s`. - Fixes in functions that accumulates in one parameter - --- include/cglm/structs/affine.h | 16 ++-- include/cglm/structs/box.h | 84 ++++++++------------- include/cglm/structs/frustum.h | 131 ++++++--------------------------- include/cglm/structs/io.h | 24 +----- include/cglm/structs/mat4.h | 2 +- include/cglm/structs/vec3.h | 118 ++++++++++++----------------- include/cglm/structs/vec4.h | 73 ++++++++++++------ 7 files changed, 160 insertions(+), 288 deletions(-) diff --git a/include/cglm/structs/affine.h b/include/cglm/structs/affine.h index 6f8be48..72cab13 100644 --- a/include/cglm/structs/affine.h +++ b/include/cglm/structs/affine.h @@ -286,15 +286,9 @@ glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis) { CGLM_INLINE vec3s glms_decompose_scalev(mat4s m) { - mat3s m3x3; - vec3s s; - - m3x3 = glms_mat4_pick3(m); - - s.x = glms_vec3_norm(m3x3.col[0]); - s.y = glms_vec3_norm(m3x3.col[1]); - s.z = glms_vec3_norm(m3x3.col[2]); - return s; + vec3s r; + glm_decompose_scalev(m.raw, r.raw); + return r; } /*! @@ -322,6 +316,8 @@ glms_uniscaled(mat4s m) { CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s r, vec3s s) { + // FIX: Modify param + // glm_decompose_rs(m.raw, r.raw, s.raw); } @@ -337,6 +333,8 @@ glms_decompose_rs(mat4s m, mat4s r, vec3s s) { CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s r, vec3s s) { + // FIX: Modify param + // glm_decompose(m.raw, t.raw, r.raw, s.raw); } diff --git a/include/cglm/structs/box.h b/include/cglm/structs/box.h index e9c813f..ca7c3a6 100644 --- a/include/cglm/structs/box.h +++ b/include/cglm/structs/box.h @@ -25,34 +25,12 @@ CGLM_INLINE void glms_aabb_transform(vec3s box[2], mat4s m, vec3s dest[2]) { - vec3s v[2], xa, xb, ya, yb, za, zb; - mat3s m3x3; + vec3 rawBox[2]; + vec3 rawDest[2]; - m3x3 = glms_mat4_pick3(m); - - xa = glms_vec3_scale(m3x3.col[0], box[0].x); - xb = glms_vec3_scale(m3x3.col[0], box[1].x); - - ya = glms_vec3_scale(m3x3.col[1], box[0].y); - yb = glms_vec3_scale(m3x3.col[1], box[1].y); - - za = glms_vec3_scale(m3x3.col[2], box[0].z); - zb = glms_vec3_scale(m3x3.col[2], box[1].z); - - /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */ - v[0] = m3x3.col[3]; - v[0] = glms_vec3_minadd(xa, xb); - v[0] = glms_vec3_minadd(ya, yb); - v[0] = glms_vec3_minadd(za, zb); - - /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ - v[1] = m3x3.col[3]; - v[1] = glms_vec3_maxadd(xa, xb); - v[1] = glms_vec3_maxadd(ya, yb); - v[1] = glms_vec3_maxadd(za, zb); - - dest[0] = glms_vec3_copy(v[0]); - dest[1] = glms_vec3_copy(v[1]); + glms_vec3_unpack(rawBox, box, 2); + glm_aabb_transform(rawBox, m.raw, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -68,13 +46,14 @@ glms_aabb_transform(vec3s box[2], mat4s m, vec3s dest[2]) { CGLM_INLINE void glms_aabb_merge(vec3s box1[2], vec3s box2[2], vec3s dest[2]) { - dest[0].x = glm_min(box1[0].x, box2[0].x); - dest[0].y = glm_min(box1[0].y, box2[0].y); - dest[0].z = glm_min(box1[0].z, box2[0].z); + vec3 rawBox1[2]; + vec3 rawBox2[2]; + vec3 rawDest[2]; - dest[1].x = glm_max(box1[1].x, box2[1].x); - dest[1].y = glm_max(box1[1].y, box2[1].y); - dest[1].z = glm_max(box1[1].z, box2[1].z); + glms_vec3_unpack(rawBox1, box1, 2); + glms_vec3_unpack(rawBox2, box2, 2); + glm_aabb_merge(rawBox1, rawBox2, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -91,13 +70,14 @@ glms_aabb_merge(vec3s box1[2], vec3s box2[2], vec3s dest[2]) { CGLM_INLINE void glms_aabb_crop(vec3s box[2], vec3s cropBox[2], vec3s dest[2]) { - dest[0].x = glm_max(box[0].x, cropBox[0].x); - dest[0].y = glm_max(box[0].y, cropBox[0].y); - dest[0].z = glm_max(box[0].z, cropBox[0].z); + vec3 rawBox[2]; + vec3 rawCropBox[2]; + vec3 rawDest[2]; - dest[1].x = glm_min(box[1].x, cropBox[1].x); - dest[1].y = glm_min(box[1].y, cropBox[1].y); - dest[1].z = glm_min(box[1].z, cropBox[1].z); + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawCropBox, cropBox, 2); + glm_aabb_crop(rawBox, rawCropBox, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -139,21 +119,12 @@ glms_aabb_crop_until(vec3s box[2], CGLM_INLINE bool glms_aabb_frustum(vec3s box[2], vec4s planes[6]) { - vec4s p; - float dp; - int i; + vec3 rawBox[2]; + vec4 rawPlanes[6]; - for (i = 0; i < 6; i++) { - p = planes[i]; - dp = p.x * box[p.x > 0.0f].x - + p.y * box[p.y > 0.0f].y - + p.z * box[p.z > 0.0f].z; - - if (dp < -p.w) - return false; - } - - return true; + glms_vec3_unpack(rawBox, box, 2); + glms_vec4_unpack(rawPlanes, planes, 6); + return glm_aabb_frustum(rawBox, rawPlanes); } /*! @@ -164,6 +135,8 @@ glms_aabb_frustum(vec3s box[2], vec4s planes[6]) { CGLM_INLINE void glms_aabb_invalidate(vec3s box[2]) { + // FIX: Modify param + // box[0] = glms_vec3_broadcast(FLT_MAX); box[1] = glms_vec3_broadcast(-FLT_MAX); } @@ -176,8 +149,9 @@ glms_aabb_invalidate(vec3s box[2]) { CGLM_INLINE bool glms_aabb_isvalid(vec3s box[2]) { - return glms_vec3_max(box[0]) != FLT_MAX && - glms_vec3_min(box[1]) != -FLT_MAX; + vec3 rawBox[2]; + glms_vec3_unpack(rawBox, box, 2); + return glm_aabb_isvalid(rawBox); } /*! diff --git a/include/cglm/structs/frustum.h b/include/cglm/structs/frustum.h index 07340c8..56bd165 100644 --- a/include/cglm/structs/frustum.h +++ b/include/cglm/structs/frustum.h @@ -55,23 +55,9 @@ CGLM_INLINE void glms_frustum_planes(mat4s m, vec4s dest[6]) { - mat4s t; - - t = glms_mat4_transpose(m); - - dest[0] = glms_vec4_add(t.col[3], t.col[0]); /* left */ - dest[1] = glms_vec4_sub(t.col[3], t.col[0]); /* right */ - dest[2] = glms_vec4_add(t.col[3], t.col[1]); /* bottom */ - dest[3] = glms_vec4_sub(t.col[3], t.col[1]); /* top */ - dest[4] = glms_vec4_add(t.col[3], t.col[2]); /* near */ - dest[5] = glms_vec4_sub(t.col[3], t.col[2]); /* far */ - - dest[0] = glms_plane_normalize(dest[0]); - dest[1] = glms_plane_normalize(dest[1]); - dest[2] = glms_plane_normalize(dest[2]); - dest[3] = glms_plane_normalize(dest[3]); - dest[4] = glms_plane_normalize(dest[4]); - dest[5] = glms_plane_normalize(dest[5]); + vec4 rawDest[6]; + glm_frustum_planes(m.raw, rawDest); + glms_vec4_pack(dest, rawDest, 6); } /*! @@ -100,38 +86,9 @@ glms_frustum_planes(mat4s m, vec4s dest[6]) { CGLM_INLINE void glms_frustum_corners(mat4s invMat, vec4s dest[8]) { - vec4s c[8]; - - /* indexOf(nearCoord) = indexOf(farCoord) + 4 */ - vec4s csCoords[8] = { - GLMS_CSCOORD_LBN, - GLMS_CSCOORD_LTN, - GLMS_CSCOORD_RTN, - GLMS_CSCOORD_RBN, - - GLMS_CSCOORD_LBF, - GLMS_CSCOORD_LTF, - GLMS_CSCOORD_RTF, - GLMS_CSCOORD_RBF - }; - - c[0] = glms_mat4_mulv(invMat, csCoords[0]); - c[1] = glms_mat4_mulv(invMat, csCoords[1]); - c[2] = glms_mat4_mulv(invMat, csCoords[2]); - c[3] = glms_mat4_mulv(invMat, csCoords[3]); - c[4] = glms_mat4_mulv(invMat, csCoords[4]); - c[5] = glms_mat4_mulv(invMat, csCoords[5]); - c[6] = glms_mat4_mulv(invMat, csCoords[6]); - c[7] = glms_mat4_mulv(invMat, csCoords[7]); - - dest[0] = glms_vec4_scale(c[0], 1.0f / c[0].z); - dest[1] = glms_vec4_scale(c[1], 1.0f / c[1].z); - dest[2] = glms_vec4_scale(c[2], 1.0f / c[2].z); - dest[3] = glms_vec4_scale(c[3], 1.0f / c[3].z); - dest[4] = glms_vec4_scale(c[4], 1.0f / c[4].z); - dest[5] = glms_vec4_scale(c[5], 1.0f / c[5].z); - dest[6] = glms_vec4_scale(c[6], 1.0f / c[6].z); - dest[7] = glms_vec4_scale(c[7], 1.0f / c[7].z); + vec4 rawDest[8]; + glm_frustum_corners(invMat.raw, rawDest); + glms_vec4_pack(dest, rawDest, 8); } /*! @@ -143,19 +100,12 @@ glms_frustum_corners(mat4s invMat, vec4s dest[8]) { CGLM_INLINE vec4s glms_frustum_center(vec4s corners[8]) { - vec4s center; + vec4 rawCorners[8]; + vec4s r; - center = glms_vec4_copy(corners[0]); - - center = glms_vec4_add(corners[1], center); - center = glms_vec4_add(corners[2], center); - center = glms_vec4_add(corners[3], center); - center = glms_vec4_add(corners[4], center); - center = glms_vec4_add(corners[5], center); - center = glms_vec4_add(corners[6], center); - center = glms_vec4_add(corners[7], center); - - return glms_vec4_scale(center, 0.125f); + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_center(rawCorners, r.raw); + return r; } /*! @@ -168,27 +118,12 @@ glms_frustum_center(vec4s corners[8]) { CGLM_INLINE void glms_frustum_box(vec4s corners[8], mat4s m, vec3s box[2]) { - vec4s v; - vec3s min, max; - int i; + vec4 rawCorners[8]; + vec3 rawBox[2]; - min = glms_vec3_broadcast(FLT_MAX); - max = glms_vec3_broadcast(-FLT_MAX); - - for (i = 0; i < 8; i++) { - v = glms_mat4_mulv(m, corners[i]); - - min.x = glm_min(min.x, v.x); - min.y = glm_min(min.y, v.y); - min.z = glm_min(min.z, v.z); - - max.x = glm_max(max.x, v.x); - max.y = glm_max(max.y, v.y); - max.z = glm_max(max.z, v.z); - } - - box[0] = glms_vec3_copy(min); - box[1] = glms_vec3_copy(max); + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_box(rawCorners, m.raw, rawBox); + glms_vec3_pack(box, rawBox, 2); } /*! @@ -205,36 +140,16 @@ glms_frustum_box(vec4s corners[8], mat4s m, vec3s box[2]) { */ CGLM_INLINE void -glms_frustum_corners_at(vec4s corners[8], +glms_frustum_corners_at(vec4s corners[8], float splitDist, float farDist, - vec4s planeCorners[4]) { - vec4s corner; - float dist, sc; + vec4s planeCorners[4]) { + vec4 rawCorners[8]; + vec4 rawPlaneCorners[4]; - /* because distance and scale is same for all */ - dist = glms_vec4_distance(corners[GLM_RTF], corners[GLM_RTN]); - sc = dist * (splitDist / farDist); - - /* left bottom */ - corner = glms_vec4_sub(corners[GLM_LBF], corners[GLM_LBN]); - corner = glms_vec4_scale_as(corner, sc); - planeCorners[0] = glms_vec4_add(corners[GLM_LBN], corner); - - /* left top */ - corner = glms_vec4_sub(corners[GLM_LTF], corners[GLM_LTN]); - corner = glms_vec4_scale_as(corner, sc); - planeCorners[1] = glms_vec4_add(corners[GLM_LTN], corner); - - /* right top */ - corner = glms_vec4_sub(corners[GLM_RTF], corners[GLM_RTN]); - corner = glms_vec4_scale_as(corner, sc); - planeCorners[2] = glms_vec4_add(corners[GLM_RTN], corner); - - /* right bottom */ - corner = glms_vec4_sub(corners[GLM_RBF], corners[GLM_RBN]); - corner = glms_vec4_scale_as(corner, sc); - planeCorners[3] = glms_vec4_add(corners[GLM_RBN], corner); + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_corners_at(rawCorners, splitDist, farDist, rawPlaneCorners); + glms_vec4_pack(planeCorners, rawPlaneCorners, 8); } #endif /* cglm_frustums_h */ diff --git a/include/cglm/structs/io.h b/include/cglm/structs/io.h index c209a47..2467d20 100644 --- a/include/cglm/structs/io.h +++ b/include/cglm/structs/io.h @@ -73,28 +73,10 @@ void glms_aabb_print(vec3s bbox[2], const char * __restrict tag, FILE * __restrict ostream) { - int i, j; + vec3 rawBbox[2]; -#define m 3 - - fprintf(ostream, "AABB (%s):\n", tag ? tag : "float"); - - for (i = 0; i < 2; i++) { - fprintf(ostream, "\t|"); - - for (j = 0; j < m; j++) { - fprintf(ostream, "%0.4f", bbox[i].raw[j]); - - if (j != m - 1) - fprintf(ostream, "\t"); - } - - fprintf(ostream, "|\n"); - } - - fprintf(ostream, "\n"); - -#undef m + glms_vec3_unpack(rawBbox, bbox, 2); + glm_aabb_print(rawBbox, tag, ostream); } #endif /* cglm_ios_h */ diff --git a/include/cglm/structs/mat4.h b/include/cglm/structs/mat4.h index 72686cd..40c7291 100644 --- a/include/cglm/structs/mat4.h +++ b/include/cglm/structs/mat4.h @@ -241,7 +241,7 @@ CGLM_INLINE mat4s glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len) { CGLM_ALIGN_MAT mat4s r = GLMS_MAT4_IDENTITY_INIT; - uint32_t i; + size_t i; for (i = 0; i < len; i++) { r = glms_mat4_mul(r, *matrices[i]); diff --git a/include/cglm/structs/vec3.h b/include/cglm/structs/vec3.h index ed77c4a..312d59a 100644 --- a/include/cglm/structs/vec3.h +++ b/include/cglm/structs/vec3.h @@ -40,6 +40,30 @@ glms_vec3_copy(vec3s v) { return r; } +CGLM_INLINE +void +glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { + size_t i; + + for (i = 0; i < len; i++) { + dst[i].x = src[i][0]; + dst[i].y = src[i][1]; + dst[i].z = src[i][2]; + } +} + +CGLM_INLINE +void +glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len) { + size_t i; + + for (i = 0; i < len; i++) { + dst[i][0] = src[i].x; + dst[i][1] = src[i].y; + dst[i][2] = src[i].z; + } +} + CGLM_INLINE vec3s glms_vec3_zero() { @@ -65,13 +89,13 @@ glms_vec3_dot(vec3s a, vec3s b) { CGLM_INLINE float glms_vec3_norm2(vec3s v) { - glm_vec3_norm2(v.raw); + return glm_vec3_norm2(v.raw); } CGLM_INLINE float glms_vec3_norm(vec3s v) { - glm_vec3_norm(v.raw); + return glm_vec3_norm(v.raw); } CGLM_INLINE @@ -148,50 +172,44 @@ glms_vec3_divs(vec3s a, float s) { CGLM_INLINE vec3s -glms_vec3_addadd(vec3s a, vec3s b) { - vec3s r; - glm_vec3_addadd(a.raw, b.raw, r.raw); - return r; +glms_vec3_addadd(vec3s a, vec3s b, vec3s dest) { + glm_vec3_addadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec3s -glms_vec3_subadd(vec3s a, vec3s b) { - vec3s r; - glm_vec3_subadd(a.raw, b.raw, r.raw); - return r; +glms_vec3_subadd(vec3s a, vec3s b, vec3s dest) { + glm_vec3_subadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec3s -glms_vec3_muladd(vec3s a, vec3s b) { - vec3s r; - glm_vec3_muladd(a.raw, b.raw, r.raw); - return r; +glms_vec3_muladd(vec3s a, vec3s b, vec3s dest) { + glm_vec3_muladd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec3s -glms_vec3_muladds(vec3s a, float s) { - vec3s r; - glm_vec3_muladds(a.raw, s, r.raw); - return r; +glms_vec3_muladds(vec3s a, float s, vec3s dest) { + glm_vec3_muladds(a.raw, s, dest.raw); + return dest; } CGLM_INLINE vec3s -glms_vec3_maxadd(vec3s a, vec3s b) { - vec3s r; - glm_vec3_maxadd(a.raw, b.raw, r.raw); - return r; +glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest) { + glm_vec3_maxadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec3s -glms_vec3_minadd(vec3s a, vec3s b) { - vec3s r; - glm_vec3_minadd(a.raw, b.raw, r.raw); - return r; +glms_vec3_minadd(vec3s a, vec3s b, vec3s dest) { + glm_vec3_minadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE @@ -201,22 +219,6 @@ glms_vec3_flipsign(vec3s v) { return v; } -CGLM_INLINE -vec3s -glms_vec3_flipsign_to(vec3s v) { - vec3s r; - glm_vec3_flipsign_to(v.raw, r.raw); - return r; -} - -CGLM_INLINE -vec3s -glms_vec3_negate_to(vec3s v) { - vec3s r; - glm_vec3_negate_to(v.raw, r.raw); - return r; -} - CGLM_INLINE vec3s glms_vec3_negate(vec3s v) { @@ -231,14 +233,6 @@ glms_vec3_inv(vec3s v) { return v; } -CGLM_INLINE -vec3s -glms_vec3_inv_to(vec3s v) { - vec3s r; - glm_vec3_inv_to(v.raw, r.raw); - return r; -} - CGLM_INLINE vec3s glms_vec3_normalize(vec3s v) { @@ -246,14 +240,6 @@ glms_vec3_normalize(vec3s v) { return v; } -CGLM_INLINE -vec3s -glms_vec3_normalize_to(vec3s v) { - vec3s r; - glm_vec3_normalize_to(v.raw, r.raw); - return r; -} - CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b) { @@ -291,17 +277,17 @@ glms_vec3_rotate(vec3s v, float angle, vec3s axis) { CGLM_INLINE vec3s -glms_vec3_rotate_m4(mat4 m, vec3s v) { +glms_vec3_rotate_m4(mat4s m, vec3s v) { vec3s r; - glm_vec3_rotate_m4(m, v.raw, r.raw); + glm_vec3_rotate_m4(m.raw, v.raw, r.raw); return r; } CGLM_INLINE vec3s -glms_vec3_rotate_m3(mat3 m, vec3s v) { +glms_vec3_rotate_m3(mat3s m, vec3s v) { vec3s r; - glm_vec3_rotate_m3(m, v.raw, r.raw); + glm_vec3_rotate_m3(m.raw, v.raw, r.raw); return r; } @@ -387,12 +373,4 @@ glms_normalize(vec3s v) { return v; } -CGLM_INLINE -vec3s -glms_normalize_to(vec3s v) { - vec3s r; - glm_normalize_to(v.raw, r.raw); - return r; -} - #endif /* cglm_vec3s_h */ diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index c8ec509..956fee1 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -54,6 +54,29 @@ glms_vec4_ucopy(vec4s v) { return r; } +CGLM_INLINE +void +glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { + size_t i; + + for (i = 0; i < len; i++) { + dst[i].x = src[i][0]; + dst[i].y = src[i][1]; + dst[i].z = src[i][2]; + } +} + +CGLM_INLINE +void +glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len) { + size_t i; + + for (i = 0; i < len; i++) { + dst[i][0] = src[i].x; + dst[i][1] = src[i].y; + dst[i][2] = src[i].z; + } +} CGLM_INLINE float @@ -147,50 +170,44 @@ glms_vec4_divs(vec4s v, float s) { CGLM_INLINE vec4s -glms_vec4_addadd(vec4s a, vec4s b) { - vec4s r; - glm_vec4_addadd(a.raw, b.raw, r.raw); - return r; +glms_vec4_addadd(vec4s a, vec4s b, vec4s dest) { + glm_vec4_addadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec4s -glms_vec4_subadd(vec4s a, vec4s b) { - vec4s r; - glm_vec4_subadd(a.raw, b.raw, r.raw); - return r; +glms_vec4_subadd(vec4s a, vec4s b, vec4s dest) { + glm_vec4_subadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec4s -glms_vec4_muladd(vec4s a, vec4s b) { - vec4s r; - glm_vec4_muladd(a.raw, b.raw, r.raw); - return r; +glms_vec4_muladd(vec4s a, vec4s b, vec4s dest) { + glm_vec4_muladd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec4s -glms_vec4_muladds(vec4s a, float s) { - vec4s r; - glm_vec4_muladds(a.raw, s, r.raw); - return r; +glms_vec4_muladds(vec4s a, float s, vec4s dest) { + glm_vec4_muladds(a.raw, s, dest.raw); + return dest; } CGLM_INLINE vec4s -glms_vec4_maxadd(vec4s a, vec4s b) { - vec4s r; - glm_vec4_maxadd(a.raw, b.raw, r.raw); - return r; +glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest) { + glm_vec4_maxadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE vec4s -glms_vec4_minadd(vec4s a, vec4s b) { - vec4s r; - glm_vec4_minadd(a.raw, b.raw, r.raw); - return r; +glms_vec4_minadd(vec4s a, vec4s b, vec4s dest) { + glm_vec4_minadd(a.raw, b.raw, dest.raw); + return dest; } CGLM_INLINE @@ -251,4 +268,12 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) { return r; } +CGLM_INLINE +vec4s +glms_vec4_cubic(float s) { + vec4s r; + glm_vec4_cubic(s, r.raw); + return r; +} + #endif /* cglm_vec4s_h */ From fd3b0634d273929dc4dc64ad4f7617e9f3910708 Mon Sep 17 00:00:00 2001 From: acoto87 Date: Mon, 20 May 2019 17:11:10 -0500 Subject: [PATCH 09/11] Change for function that output multiple values - Now the functions that output mutliple values, such as glms_decompose_rs and glms_decompose receive pointers. - Added missing comments to struct/vec3 and struct/vec4 files. --- include/cglm/structs/affine.h | 16 +- include/cglm/structs/box.h | 45 ++-- include/cglm/structs/vec3-ext.h | 98 ++++++++ include/cglm/structs/vec3.h | 422 +++++++++++++++++++++++++++++++- include/cglm/structs/vec4-ext.h | 98 ++++++++ include/cglm/structs/vec4.h | 321 +++++++++++++++++++++++- 6 files changed, 947 insertions(+), 53 deletions(-) diff --git a/include/cglm/structs/affine.h b/include/cglm/structs/affine.h index 72cab13..481aaa3 100644 --- a/include/cglm/structs/affine.h +++ b/include/cglm/structs/affine.h @@ -25,8 +25,8 @@ CGLM_INLINE mat4s glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis); CGLM_INLINE vec3s glms_decompose_scalev(mat4s m); CGLM_INLINE bool glms_uniscaled(mat4s m); - CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s r, vec3s s); - CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s r, vec3s s); + CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s * r, vec3s * s); + CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s * r, vec3s * s); */ #ifndef cglm_affines_h @@ -315,10 +315,8 @@ glms_uniscaled(mat4s m) { */ CGLM_INLINE void -glms_decompose_rs(mat4s m, mat4s r, vec3s s) { - // FIX: Modify param - // - glm_decompose_rs(m.raw, r.raw, s.raw); +glms_decompose_rs(mat4s m, mat4s * __restrict r, vec3s * __restrict s) { + glm_decompose_rs(m.raw, r->raw, s->raw); } /*! @@ -332,10 +330,8 @@ glms_decompose_rs(mat4s m, mat4s r, vec3s s) { */ CGLM_INLINE void -glms_decompose(mat4s m, vec4s t, mat4s r, vec3s s) { - // FIX: Modify param - // - glm_decompose(m.raw, t.raw, r.raw, s.raw); +glms_decompose(mat4s m, vec4s * __restrict t, mat4s * __restrict r, vec3s * __restrict s) { + glm_decompose(m.raw, t->raw, r->raw, s->raw); } #endif /* cglm_affines_h */ diff --git a/include/cglm/structs/box.h b/include/cglm/structs/box.h index ca7c3a6..4251bd7 100644 --- a/include/cglm/structs/box.h +++ b/include/cglm/structs/box.h @@ -162,7 +162,7 @@ glms_aabb_isvalid(vec3s box[2]) { CGLM_INLINE float glms_aabb_size(vec3s box[2]) { - return glms_vec3_distance(box[0], box[1]); + return glm_vec3_distance(box[0].raw, box[1].raw); } /*! @@ -197,9 +197,12 @@ glms_aabb_center(vec3s box[2]) { CGLM_INLINE bool glms_aabb_aabb(vec3s box[2], vec3s other[2]) { - return (box[0].x <= other[1].x && box[1].x >= other[0].x) - && (box[0].y <= other[1].y && box[1].y >= other[0].y) - && (box[0].z <= other[1].z && box[1].z >= other[0].z); + vec3 rawBox[2]; + vec3 rawOther[2]; + + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawOther, other, 2); + return glm_aabb_aabb(rawBox, rawOther); } /*! @@ -213,19 +216,11 @@ glms_aabb_aabb(vec3s box[2], vec3s other[2]) { */ CGLM_INLINE bool -glms_aabb_sphere(vec3s box[2], vec4 s) { - float dmin; - int a, b, c; - - a = s[0] >= box[0].x; - b = s[1] >= box[0].y; - c = s[2] >= box[0].z; - - dmin = glm_pow2(s[0] - box[a].x) - + glm_pow2(s[1] - box[b].y) - + glm_pow2(s[2] - box[c].z); - - return dmin <= glm_pow2(s[3]); +glms_aabb_sphere(vec3s box[2], vec4s s) { + vec3 rawBox[2]; + + glms_vec3_unpack(rawBox, box, 2); + return glm_aabb_sphere(rawBox, s.raw); } /*! @@ -237,9 +232,10 @@ glms_aabb_sphere(vec3s box[2], vec4 s) { CGLM_INLINE bool glms_aabb_point(vec3s box[2], vec3s point) { - return (point.x >= box[0].x && point.x <= box[1].x) - && (point.y >= box[0].y && point.y <= box[1].y) - && (point.z >= box[0].z && point.z <= box[1].z); + vec3 rawBox[2]; + + glms_vec3_unpack(rawBox, box, 2); + return glm_aabb_point(rawBox, point.raw); } /*! @@ -251,9 +247,12 @@ glms_aabb_point(vec3s box[2], vec3s point) { CGLM_INLINE bool glms_aabb_contains(vec3s box[2], vec3s other[2]) { - return (box[0].x <= other[0].x && box[1].x >= other[1].x) - && (box[0].y <= other[0].y && box[1].y >= other[1].y) - && (box[0].z <= other[0].z && box[1].z >= other[1].z); + vec3 rawBox[2]; + vec3 rawOther[2]; + + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawOther, other, 2); + return glm_aabb_contains(rawBox, rawOther); } #endif /* cglm_boxs_h */ diff --git a/include/cglm/structs/vec3-ext.h b/include/cglm/structs/vec3-ext.h index 0449b3d..2e5e729 100644 --- a/include/cglm/structs/vec3-ext.h +++ b/include/cglm/structs/vec3-ext.h @@ -5,6 +5,27 @@ * Full license can be found in the LICENSE file */ +/*! + * @brief SIMD like functions + */ + +/* + Functions: + CGLM_INLINE vec3s glms_vec3_broadcast(float val); + CGLM_INLINE bool glms_vec3_eq(vec3s v, float val); + CGLM_INLINE bool glms_vec3_eq_eps(vec3s v, float val); + CGLM_INLINE bool glms_vec3_eq_all(vec3s v); + CGLM_INLINE bool glms_vec3_eqv(vec3s a, vec3s b); + CGLM_INLINE bool glms_vec3_eqv_eps(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_max(vec3s v); + CGLM_INLINE float glms_vec3_min(vec3s v); + CGLM_INLINE bool glms_vec3_isnan(vec3s v); + CGLM_INLINE bool glms_vec3_isinf(vec3s v); + CGLM_INLINE bool glms_vec3_isvalid(vec3s v); + CGLM_INLINE vec3s glms_vec3_sign(vec3s v); + CGLM_INLINE vec3s glms_vec3_sqrt(vec3s v); + */ + #ifndef cglm_vec3s_ext_h #define cglm_vec3s_ext_h @@ -13,6 +34,12 @@ #include "../util.h" #include "../vec3-ext.h" +/*! + * @brief fill a vector with specified value + * + * @param[in] val value + * @returns dest + */ CGLM_INLINE vec3s glms_vec3_broadcast(float val) { @@ -21,66 +48,131 @@ glms_vec3_broadcast(float val) { return r; } +/*! + * @brief check if vector is equal to value (without epsilon) + * + * @param[in] v vector + * @param[in] val value + */ CGLM_INLINE bool glms_vec3_eq(vec3s v, float val) { return glm_vec3_eq(v.raw, val); } +/*! + * @brief check if vector is equal to value (with epsilon) + * + * @param[in] v vector + * @param[in] val value + */ CGLM_INLINE bool glms_vec3_eq_eps(vec3s v, float val) { return glm_vec3_eq_eps(v.raw, val); } +/*! + * @brief check if vectors members are equal (without epsilon) + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec3_eq_all(vec3s v) { return glm_vec3_eq_all(v.raw); } +/*! + * @brief check if vector is equal to another (without epsilon) + * + * @param[in] a vector + * @param[in] b vector + */ CGLM_INLINE bool glms_vec3_eqv(vec3s a, vec3s b) { return glm_vec3_eqv(a.raw, b.raw); } +/*! + * @brief check if vector is equal to another (with epsilon) + * + * @param[in] a vector + * @param[in] b vector + */ CGLM_INLINE bool glms_vec3_eqv_eps(vec3s a, vec3s b) { return glm_vec3_eqv_eps(a.raw, b.raw); } +/*! + * @brief max value of vector + * + * @param[in] v vector + */ CGLM_INLINE float glms_vec3_max(vec3s v) { return glm_vec3_max(v.raw); } +/*! + * @brief min value of vector + * + * @param[in] v vector + */ CGLM_INLINE float glms_vec3_min(vec3s v) { return glm_vec3_min(v.raw); } +/*! + * @brief check if all items are NaN (not a number) + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec3_isnan(vec3s v) { return glm_vec3_isnan(v.raw); } +/*! + * @brief check if all items are INFINITY + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec3_isinf(vec3s v) { return glm_vec3_isinf(v.raw); } +/*! + * @brief check if all items are valid number + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec3_isvalid(vec3s v) { return glm_vec3_isvalid(v.raw); } +/*! + * @brief get sign of 32 bit float as +1, -1, 0 + * + * Important: It returns 0 for zero/NaN input + * + * @param v vector + * @returns sign vector + */ CGLM_INLINE vec3s glms_vec3_sign(vec3s v) { @@ -89,6 +181,12 @@ glms_vec3_sign(vec3s v) { return r; } +/*! + * @brief square root of each vector item + * + * @param[in] v vector + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_sqrt(vec3s v) { diff --git a/include/cglm/structs/vec3.h b/include/cglm/structs/vec3.h index 312d59a..766742b 100644 --- a/include/cglm/structs/vec3.h +++ b/include/cglm/structs/vec3.h @@ -5,6 +5,67 @@ * Full license can be found in the LICENSE file */ +/* + Macros: + GLMS_VEC3_ONE_INIT + GLMS_VEC3_ZERO_INIT + GLMS_VEC3_ONE + GLMS_VEC3_ZERO + GLMS_YUP + GLMS_ZUP + GLMS_XUP + + Functions: + CGLM_INLINE vec3s glms_vec3(vec4s v4); + CGLM_INLINE vec3s glms_vec3_copy(vec3s v); + CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len); + CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len); + CGLM_INLINE vec3s glms_vec3_zero(); + CGLM_INLINE vec3s glms_vec3_one(); + CGLM_INLINE float glms_vec3_dot(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_norm2(vec3s v); + CGLM_INLINE float glms_vec3_norm(vec3s v); + CGLM_INLINE vec3s glms_vec3_add(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_adds(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_sub(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_subs(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_mul(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_scale(vec3s v, float s); + CGLM_INLINE vec3s glms_vec3_scale_as(vec3s v, float s); + CGLM_INLINE vec3s glms_vec3_div(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_divs(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_addadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_subadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_muladd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_muladds(vec3s a, float s, vec3s dest); + CGLM_INLINE vec3s glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v); + CGLM_INLINE vec3s glms_vec3_negate(vec3s v); + CGLM_INLINE vec3s glms_vec3_inv(vec3s v); + CGLM_INLINE vec3s glms_vec3_normalize(vec3s v); + CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_distance(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_angle(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_rotate(vec3s v, float angle, vec3s axis); + CGLM_INLINE vec3s glms_vec3_rotate_m4(mat4s m, vec3s v); + CGLM_INLINE vec3s glms_vec3_rotate_m3(mat3s m, vec3s v); + CGLM_INLINE vec3s glms_vec3_proj(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_center(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_distance2(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_maxv(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_minv(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_ortho(vec3s v); + CGLM_INLINE vec3s glms_vec3_clamp(vec3s v, float minVal, float maxVal); + CGLM_INLINE vec3s glms_vec3_lerp(vec3s from, vec3s to, float t); + + Convenient: + CGLM_INLINE vec3s glms_cross(vec3s a, vec3s b); + CGLM_INLINE float glms_dot(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_normalize(vec3s v); + */ + #ifndef cglm_vec3s_h #define cglm_vec3s_h @@ -24,6 +85,12 @@ #define GLMS_ZUP ((vec3s){0.0f, 0.0f, 1.0f}) #define GLMS_XUP ((vec3s){1.0f, 0.0f, 0.0f}) +/*! + * @brief init vec3 using vec4 + * + * @param[in] v4 vector4 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3(vec4s v4) { @@ -32,6 +99,12 @@ glms_vec3(vec4s v4) { return r; } +/*! + * @brief copy all members of [a] to [dest] + * + * @param[in] a source + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_copy(vec3s v) { @@ -40,6 +113,13 @@ glms_vec3_copy(vec3s v) { return r; } +/*! + * @brief pack an array of vec3 into an array of vec3s + * + * @param[out] dst array of vec3 + * @param[in] src array of vec3s + * @param[in] len number of elements + */ CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { @@ -52,6 +132,13 @@ glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { } } +/*! + * @brief unpack an array of vec3s into an array of vec3 + * + * @param[out] dst array of vec3s + * @param[in] src array of vec3 + * @param[in] len number of elements + */ CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len) { @@ -64,6 +151,12 @@ glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len) { } } +/*! + * @brief make vector zero + * + * @param[in] v vector + * @returns zero vector + */ CGLM_INLINE vec3s glms_vec3_zero() { @@ -72,6 +165,12 @@ glms_vec3_zero() { return r; } +/*! + * @brief make vector one + * + * @param[in] v vector + * @returns one vector + */ CGLM_INLINE vec3s glms_vec3_one() { @@ -80,24 +179,57 @@ glms_vec3_one() { return r; } +/*! + * @brief vec3 dot product + * + * @param[in] a vector1 + * @param[in] b vector2 + * + * @return dot product + */ CGLM_INLINE float glms_vec3_dot(vec3s a, vec3s b) { return glm_vec3_dot(a.raw, b.raw); } +/*! + * @brief norm * norm (magnitude) of vec + * + * we can use this func instead of calling norm * norm, because it would call + * sqrtf fuction twice but with this func we can avoid func call, maybe this is + * not good name for this func + * + * @param[in] v vector + * + * @return norm * norm + */ CGLM_INLINE float glms_vec3_norm2(vec3s v) { return glm_vec3_norm2(v.raw); } +/*! + * @brief norm (magnitude) of vec3 + * + * @param[in] v vector + * + * @return norm + */ CGLM_INLINE float glms_vec3_norm(vec3s v) { return glm_vec3_norm(v.raw); } +/*! + * @brief add a vector to b vector store result in dest + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_add(vec3s a, vec3s b) { @@ -106,6 +238,13 @@ glms_vec3_add(vec3s a, vec3s b) { return r; } +/*! + * @brief add scalar to v vector store result in dest (d = v + s) + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_adds(vec3s a, float s) { @@ -114,6 +253,13 @@ glms_vec3_adds(vec3s a, float s) { return r; } +/*! + * @brief subtract b vector from a vector store result in dest + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_sub(vec3s a, vec3s b) { @@ -122,6 +268,13 @@ glms_vec3_sub(vec3s a, vec3s b) { return r; } +/*! + * @brief subtract scalar from v vector store result in dest (d = v - s) + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_subs(vec3s a, float s) { @@ -130,6 +283,13 @@ glms_vec3_subs(vec3s a, float s) { return r; } +/*! + * @brief multiply two vector (component-wise multiplication) + * + * @param a vector1 + * @param b vector2 + * @returns v3 = (a[0] * b[0], a[1] * b[1], a[2] * b[2]) + */ CGLM_INLINE vec3s glms_vec3_mul(vec3s a, vec3s b) { @@ -138,6 +298,13 @@ glms_vec3_mul(vec3s a, vec3s b) { return r; } +/*! + * @brief multiply/scale vec3 vector with scalar: result = v * s + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_scale(vec3s v, float s) { @@ -146,6 +313,13 @@ glms_vec3_scale(vec3s v, float s) { return r; } +/*! + * @brief make vec3 vector scale as specified: result = unit(v) * s + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec3s glms_vec3_scale_as(vec3s v, float s) { @@ -154,6 +328,13 @@ glms_vec3_scale_as(vec3s v, float s) { return r; } +/*! + * @brief div vector with another component-wise division: d = a / b + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2]) + */ CGLM_INLINE vec3s glms_vec3_div(vec3s a, vec3s b) { @@ -162,6 +343,13 @@ glms_vec3_div(vec3s a, vec3s b) { return r; } +/*! + * @brief div vector with scalar: d = v / s + * + * @param[in] v vector + * @param[in] s scalar + * @returns result = (a[0]/s, a[1]/s, a[2]/s) + */ CGLM_INLINE vec3s glms_vec3_divs(vec3s a, float s) { @@ -170,6 +358,15 @@ glms_vec3_divs(vec3s a, float s) { return r; } +/*! + * @brief add two vectors and add result to sum + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a + b) + */ CGLM_INLINE vec3s glms_vec3_addadd(vec3s a, vec3s b, vec3s dest) { @@ -177,6 +374,15 @@ glms_vec3_addadd(vec3s a, vec3s b, vec3s dest) { return dest; } +/*! + * @brief sub two vectors and add result to dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a + b) + */ CGLM_INLINE vec3s glms_vec3_subadd(vec3s a, vec3s b, vec3s dest) { @@ -184,6 +390,15 @@ glms_vec3_subadd(vec3s a, vec3s b, vec3s dest) { return dest; } +/*! + * @brief mul two vectors and add result to dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a * b) + */ CGLM_INLINE vec3s glms_vec3_muladd(vec3s a, vec3s b, vec3s dest) { @@ -191,6 +406,15 @@ glms_vec3_muladd(vec3s a, vec3s b, vec3s dest) { return dest; } +/*! + * @brief mul vector with scalar and add result to sum + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector + * @param[in] s scalar + * @returns dest += (a * b) + */ CGLM_INLINE vec3s glms_vec3_muladds(vec3s a, float s, vec3s dest) { @@ -198,6 +422,15 @@ glms_vec3_muladds(vec3s a, float s, vec3s dest) { return dest; } +/*! + * @brief add max of two vector to result/dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += max(a, b) + */ CGLM_INLINE vec3s glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest) { @@ -205,6 +438,15 @@ glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest) { return dest; } +/*! + * @brief add min of two vector to result/dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += min(a, b) + */ CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest) { @@ -212,6 +454,12 @@ glms_vec3_minadd(vec3s a, vec3s b, vec3s dest) { return dest; } +/*! + * @brief negate vector components and store result in dest + * + * @param[in] v vector + * @returns result vector + */ CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v) { @@ -219,6 +467,12 @@ glms_vec3_flipsign(vec3s v) { return v; } +/*! + * @brief negate vector components + * + * @param[in] v vector + * @returns negated vector + */ CGLM_INLINE vec3s glms_vec3_negate(vec3s v) { @@ -226,13 +480,12 @@ glms_vec3_negate(vec3s v) { return v; } -CGLM_INLINE -vec3s -glms_vec3_inv(vec3s v) { - glm_vec3_inv(v.raw); - return v; -} - +/*! + * @brief normalize vec3 and store result in same vec + * + * @param[in] v vector + * @returns normalized vector + */ CGLM_INLINE vec3s glms_vec3_normalize(vec3s v) { @@ -240,6 +493,13 @@ glms_vec3_normalize(vec3s v) { return v; } +/*! + * @brief cross product of two vector (RH) + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b) { @@ -248,6 +508,13 @@ glms_vec3_cross(vec3s a, vec3s b) { return r; } +/*! + * @brief cross product of two vector (RH) and normalize the result + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b) { @@ -256,18 +523,28 @@ glms_vec3_crossn(vec3s a, vec3s b) { return r; } -CGLM_INLINE -float -glms_vec3_distance(vec3s a, vec3s b) { - return glm_vec3_distance(a.raw, b.raw); -} - +/*! + * @brief angle betwen two vector + * + * @param[in] a vector1 + * @param[in] b vector2 + * + * @return angle as radians + */ CGLM_INLINE float glms_vec3_angle(vec3s a, vec3s b) { return glm_vec3_angle(a.raw, b.raw); } +/*! + * @brief rotate vec3 around axis by angle using Rodrigues' rotation formula + * + * @param[in] v vector + * @param[in] axis axis vector (must be unit vector) + * @param[in] angle angle by radians + * @returns rotated vector + */ CGLM_INLINE vec3s glms_vec3_rotate(vec3s v, float angle, vec3s axis) { @@ -275,6 +552,19 @@ glms_vec3_rotate(vec3s v, float angle, vec3s axis) { return v; } +/*! + * @brief apply rotation matrix to vector + * + * matrix format should be (no perspective): + * a b c x + * e f g y + * i j k z + * 0 0 0 w + * + * @param[in] m affine matrix or rot matrix + * @param[in] v vector + * @returns rotated vector + */ CGLM_INLINE vec3s glms_vec3_rotate_m4(mat4s m, vec3s v) { @@ -283,6 +573,13 @@ glms_vec3_rotate_m4(mat4s m, vec3s v) { return r; } +/*! + * @brief apply rotation matrix to vector + * + * @param[in] m affine matrix or rot matrix + * @param[in] v vector + * @returns rotated vector + */ CGLM_INLINE vec3s glms_vec3_rotate_m3(mat3s m, vec3s v) { @@ -291,6 +588,13 @@ glms_vec3_rotate_m3(mat3s m, vec3s v) { return r; } +/*! + * @brief project a vector onto b vector + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns projected vector + */ CGLM_INLINE vec3s glms_vec3_proj(vec3s a, vec3s b) { @@ -299,6 +603,13 @@ glms_vec3_proj(vec3s a, vec3s b) { return r; } +/** + * @brief find center point of two vector + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns center point + */ CGLM_INLINE vec3s glms_vec3_center(vec3s a, vec3s b) { @@ -307,12 +618,39 @@ glms_vec3_center(vec3s a, vec3s b) { return r; } +/** + * @brief squared distance between two vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @return squared distance (distance * distance) + */ CGLM_INLINE float glms_vec3_distance2(vec3s a, vec3s b) { return glm_vec3_distance2(a.raw, b.raw); } +/** + * @brief distance between two vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @return distance + */ +CGLM_INLINE +float +glms_vec3_distance(vec3s a, vec3s b) { + return glm_vec3_distance(a.raw, b.raw); +} + +/*! + * @brief max values of vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_maxv(vec3s a, vec3s b) { @@ -321,6 +659,13 @@ glms_vec3_maxv(vec3s a, vec3s b) { return r; } +/*! + * @brief min values of vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_minv(vec3s a, vec3s b) { @@ -329,6 +674,12 @@ glms_vec3_minv(vec3s a, vec3s b) { return r; } +/*! + * @brief possible orthogonal/perpendicular vector + * + * @param[in] v vector + * @returns orthogonal/perpendicular vector + */ CGLM_INLINE vec3s glms_vec3_ortho(vec3s v) { @@ -337,6 +688,14 @@ glms_vec3_ortho(vec3s v) { return r; } +/*! + * @brief clamp vector's individual members between min and max values + * + * @param[in] v vector + * @param[in] minVal minimum value + * @param[in] maxVal maximum value + * @returns clamped vector + */ CGLM_INLINE vec3s glms_vec3_clamp(vec3s v, float minVal, float maxVal) { @@ -344,6 +703,16 @@ glms_vec3_clamp(vec3s v, float minVal, float maxVal) { return v; } +/*! + * @brief linear interpolation between two vector + * + * formula: from + s * (to - from) + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t interpolant (amount) clamped between 0 and 1 + * @returns destination + */ CGLM_INLINE vec3s glms_vec3_lerp(vec3s from, vec3s to, float t) { @@ -352,6 +721,15 @@ glms_vec3_lerp(vec3s from, vec3s to, float t) { return r; } +/*! + * @brief vec3 cross product + * + * this is just convenient wrapper + * + * @param[in] a source 1 + * @param[in] b source 2 + * @returns destination + */ CGLM_INLINE vec3s glms_cross(vec3s a, vec3s b) { @@ -360,12 +738,30 @@ glms_cross(vec3s a, vec3s b) { return r; } +/*! + * @brief vec3 dot product + * + * this is just convenient wrapper + * + * @param[in] a vector1 + * @param[in] b vector2 + * + * @return dot product + */ CGLM_INLINE float glms_dot(vec3s a, vec3s b) { return glm_dot(a.raw, b.raw); } +/*! + * @brief normalize vec3 and store result in same vec + * + * this is just convenient wrapper + * + * @param[in] v vector + * @returns normalized vector + */ CGLM_INLINE vec3s glms_normalize(vec3s v) { diff --git a/include/cglm/structs/vec4-ext.h b/include/cglm/structs/vec4-ext.h index 4fbb2d3..0b81b49 100644 --- a/include/cglm/structs/vec4-ext.h +++ b/include/cglm/structs/vec4-ext.h @@ -5,6 +5,27 @@ * Full license can be found in the LICENSE file */ +/*! + * @brief SIMD like functions + */ + +/* + Functions: + CGLM_INLINE vec4s glms_vec4_broadcast(float val); + CGLM_INLINE bool glms_vec4_eq(vec4s v, float val); + CGLM_INLINE bool glms_vec4_eq_eps(vec4s v, float val); + CGLM_INLINE bool glms_vec4_eq_all(vec4s v); + CGLM_INLINE bool glms_vec4_eqv(vec4s a, vec4s b); + CGLM_INLINE bool glms_vec4_eqv_eps(vec4s a, vec4s b); + CGLM_INLINE float glms_vec4_max(vec4s v); + CGLM_INLINE float glms_vec4_min(vec4s v); + CGLM_INLINE bool glms_vec4_isnan(vec4s v); + CGLM_INLINE bool glms_vec4_isinf(vec4s v); + CGLM_INLINE bool glms_vec4_isvalid(vec4s v); + CGLM_INLINE vec4s glms_vec4_sign(vec4s v); + CGLM_INLINE vec4s glms_vec4_sqrt(vec4s v); + */ + #ifndef cglm_vec4s_ext_h #define cglm_vec4s_ext_h @@ -13,6 +34,12 @@ #include "../util.h" #include "../vec4-ext.h" +/*! + * @brief fill a vector with specified value + * + * @param val value + * @returns dest + */ CGLM_INLINE vec4s glms_vec4_broadcast(float val) { @@ -21,66 +48,131 @@ glms_vec4_broadcast(float val) { return r; } +/*! + * @brief check if vector is equal to value (without epsilon) + * + * @param v vector + * @param val value + */ CGLM_INLINE bool glms_vec4_eq(vec4s v, float val) { return glm_vec4_eq(v.raw, val); } +/*! + * @brief check if vector is equal to value (with epsilon) + * + * @param v vector + * @param val value + */ CGLM_INLINE bool glms_vec4_eq_eps(vec4s v, float val) { return glm_vec4_eq_eps(v.raw, val); } +/*! + * @brief check if vectors members are equal (without epsilon) + * + * @param v vector + */ CGLM_INLINE bool glms_vec4_eq_all(vec4s v) { return glm_vec4_eq_all(v.raw); } +/*! + * @brief check if vector is equal to another (without epsilon) + * + * @param a vector + * @param b vector + */ CGLM_INLINE bool glms_vec4_eqv(vec4s a, vec4s b) { return glm_vec4_eqv(a.raw, b.raw); } +/*! + * @brief check if vector is equal to another (with epsilon) + * + * @param a vector + * @param b vector + */ CGLM_INLINE bool glms_vec4_eqv_eps(vec4s a, vec4s b) { return glm_vec4_eqv_eps(a.raw, b.raw); } +/*! + * @brief max value of vector + * + * @param v vector + */ CGLM_INLINE float glms_vec4_max(vec4s v) { return glm_vec4_max(v.raw); } +/*! + * @brief min value of vector + * + * @param v vector + */ CGLM_INLINE float glms_vec4_min(vec4s v) { return glm_vec4_min(v.raw); } +/*! + * @brief check if one of items is NaN (not a number) + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec4_isnan(vec4s v) { return glm_vec4_isnan(v.raw); } +/*! + * @brief check if one of items is INFINITY + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec4_isinf(vec4s v) { return glm_vec4_isinf(v.raw); } +/*! + * @brief check if all items are valid number + * you should only use this in DEBUG mode or very critical asserts + * + * @param[in] v vector + */ CGLM_INLINE bool glms_vec4_isvalid(vec4s v) { return glm_vec4_isvalid(v.raw); } +/*! + * @brief get sign of 32 bit float as +1, -1, 0 + * + * Important: It returns 0 for zero/NaN input + * + * @param v vector + * @returns sign vector + */ CGLM_INLINE vec4s glms_vec4_sign(vec4s v) { @@ -89,6 +181,12 @@ glms_vec4_sign(vec4s v) { return r; } +/*! + * @brief square root of each vector item + * + * @param[in] v vector + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_sqrt(vec4s v) { diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index 956fee1..2f950a7 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -5,6 +5,51 @@ * Full license can be found in the LICENSE file */ +/* + Macros: + GLM_VEC4_ONE_INIT + GLM_VEC4_BLACK_INIT + GLM_VEC4_ZERO_INIT + GLM_VEC4_ONE + GLM_VEC4_BLACK + GLM_VEC4_ZERO + + Functions: + CGLM_INLINE vec4s glms_vec4(vec3s v3, float last); + CGLM_INLINE vec3s glms_vec4_copy3(vec4s v); + CGLM_INLINE vec4s glms_vec4_copy(vec4s v); + CGLM_INLINE vec4s glms_vec4_ucopy(vec4s v); + CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len); + CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len); + CGLM_INLINE float glms_vec4_dot(vec4s a, vec4s b); + CGLM_INLINE float glms_vec4_norm2(vec4s v); + CGLM_INLINE float glms_vec4_norm(vec4s v); + CGLM_INLINE vec4s glms_vec4_add(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_adds(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_sub(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_subs(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_mul(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_scale(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_scale_as(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_div(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_divs(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_addadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_subadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_muladd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_muladds(vec4s a, float s, vec4s dest); + CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_negate(vec4s v); + CGLM_INLINE vec4s glms_vec4_inv(vec4s v); + CGLM_INLINE vec4s glms_vec4_normalize(vec4s v); + CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_maxv(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_minv(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_clamp(vec4s v, float minVal, float maxVal); + CGLM_INLINE vec4s glms_vec4_lerp(vec4s from, vec4s to, float t); + CGLM_INLINE vec4s glms_vec4_cubic(float s); + */ + #ifndef cglm_vec4s_h #define cglm_vec4s_h @@ -22,6 +67,13 @@ #define GLMS_VEC4_BLACK ((vec4s)GLM_VEC4_BLACK_INIT) #define GLMS_VEC4_ZERO ((vec4s)GLM_VEC4_ZERO_INIT) +/*! + * @brief init vec4 using vec3 + * + * @param[in] v3 vector3 + * @param[in] last last item + * @returns destination + */ CGLM_INLINE vec4s glms_vec4(vec3s v3, float last) { @@ -30,6 +82,12 @@ glms_vec4(vec3s v3, float last) { return r; } +/*! + * @brief copy first 3 members of [a] to [dest] + * + * @param[in] a source + * @returns destination + */ CGLM_INLINE vec3s glms_vec4_copy3(vec4s v) { @@ -38,6 +96,12 @@ glms_vec4_copy3(vec4s v) { return r; } +/*! + * @brief copy all members of [a] to [dest] + * + * @param[in] v source + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_copy(vec4s v) { @@ -46,6 +110,14 @@ glms_vec4_copy(vec4s v) { return r; } +/*! + * @brief copy all members of [a] to [dest] + * + * alignment is not required + * + * @param[in] v source + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_ucopy(vec4s v) { @@ -54,6 +126,13 @@ glms_vec4_ucopy(vec4s v) { return r; } +/*! + * @brief pack an array of vec4 into an array of vec4s + * + * @param[out] dst array of vec4 + * @param[in] src array of vec4s + * @param[in] len number of elements + */ CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { @@ -66,6 +145,13 @@ glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { } } +/*! + * @brief unpack an array of vec4s into an array of vec4 + * + * @param[out] dst array of vec4s + * @param[in] src array of vec4 + * @param[in] len number of elements + */ CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len) { @@ -78,24 +164,85 @@ glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len) { } } +/*! + * @brief make vector zero + * + * @param[in] v vector + * @returns zero vector + */ +CGLM_INLINE +vec4s +glms_vec4_zero() { + vec4s r; + glm_vec4_zero(r.raw); + return r; +} + +/*! + * @brief make vector one + * + * @param[in] v vector + * @returns one vector + */ +CGLM_INLINE +vec4s +glms_vec4_one() { + vec4s r; + glm_vec4_one(r.raw); + return r; +} + +/*! + * @brief vec4 dot product + * + * @param[in] a vector1 + * @param[in] b vector2 + * + * @return dot product + */ CGLM_INLINE float glms_vec4_dot(vec4s a, vec4s b) { return glm_vec4_dot(a.raw, b.raw); } +/*! + * @brief norm * norm (magnitude) of vec + * + * we can use this func instead of calling norm * norm, because it would call + * sqrtf fuction twice but with this func we can avoid func call, maybe this is + * not good name for this func + * + * @param[in] v vec4 + * + * @return norm * norm + */ CGLM_INLINE float glms_vec4_norm2(vec4s v) { return glm_vec4_norm2(v.raw); } +/*! + * @brief norm (magnitude) of vec4 + * + * @param[in] v vector + * + * @return norm + */ CGLM_INLINE float glms_vec4_norm(vec4s v) { return glm_vec4_norm(v.raw); } +/*! + * @brief add b vector to a vector store result in dest + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_add(vec4s a, vec4s b) { @@ -104,6 +251,13 @@ glms_vec4_add(vec4s a, vec4s b) { return r; } +/*! + * @brief add scalar to v vector store result in dest (d = v + vec(s)) + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_adds(vec4s v, float s) { @@ -112,6 +266,13 @@ glms_vec4_adds(vec4s v, float s) { return r; } +/*! + * @brief subtract b vector from a vector store result in dest (d = a - b) + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_sub(vec4s a, vec4s b) { @@ -120,6 +281,13 @@ glms_vec4_sub(vec4s a, vec4s b) { return r; } +/*! + * @brief subtract scalar from v vector store result in dest (d = v - vec(s)) + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_subs(vec4s v, float s) { @@ -128,6 +296,13 @@ glms_vec4_subs(vec4s v, float s) { return r; } +/*! + * @brief multiply two vector (component-wise multiplication) + * + * @param a vector1 + * @param b vector2 + * @returns dest = (a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]) + */ CGLM_INLINE vec4s glms_vec4_mul(vec4s a, vec4s b) { @@ -136,6 +311,13 @@ glms_vec4_mul(vec4s a, vec4s b) { return r; } +/*! + * @brief multiply/scale vec4 vector with scalar: result = v * s + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_scale(vec4s v, float s) { @@ -144,6 +326,13 @@ glms_vec4_scale(vec4s v, float s) { return r; } +/*! + * @brief make vec4 vector scale as specified: result = unit(v) * s + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_scale_as(vec4s v, float s) { @@ -152,6 +341,13 @@ glms_vec4_scale_as(vec4s v, float s) { return r; } +/*! + * @brief div vector with another component-wise division: d = a / b + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2], a[3]/b[3]) + */ CGLM_INLINE vec4s glms_vec4_div(vec4s a, vec4s b) { @@ -160,6 +356,13 @@ glms_vec4_div(vec4s a, vec4s b) { return r; } +/*! + * @brief div vec4 vector with scalar: d = v / s + * + * @param[in] v vector + * @param[in] s scalar + * @returns destination vector + */ CGLM_INLINE vec4s glms_vec4_divs(vec4s v, float s) { @@ -168,6 +371,15 @@ glms_vec4_divs(vec4s v, float s) { return r; } +/*! + * @brief add two vectors and add result to sum + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a + b) + */ CGLM_INLINE vec4s glms_vec4_addadd(vec4s a, vec4s b, vec4s dest) { @@ -175,6 +387,15 @@ glms_vec4_addadd(vec4s a, vec4s b, vec4s dest) { return dest; } +/*! + * @brief sub two vectors and add result to dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a - b) + */ CGLM_INLINE vec4s glms_vec4_subadd(vec4s a, vec4s b, vec4s dest) { @@ -182,6 +403,15 @@ glms_vec4_subadd(vec4s a, vec4s b, vec4s dest) { return dest; } +/*! + * @brief mul two vectors and add result to dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += (a * b) + */ CGLM_INLINE vec4s glms_vec4_muladd(vec4s a, vec4s b, vec4s dest) { @@ -189,6 +419,15 @@ glms_vec4_muladd(vec4s a, vec4s b, vec4s dest) { return dest; } +/*! + * @brief mul vector with scalar and add result to sum + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector + * @param[in] s scalar + * @returns dest += (a * b) + */ CGLM_INLINE vec4s glms_vec4_muladds(vec4s a, float s, vec4s dest) { @@ -196,6 +435,15 @@ glms_vec4_muladds(vec4s a, float s, vec4s dest) { return dest; } +/*! + * @brief add max of two vector to result/dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += max(a, b) + */ CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest) { @@ -203,6 +451,15 @@ glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest) { return dest; } +/*! + * @brief add min of two vector to result/dest + * + * it applies += operator so dest must be initialized + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @returns dest += min(a, b) + */ CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest) { @@ -210,6 +467,12 @@ glms_vec4_minadd(vec4s a, vec4s b, vec4s dest) { return dest; } +/*! + * @brief negate vector components and store result in dest + * + * @param[in] v vector + * @returns result vector + */ CGLM_INLINE vec4s glms_vec4_negate(vec4s v) { @@ -217,13 +480,12 @@ glms_vec4_negate(vec4s v) { return v; } -CGLM_INLINE -vec4s -glms_vec4_inv(vec4s v) { - glm_vec4_inv(v.raw); - return v; -} - +/*! + * @brief normalize vec4 and store result in same vec + * + * @param[in] v vector + * @returns normalized vector + */ CGLM_INLINE vec4s glms_vec4_normalize(vec4s v) { @@ -231,12 +493,26 @@ glms_vec4_normalize(vec4s v) { return v; } +/** + * @brief distance between two vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @return returns distance + */ CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b) { return glm_vec4_distance(a.raw, b.raw); } +/*! + * @brief max values of vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_maxv(vec4s a, vec4s b) { @@ -245,6 +521,13 @@ glms_vec4_maxv(vec4s a, vec4s b) { return r; } +/*! + * @brief min values of vectors + * + * @param[in] a vector1 + * @param[in] b vector2 + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_minv(vec4s a, vec4s b) { @@ -253,6 +536,14 @@ glms_vec4_minv(vec4s a, vec4s b) { return r; } +/*! + * @brief clamp vector's individual members between min and max values + * + * @param[in] v vector + * @param[in] minVal minimum value + * @param[in] maxVal maximum value + * @returns clamped vector + */ CGLM_INLINE vec4s glms_vec4_clamp(vec4s v, float minVal, float maxVal) { @@ -260,6 +551,16 @@ glms_vec4_clamp(vec4s v, float minVal, float maxVal) { return v; } +/*! + * @brief linear interpolation between two vector + * + * formula: from + s * (to - from) + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t interpolant (amount) clamped between 0 and 1 + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_lerp(vec4s from, vec4s to, float t) { @@ -268,6 +569,12 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) { return r; } +/*! + * @brief helper to fill vec4 as [S^3, S^2, S, 1] + * + * @param[in] s parameter + * @returns destination + */ CGLM_INLINE vec4s glms_vec4_cubic(float s) { From ba2031d977a846786d8ef373d02c0f12e790d4dc Mon Sep 17 00:00:00 2001 From: acoto87 Date: Tue, 21 May 2019 16:45:34 -0500 Subject: [PATCH 10/11] - Rename `structs` folder to `struct` - Include files in `cglms.h` - Fix style issues (tabs to spaces, open brace without newline) --- include/cglm/cglms.h | 18 +- include/cglm/{structs => struct}/affine.h | 206 +++++++++--------- include/cglm/{structs => struct}/box.h | 86 ++++---- include/cglm/{structs => struct}/color.h | 2 +- include/cglm/{structs => struct}/curve.h | 2 +- include/cglm/{structs => struct}/frustum.h | 42 ++-- include/cglm/{structs => struct}/io.h | 10 +- include/cglm/{structs => struct}/mat3.h | 0 include/cglm/{structs => struct}/mat4.h | 6 +- include/cglm/{structs => struct}/plane.h | 4 +- include/cglm/{structs => struct}/project.h | 22 +- include/cglm/{structs => struct}/sphere.h | 0 include/cglm/{structs => struct}/vec3-ext.h | 32 +-- include/cglm/{structs => struct}/vec3.h | 225 ++++++++++---------- include/cglm/{structs => struct}/vec4-ext.h | 34 +-- include/cglm/{structs => struct}/vec4.h | 182 ++++++++-------- include/cglm/types-struct.h | 21 +- test/src/test_common.c | 12 +- 18 files changed, 447 insertions(+), 457 deletions(-) rename include/cglm/{structs => struct}/affine.h (53%) rename include/cglm/{structs => struct}/box.h (78%) rename include/cglm/{structs => struct}/color.h (93%) rename include/cglm/{structs => struct}/curve.h (96%) rename include/cglm/{structs => struct}/frustum.h (83%) rename include/cglm/{structs => struct}/io.h (90%) rename include/cglm/{structs => struct}/mat3.h (100%) rename include/cglm/{structs => struct}/mat4.h (99%) rename include/cglm/{structs => struct}/plane.h (93%) rename include/cglm/{structs => struct}/project.h (89%) rename include/cglm/{structs => struct}/sphere.h (100%) rename include/cglm/{structs => struct}/vec3-ext.h (81%) rename include/cglm/{structs => struct}/vec3.h (70%) rename include/cglm/{structs => struct}/vec4-ext.h (79%) rename include/cglm/{structs => struct}/vec4.h (70%) diff --git a/include/cglm/cglms.h b/include/cglm/cglms.h index 67dbfa4..0ecc8de 100644 --- a/include/cglm/cglms.h +++ b/include/cglm/cglms.h @@ -13,11 +13,19 @@ extern "C" { #include "cglm.h" #include "types-struct.h" -#include "structs/vec3.h" -#include "structs/vec4.h" -#include "structs/mat3.h" -#include "structs/mat4.h" -#include "structs/sphere.h" +#include "struct/vec3.h" +#include "struct/vec4.h" +#include "struct/mat3.h" +#include "struct/mat4.h" +#include "struct/affine.h" +#include "struct/frustum.h" +#include "struct/plane.h" +#include "struct/box.h" +#include "struct/color.h" +#include "struct/io.h" +#include "struct/project.h" +#include "struct/sphere.h" +#include "struct/curve.h" #ifdef __cplusplus } diff --git a/include/cglm/structs/affine.h b/include/cglm/struct/affine.h similarity index 53% rename from include/cglm/structs/affine.h rename to include/cglm/struct/affine.h index 481aaa3..0f753c5 100644 --- a/include/cglm/structs/affine.h +++ b/include/cglm/struct/affine.h @@ -8,25 +8,25 @@ /* Functions: CGLM_INLINE mat4s glms_translate(mat4s m, vec3s v); - CGLM_INLINE mat4s glms_translate_x(mat4s m, float x); - CGLM_INLINE mat4s glms_translate_y(mat4s m, float y); - CGLM_INLINE mat4s glms_translate_z(mat4s m, float z); - CGLM_INLINE mat4s glms_translate_make(vec3s v); - CGLM_INLINE mat4s glms_scale_to(mat4s m, vec3s v); - CGLM_INLINE mat4s glms_scale_make(vec3s v); - CGLM_INLINE mat4s glms_scale(mat4s m, vec3s v); - CGLM_INLINE mat4s glms_scale_uni(mat4s m, float s); - CGLM_INLINE mat4s glmx_rotate_x(mat4s m, float angle); - CGLM_INLINE mat4s glms_rotate_y(mat4s m, float angle); - CGLM_INLINE mat4s glms_rotate_z(mat4s m, float angle); - CGLM_INLINE mat4s glms_rotate_make(float angle, vec3s axis); - CGLM_INLINE mat4s glms_rotate(mat4s m, float angle, vec3s axis); - CGLM_INLINE mat4s glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis); - CGLM_INLINE mat4s glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis); - CGLM_INLINE vec3s glms_decompose_scalev(mat4s m); - CGLM_INLINE bool glms_uniscaled(mat4s m); - CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s * r, vec3s * s); - CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s * r, vec3s * s); + CGLM_INLINE mat4s glms_translate_x(mat4s m, float x); + CGLM_INLINE mat4s glms_translate_y(mat4s m, float y); + CGLM_INLINE mat4s glms_translate_z(mat4s m, float z); + CGLM_INLINE mat4s glms_translate_make(vec3s v); + CGLM_INLINE mat4s glms_scale_to(mat4s m, vec3s v); + CGLM_INLINE mat4s glms_scale_make(vec3s v); + CGLM_INLINE mat4s glms_scale(mat4s m, vec3s v); + CGLM_INLINE mat4s glms_scale_uni(mat4s m, float s); + CGLM_INLINE mat4s glmx_rotate_x(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_y(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_z(mat4s m, float angle); + CGLM_INLINE mat4s glms_rotate_make(float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate(mat4s m, float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis); + CGLM_INLINE mat4s glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis); + CGLM_INLINE vec3s glms_decompose_scalev(mat4s m); + CGLM_INLINE bool glms_uniscaled(mat4s m); + CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s * r, vec3s * s); + CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s * r, vec3s * s); */ #ifndef cglm_affines_h @@ -47,71 +47,71 @@ glms_mat4_mul(mat4s m1, mat4s m2); * @brief translate existing transform matrix by v vector * and stores result in same matrix * - * @param[in] m affine transfrom - * @param[in] v translate vector [x, y, z] - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] v translate vector [x, y, z] + * @returns affine transfrom */ CGLM_INLINE mat4s glms_translate(mat4s m, vec3s v) { - glm_translate(m.raw, v.raw); - return m; + glm_translate(m.raw, v.raw); + return m; } /*! * @brief translate existing transform matrix by x factor * - * @param[in] m affine transfrom - * @param[in] x x factor - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] x x factor + * @returns affine transfrom */ CGLM_INLINE mat4s glms_translate_x(mat4s m, float x) { - glm_translate_x(m.raw, x); - return m; + glm_translate_x(m.raw, x); + return m; } /*! * @brief translate existing transform matrix by y factor * - * @param[in] m affine transfrom - * @param[in] y y factor - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] y y factor + * @returns affine transfrom */ CGLM_INLINE mat4s glms_translate_y(mat4s m, float y) { - glm_translate_y(m.raw, y); - return m; + glm_translate_y(m.raw, y); + return m; } /*! * @brief translate existing transform matrix by z factor * - * @param[in] m affine transfrom - * @param[in] z z factor - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] z z factor + * @returns affine transfrom */ CGLM_INLINE mat4s glms_translate_z(mat4s m, float z) { - glm_translate_z(m.raw, z); - return m; + glm_translate_z(m.raw, z); + return m; } /*! * @brief creates NEW translate transform matrix by v vector * - * @param[in] v translate vector [x, y, z] - * @returns affine transfrom + * @param[in] v translate vector [x, y, z] + * @returns affine transfrom */ CGLM_INLINE mat4s glms_translate_make(vec3s v) { - mat4s m; - glm_translate_make(m.raw, v.raw); - return m; + mat4s m; + glm_translate_make(m.raw, v.raw); + return m; } /*! @@ -123,88 +123,88 @@ glms_translate_make(vec3s v) { CGLM_INLINE mat4s glms_scale_make(vec3s v) { - mat4s m; - glm_scale_make(m.raw, v.raw); - return m; + mat4s m; + glm_scale_make(m.raw, v.raw); + return m; } /*! * @brief scales existing transform matrix by v vector * and stores result in same matrix * - * @param[in] m affine transfrom - * @param[in] v scale vector [x, y, z] - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] v scale vector [x, y, z] + * @returns affine transfrom */ CGLM_INLINE mat4s glms_scale(mat4s m, vec3s v) { - mat4s r; + mat4s r; glm_scale_to(m.raw, v.raw, r.raw); - return r; + return r; } /*! * @brief applies uniform scale to existing transform matrix v = [s, s, s] * and stores result in same matrix * - * @param[in] m affine transfrom - * @param[in] s scale factor - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] s scale factor + * @returns affine transfrom */ CGLM_INLINE mat4s glms_scale_uni(mat4s m, float s) { - glm_scale_uni(m.raw, s); - return m; + glm_scale_uni(m.raw, s); + return m; } /*! * @brief rotate existing transform matrix around X axis by angle * and store result in dest * - * @param[in] m affine transfrom - * @param[in] angle angle (radians) - * @returns rotated matrix + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix */ CGLM_INLINE mat4s glmx_rotate_x(mat4s m, float angle) { - mat4s r; - glm_rotate_x(m.raw, angle, r.raw); - return r; + mat4s r; + glm_rotate_x(m.raw, angle, r.raw); + return r; } /*! * @brief rotate existing transform matrix around Y axis by angle * and store result in dest * - * @param[in] m affine transfrom - * @param[in] angle angle (radians) - * @returns rotated matrix + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix */ CGLM_INLINE mat4s glms_rotate_y(mat4s m, float angle) { - mat4s r; - glm_rotate_y(m.raw, angle, r.raw); - return r; + mat4s r; + glm_rotate_y(m.raw, angle, r.raw); + return r; } /*! * @brief rotate existing transform matrix around Z axis by angle * and store result in dest * - * @param[in] m affine transfrom - * @param[in] angle angle (radians) - * @returns rotated matrix + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @returns rotated matrix */ CGLM_INLINE mat4s glms_rotate_z(mat4s m, float angle) { - mat4s r; - glm_rotate_z(m.raw, angle, r.raw); - return r; + mat4s r; + glm_rotate_z(m.raw, angle, r.raw); + return r; } /*! @@ -212,48 +212,48 @@ glms_rotate_z(mat4s m, float angle) { * * axis will be normalized so you don't need to normalize it * - * @param[in] angle angle (radians) - * @param[in] axis axis - * @returns affine transfrom + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom */ CGLM_INLINE mat4s glms_rotate_make(float angle, vec3s axis) { - mat4s m; - glm_rotate_make(m.raw, angle, axis.raw); - return m; + mat4s m; + glm_rotate_make(m.raw, angle, axis.raw); + return m; } /*! * @brief rotate existing transform matrix around given axis by angle * - * @param[in] m affine transfrom - * @param[in] angle angle (radians) - * @param[in] axis axis - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom */ CGLM_INLINE mat4s glms_rotate(mat4s m, float angle, vec3s axis) { - glm_rotate(m.raw, angle, axis.raw); - return m; + glm_rotate(m.raw, angle, axis.raw); + return m; } /*! * @brief rotate existing transform * around given axis by angle at given pivot point (rotation center) * - * @param[in] m affine transfrom - * @param[in] pivot rotation center - * @param[in] angle angle (radians) - * @param[in] axis axis - * @returns affine transfrom + * @param[in] m affine transfrom + * @param[in] pivot rotation center + * @param[in] angle angle (radians) + * @param[in] axis axis + * @returns affine transfrom */ CGLM_INLINE mat4s glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis) { - glm_rotate_at(m.raw, pivot.raw, angle, axis.raw); - return m; + glm_rotate_at(m.raw, pivot.raw, angle, axis.raw); + return m; } /*! @@ -268,27 +268,27 @@ glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis) { * @param[in] pivot rotation center * @param[in] angle angle (radians) * @param[in] axis axis - * @returns affine transfrom + * @returns affine transfrom */ CGLM_INLINE mat4s glms_rotate_atm(mat4s m, vec3s pivot, float angle, vec3s axis) { - glm_rotate_atm(m.raw, pivot.raw, angle, axis.raw); - return m; + glm_rotate_atm(m.raw, pivot.raw, angle, axis.raw); + return m; } /*! * @brief decompose scale vector * * @param[in] m affine transform - * @returns scale vector (Sx, Sy, Sz) + * @returns scale vector (Sx, Sy, Sz) */ CGLM_INLINE vec3s glms_decompose_scalev(mat4s m) { - vec3s r; - glm_decompose_scalev(m.raw, r.raw); - return r; + vec3s r; + glm_decompose_scalev(m.raw, r.raw); + return r; } /*! @@ -302,7 +302,7 @@ glms_decompose_scalev(mat4s m) { CGLM_INLINE bool glms_uniscaled(mat4s m) { - return glm_uniscaled(m.raw); + return glm_uniscaled(m.raw); } /*! @@ -316,7 +316,7 @@ glms_uniscaled(mat4s m) { CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s * __restrict r, vec3s * __restrict s) { - glm_decompose_rs(m.raw, r->raw, s->raw); + glm_decompose_rs(m.raw, r->raw, s->raw); } /*! diff --git a/include/cglm/structs/box.h b/include/cglm/struct/box.h similarity index 78% rename from include/cglm/structs/box.h rename to include/cglm/struct/box.h index 4251bd7..9dc915e 100644 --- a/include/cglm/structs/box.h +++ b/include/cglm/struct/box.h @@ -25,12 +25,12 @@ CGLM_INLINE void glms_aabb_transform(vec3s box[2], mat4s m, vec3s dest[2]) { - vec3 rawBox[2]; - vec3 rawDest[2]; + vec3 rawBox[2]; + vec3 rawDest[2]; - glms_vec3_unpack(rawBox, box, 2); - glm_aabb_transform(rawBox, m.raw, rawDest); - glms_vec3_pack(dest, rawDest, 2); + glms_vec3_unpack(rawBox, box, 2); + glm_aabb_transform(rawBox, m.raw, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -46,14 +46,14 @@ glms_aabb_transform(vec3s box[2], mat4s m, vec3s dest[2]) { CGLM_INLINE void glms_aabb_merge(vec3s box1[2], vec3s box2[2], vec3s dest[2]) { - vec3 rawBox1[2]; - vec3 rawBox2[2]; - vec3 rawDest[2]; + vec3 rawBox1[2]; + vec3 rawBox2[2]; + vec3 rawDest[2]; - glms_vec3_unpack(rawBox1, box1, 2); - glms_vec3_unpack(rawBox2, box2, 2); - glm_aabb_merge(rawBox1, rawBox2, rawDest); - glms_vec3_pack(dest, rawDest, 2); + glms_vec3_unpack(rawBox1, box1, 2); + glms_vec3_unpack(rawBox2, box2, 2); + glm_aabb_merge(rawBox1, rawBox2, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -70,14 +70,14 @@ glms_aabb_merge(vec3s box1[2], vec3s box2[2], vec3s dest[2]) { CGLM_INLINE void glms_aabb_crop(vec3s box[2], vec3s cropBox[2], vec3s dest[2]) { - vec3 rawBox[2]; - vec3 rawCropBox[2]; - vec3 rawDest[2]; + vec3 rawBox[2]; + vec3 rawCropBox[2]; + vec3 rawDest[2]; - glms_vec3_unpack(rawBox, box, 2); - glms_vec3_unpack(rawCropBox, cropBox, 2); - glm_aabb_crop(rawBox, rawCropBox, rawDest); - glms_vec3_pack(dest, rawDest, 2); + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawCropBox, cropBox, 2); + glm_aabb_crop(rawBox, rawCropBox, rawDest); + glms_vec3_pack(dest, rawDest, 2); } /*! @@ -119,12 +119,12 @@ glms_aabb_crop_until(vec3s box[2], CGLM_INLINE bool glms_aabb_frustum(vec3s box[2], vec4s planes[6]) { - vec3 rawBox[2]; - vec4 rawPlanes[6]; + vec3 rawBox[2]; + vec4 rawPlanes[6]; - glms_vec3_unpack(rawBox, box, 2); - glms_vec4_unpack(rawPlanes, planes, 6); - return glm_aabb_frustum(rawBox, rawPlanes); + glms_vec3_unpack(rawBox, box, 2); + glms_vec4_unpack(rawPlanes, planes, 6); + return glm_aabb_frustum(rawBox, rawPlanes); } /*! @@ -135,8 +135,6 @@ glms_aabb_frustum(vec3s box[2], vec4s planes[6]) { CGLM_INLINE void glms_aabb_invalidate(vec3s box[2]) { - // FIX: Modify param - // box[0] = glms_vec3_broadcast(FLT_MAX); box[1] = glms_vec3_broadcast(-FLT_MAX); } @@ -149,9 +147,9 @@ glms_aabb_invalidate(vec3s box[2]) { CGLM_INLINE bool glms_aabb_isvalid(vec3s box[2]) { - vec3 rawBox[2]; - glms_vec3_unpack(rawBox, box, 2); - return glm_aabb_isvalid(rawBox); + vec3 rawBox[2]; + glms_vec3_unpack(rawBox, box, 2); + return glm_aabb_isvalid(rawBox); } /*! @@ -197,11 +195,11 @@ glms_aabb_center(vec3s box[2]) { CGLM_INLINE bool glms_aabb_aabb(vec3s box[2], vec3s other[2]) { - vec3 rawBox[2]; - vec3 rawOther[2]; + vec3 rawBox[2]; + vec3 rawOther[2]; - glms_vec3_unpack(rawBox, box, 2); - glms_vec3_unpack(rawOther, other, 2); + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawOther, other, 2); return glm_aabb_aabb(rawBox, rawOther); } @@ -217,10 +215,10 @@ glms_aabb_aabb(vec3s box[2], vec3s other[2]) { CGLM_INLINE bool glms_aabb_sphere(vec3s box[2], vec4s s) { - vec3 rawBox[2]; - - glms_vec3_unpack(rawBox, box, 2); - return glm_aabb_sphere(rawBox, s.raw); + vec3 rawBox[2]; + + glms_vec3_unpack(rawBox, box, 2); + return glm_aabb_sphere(rawBox, s.raw); } /*! @@ -232,9 +230,9 @@ glms_aabb_sphere(vec3s box[2], vec4s s) { CGLM_INLINE bool glms_aabb_point(vec3s box[2], vec3s point) { - vec3 rawBox[2]; + vec3 rawBox[2]; - glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawBox, box, 2); return glm_aabb_point(rawBox, point.raw); } @@ -247,12 +245,12 @@ glms_aabb_point(vec3s box[2], vec3s point) { CGLM_INLINE bool glms_aabb_contains(vec3s box[2], vec3s other[2]) { - vec3 rawBox[2]; - vec3 rawOther[2]; + vec3 rawBox[2]; + vec3 rawOther[2]; - glms_vec3_unpack(rawBox, box, 2); - glms_vec3_unpack(rawOther, other, 2); - return glm_aabb_contains(rawBox, rawOther); + glms_vec3_unpack(rawBox, box, 2); + glms_vec3_unpack(rawOther, other, 2); + return glm_aabb_contains(rawBox, rawOther); } #endif /* cglm_boxs_h */ diff --git a/include/cglm/structs/color.h b/include/cglm/struct/color.h similarity index 93% rename from include/cglm/structs/color.h rename to include/cglm/struct/color.h index 62cf5a8..e9ddc0c 100644 --- a/include/cglm/structs/color.h +++ b/include/cglm/struct/color.h @@ -21,7 +21,7 @@ CGLM_INLINE float glms_luminance(vec3s rgb) { - return glm_luminance(rgb.raw); + return glm_luminance(rgb.raw); } #endif /* cglm_colors_h */ diff --git a/include/cglm/structs/curve.h b/include/cglm/struct/curve.h similarity index 96% rename from include/cglm/structs/curve.h rename to include/cglm/struct/curve.h index 724b0be..0972f0f 100644 --- a/include/cglm/structs/curve.h +++ b/include/cglm/struct/curve.h @@ -34,7 +34,7 @@ CGLM_INLINE float glms_smc(float s, mat4s m, vec4s c) { - return glm_smc(s, m.raw, c.raw); + return glm_smc(s, m.raw, c.raw); } #endif /* cglm_curves_h */ diff --git a/include/cglm/structs/frustum.h b/include/cglm/struct/frustum.h similarity index 83% rename from include/cglm/structs/frustum.h rename to include/cglm/struct/frustum.h index 56bd165..98931f4 100644 --- a/include/cglm/structs/frustum.h +++ b/include/cglm/struct/frustum.h @@ -55,9 +55,9 @@ CGLM_INLINE void glms_frustum_planes(mat4s m, vec4s dest[6]) { - vec4 rawDest[6]; - glm_frustum_planes(m.raw, rawDest); - glms_vec4_pack(dest, rawDest, 6); + vec4 rawDest[6]; + glm_frustum_planes(m.raw, rawDest); + glms_vec4_pack(dest, rawDest, 6); } /*! @@ -86,9 +86,9 @@ glms_frustum_planes(mat4s m, vec4s dest[6]) { CGLM_INLINE void glms_frustum_corners(mat4s invMat, vec4s dest[8]) { - vec4 rawDest[8]; - glm_frustum_corners(invMat.raw, rawDest); - glms_vec4_pack(dest, rawDest, 8); + vec4 rawDest[8]; + glm_frustum_corners(invMat.raw, rawDest); + glms_vec4_pack(dest, rawDest, 8); } /*! @@ -100,12 +100,12 @@ glms_frustum_corners(mat4s invMat, vec4s dest[8]) { CGLM_INLINE vec4s glms_frustum_center(vec4s corners[8]) { - vec4 rawCorners[8]; - vec4s r; + vec4 rawCorners[8]; + vec4s r; - glms_vec4_unpack(rawCorners, corners, 8); - glm_frustum_center(rawCorners, r.raw); - return r; + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_center(rawCorners, r.raw); + return r; } /*! @@ -118,12 +118,12 @@ glms_frustum_center(vec4s corners[8]) { CGLM_INLINE void glms_frustum_box(vec4s corners[8], mat4s m, vec3s box[2]) { - vec4 rawCorners[8]; - vec3 rawBox[2]; + vec4 rawCorners[8]; + vec3 rawBox[2]; - glms_vec4_unpack(rawCorners, corners, 8); - glm_frustum_box(rawCorners, m.raw, rawBox); - glms_vec3_pack(box, rawBox, 2); + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_box(rawCorners, m.raw, rawBox); + glms_vec3_pack(box, rawBox, 2); } /*! @@ -144,12 +144,12 @@ glms_frustum_corners_at(vec4s corners[8], float splitDist, float farDist, vec4s planeCorners[4]) { - vec4 rawCorners[8]; - vec4 rawPlaneCorners[4]; + vec4 rawCorners[8]; + vec4 rawPlaneCorners[4]; - glms_vec4_unpack(rawCorners, corners, 8); - glm_frustum_corners_at(rawCorners, splitDist, farDist, rawPlaneCorners); - glms_vec4_pack(planeCorners, rawPlaneCorners, 8); + glms_vec4_unpack(rawCorners, corners, 8); + glm_frustum_corners_at(rawCorners, splitDist, farDist, rawPlaneCorners); + glms_vec4_pack(planeCorners, rawPlaneCorners, 8); } #endif /* cglm_frustums_h */ diff --git a/include/cglm/structs/io.h b/include/cglm/struct/io.h similarity index 90% rename from include/cglm/structs/io.h rename to include/cglm/struct/io.h index 2467d20..0a084e4 100644 --- a/include/cglm/structs/io.h +++ b/include/cglm/struct/io.h @@ -30,14 +30,14 @@ void glms_mat4_print(mat4s matrix, FILE * __restrict ostream) { - glm_mat4_print(matrix.raw, ostream); + glm_mat4_print(matrix.raw, ostream); } CGLM_INLINE void glms_mat3_print(mat3s matrix, FILE * __restrict ostream) { - glm_mat3_print(matrix.raw, ostream); + glm_mat3_print(matrix.raw, ostream); } CGLM_INLINE @@ -73,10 +73,10 @@ void glms_aabb_print(vec3s bbox[2], const char * __restrict tag, FILE * __restrict ostream) { - vec3 rawBbox[2]; + vec3 rawBbox[2]; - glms_vec3_unpack(rawBbox, bbox, 2); - glm_aabb_print(rawBbox, tag, ostream); + glms_vec3_unpack(rawBbox, bbox, 2); + glm_aabb_print(rawBbox, tag, ostream); } #endif /* cglm_ios_h */ diff --git a/include/cglm/structs/mat3.h b/include/cglm/struct/mat3.h similarity index 100% rename from include/cglm/structs/mat3.h rename to include/cglm/struct/mat3.h diff --git a/include/cglm/structs/mat4.h b/include/cglm/struct/mat4.h similarity index 99% rename from include/cglm/structs/mat4.h rename to include/cglm/struct/mat4.h index 40c7291..5947dc0 100644 --- a/include/cglm/structs/mat4.h +++ b/include/cglm/struct/mat4.h @@ -241,11 +241,11 @@ CGLM_INLINE mat4s glms_mat4_mulN(mat4s * __restrict matrices[], uint32_t len) { CGLM_ALIGN_MAT mat4s r = GLMS_MAT4_IDENTITY_INIT; - size_t i; + size_t i; for (i = 0; i < len; i++) { - r = glms_mat4_mul(r, *matrices[i]); - } + r = glms_mat4_mul(r, *matrices[i]); + } return r; } diff --git a/include/cglm/structs/plane.h b/include/cglm/struct/plane.h similarity index 93% rename from include/cglm/structs/plane.h rename to include/cglm/struct/plane.h index 6e23482..cea7e8c 100644 --- a/include/cglm/structs/plane.h +++ b/include/cglm/struct/plane.h @@ -33,8 +33,8 @@ CGLM_INLINE vec4s glms_plane_normalize(vec4s plane) { - glm_plane_normalize(plane.raw); - return plane; + glm_plane_normalize(plane.raw); + return plane; } #endif /* cglm_planes_h */ diff --git a/include/cglm/structs/project.h b/include/cglm/struct/project.h similarity index 89% rename from include/cglm/structs/project.h rename to include/cglm/struct/project.h index 1cbb137..d45ba3d 100644 --- a/include/cglm/structs/project.h +++ b/include/cglm/struct/project.h @@ -39,14 +39,14 @@ * @param[in] pos point/position in viewport coordinates * @param[in] invMat matrix (see brief) * @param[in] vp viewport as [x, y, width, height] - * @returns unprojected coordinates + * @returns unprojected coordinates */ CGLM_INLINE vec3s glms_unprojecti(vec3s pos, mat4s invMat, vec4s vp) { - vec3s r; - glm_unprojecti(pos.raw, invMat.raw, vp.raw, r.raw); - return r; + vec3s r; + glm_unprojecti(pos.raw, invMat.raw, vp.raw, r.raw); + return r; } /*! @@ -71,14 +71,14 @@ glms_unprojecti(vec3s pos, mat4s invMat, vec4s vp) { * @param[in] pos point/position in viewport coordinates * @param[in] m matrix (see brief) * @param[in] vp viewport as [x, y, width, height] - * @returns unprojected coordinates + * @returns unprojected coordinates */ CGLM_INLINE vec3s glms_unproject(vec3s pos, mat4s m, vec4s vp) { - vec3s r; - glm_unproject(pos.raw, m.raw, vp.raw, r.raw); - return r; + vec3s r; + glm_unproject(pos.raw, m.raw, vp.raw, r.raw); + return r; } /*! @@ -96,9 +96,9 @@ glms_unproject(vec3s pos, mat4s m, vec4s vp) { CGLM_INLINE vec3s glms_project(vec3s pos, mat4s m, vec4s vp) { - vec3s r; - glm_project(pos.raw, m.raw, vp.raw, r.raw); - return r; + vec3s r; + glm_project(pos.raw, m.raw, vp.raw, r.raw); + return r; } #endif /* cglm_projects_h */ diff --git a/include/cglm/structs/sphere.h b/include/cglm/struct/sphere.h similarity index 100% rename from include/cglm/structs/sphere.h rename to include/cglm/struct/sphere.h diff --git a/include/cglm/structs/vec3-ext.h b/include/cglm/struct/vec3-ext.h similarity index 81% rename from include/cglm/structs/vec3-ext.h rename to include/cglm/struct/vec3-ext.h index 2e5e729..53a1199 100644 --- a/include/cglm/structs/vec3-ext.h +++ b/include/cglm/struct/vec3-ext.h @@ -12,17 +12,17 @@ /* Functions: CGLM_INLINE vec3s glms_vec3_broadcast(float val); - CGLM_INLINE bool glms_vec3_eq(vec3s v, float val); - CGLM_INLINE bool glms_vec3_eq_eps(vec3s v, float val); - CGLM_INLINE bool glms_vec3_eq_all(vec3s v); - CGLM_INLINE bool glms_vec3_eqv(vec3s a, vec3s b); - CGLM_INLINE bool glms_vec3_eqv_eps(vec3s a, vec3s b); - CGLM_INLINE float glms_vec3_max(vec3s v); - CGLM_INLINE float glms_vec3_min(vec3s v); - CGLM_INLINE bool glms_vec3_isnan(vec3s v); - CGLM_INLINE bool glms_vec3_isinf(vec3s v); - CGLM_INLINE bool glms_vec3_isvalid(vec3s v); - CGLM_INLINE vec3s glms_vec3_sign(vec3s v); + CGLM_INLINE bool glms_vec3_eq(vec3s v, float val); + CGLM_INLINE bool glms_vec3_eq_eps(vec3s v, float val); + CGLM_INLINE bool glms_vec3_eq_all(vec3s v); + CGLM_INLINE bool glms_vec3_eqv(vec3s a, vec3s b); + CGLM_INLINE bool glms_vec3_eqv_eps(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_max(vec3s v); + CGLM_INLINE float glms_vec3_min(vec3s v); + CGLM_INLINE bool glms_vec3_isnan(vec3s v); + CGLM_INLINE bool glms_vec3_isinf(vec3s v); + CGLM_INLINE bool glms_vec3_isvalid(vec3s v); + CGLM_INLINE vec3s glms_vec3_sign(vec3s v); CGLM_INLINE vec3s glms_vec3_sqrt(vec3s v); */ @@ -37,8 +37,8 @@ /*! * @brief fill a vector with specified value * - * @param[in] val value - * @returns dest + * @param[in] val value + * @returns dest */ CGLM_INLINE vec3s @@ -170,8 +170,8 @@ glms_vec3_isvalid(vec3s v) { * * Important: It returns 0 for zero/NaN input * - * @param v vector - * @returns sign vector + * @param v vector + * @returns sign vector */ CGLM_INLINE vec3s @@ -185,7 +185,7 @@ glms_vec3_sign(vec3s v) { * @brief square root of each vector item * * @param[in] v vector - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s diff --git a/include/cglm/structs/vec3.h b/include/cglm/struct/vec3.h similarity index 70% rename from include/cglm/structs/vec3.h rename to include/cglm/struct/vec3.h index 766742b..06ad75f 100644 --- a/include/cglm/structs/vec3.h +++ b/include/cglm/struct/vec3.h @@ -17,53 +17,53 @@ Functions: CGLM_INLINE vec3s glms_vec3(vec4s v4); - CGLM_INLINE vec3s glms_vec3_copy(vec3s v); - CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len); - CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len); - CGLM_INLINE vec3s glms_vec3_zero(); - CGLM_INLINE vec3s glms_vec3_one(); - CGLM_INLINE float glms_vec3_dot(vec3s a, vec3s b); - CGLM_INLINE float glms_vec3_norm2(vec3s v); - CGLM_INLINE float glms_vec3_norm(vec3s v); - CGLM_INLINE vec3s glms_vec3_add(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_adds(vec3s a, float s); - CGLM_INLINE vec3s glms_vec3_sub(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_subs(vec3s a, float s); - CGLM_INLINE vec3s glms_vec3_mul(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_scale(vec3s v, float s); - CGLM_INLINE vec3s glms_vec3_scale_as(vec3s v, float s); - CGLM_INLINE vec3s glms_vec3_div(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_divs(vec3s a, float s); - CGLM_INLINE vec3s glms_vec3_addadd(vec3s a, vec3s b, vec3s dest); - CGLM_INLINE vec3s glms_vec3_subadd(vec3s a, vec3s b, vec3s dest); - CGLM_INLINE vec3s glms_vec3_muladd(vec3s a, vec3s b, vec3s dest); - CGLM_INLINE vec3s glms_vec3_muladds(vec3s a, float s, vec3s dest); - CGLM_INLINE vec3s glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest); - CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest); - CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v); - CGLM_INLINE vec3s glms_vec3_negate(vec3s v); - CGLM_INLINE vec3s glms_vec3_inv(vec3s v); - CGLM_INLINE vec3s glms_vec3_normalize(vec3s v); - CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b); - CGLM_INLINE float glms_vec3_distance(vec3s a, vec3s b); - CGLM_INLINE float glms_vec3_angle(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_rotate(vec3s v, float angle, vec3s axis); - CGLM_INLINE vec3s glms_vec3_rotate_m4(mat4s m, vec3s v); - CGLM_INLINE vec3s glms_vec3_rotate_m3(mat3s m, vec3s v); - CGLM_INLINE vec3s glms_vec3_proj(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_center(vec3s a, vec3s b); - CGLM_INLINE float glms_vec3_distance2(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_maxv(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_minv(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_vec3_ortho(vec3s v); - CGLM_INLINE vec3s glms_vec3_clamp(vec3s v, float minVal, float maxVal); - CGLM_INLINE vec3s glms_vec3_lerp(vec3s from, vec3s to, float t); + CGLM_INLINE vec3s glms_vec3_copy(vec3s v); + CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len); + CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len); + CGLM_INLINE vec3s glms_vec3_zero(); + CGLM_INLINE vec3s glms_vec3_one(); + CGLM_INLINE float glms_vec3_dot(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_norm2(vec3s v); + CGLM_INLINE float glms_vec3_norm(vec3s v); + CGLM_INLINE vec3s glms_vec3_add(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_adds(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_sub(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_subs(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_mul(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_scale(vec3s v, float s); + CGLM_INLINE vec3s glms_vec3_scale_as(vec3s v, float s); + CGLM_INLINE vec3s glms_vec3_div(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_divs(vec3s a, float s); + CGLM_INLINE vec3s glms_vec3_addadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_subadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_muladd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_muladds(vec3s a, float s, vec3s dest); + CGLM_INLINE vec3s glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest); + CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v); + CGLM_INLINE vec3s glms_vec3_negate(vec3s v); + CGLM_INLINE vec3s glms_vec3_inv(vec3s v); + CGLM_INLINE vec3s glms_vec3_normalize(vec3s v); + CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_distance(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_angle(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_rotate(vec3s v, float angle, vec3s axis); + CGLM_INLINE vec3s glms_vec3_rotate_m4(mat4s m, vec3s v); + CGLM_INLINE vec3s glms_vec3_rotate_m3(mat3s m, vec3s v); + CGLM_INLINE vec3s glms_vec3_proj(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_center(vec3s a, vec3s b); + CGLM_INLINE float glms_vec3_distance2(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_maxv(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_minv(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_vec3_ortho(vec3s v); + CGLM_INLINE vec3s glms_vec3_clamp(vec3s v, float minVal, float maxVal); + CGLM_INLINE vec3s glms_vec3_lerp(vec3s from, vec3s to, float t); Convenient: CGLM_INLINE vec3s glms_cross(vec3s a, vec3s b); - CGLM_INLINE float glms_dot(vec3s a, vec3s b); - CGLM_INLINE vec3s glms_normalize(vec3s v); + CGLM_INLINE float glms_dot(vec3s a, vec3s b); + CGLM_INLINE vec3s glms_normalize(vec3s v); */ #ifndef cglm_vec3s_h @@ -89,7 +89,7 @@ * @brief init vec3 using vec4 * * @param[in] v4 vector4 - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -103,7 +103,7 @@ glms_vec3(vec4s v4) { * @brief copy all members of [a] to [dest] * * @param[in] a source - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -123,13 +123,13 @@ glms_vec3_copy(vec3s v) { CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { - size_t i; - - for (i = 0; i < len; i++) { - dst[i].x = src[i][0]; - dst[i].y = src[i][1]; - dst[i].z = src[i][2]; - } + size_t i; + + for (i = 0; i < len; i++) { + dst[i].x = src[i][0]; + dst[i].y = src[i][1]; + dst[i].z = src[i][2]; + } } /*! @@ -142,20 +142,20 @@ glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len) { - size_t i; + size_t i; - for (i = 0; i < len; i++) { - dst[i][0] = src[i].x; - dst[i][1] = src[i].y; - dst[i][2] = src[i].z; - } + for (i = 0; i < len; i++) { + dst[i][0] = src[i].x; + dst[i][1] = src[i].y; + dst[i][2] = src[i].z; + } } /*! * @brief make vector zero * - * @param[in] v vector - * @returns zero vector + * @param[in] v vector + * @returns zero vector */ CGLM_INLINE vec3s @@ -168,8 +168,8 @@ glms_vec3_zero() { /*! * @brief make vector one * - * @param[in] v vector - * @returns one vector + * @param[in] v vector + * @returns one vector */ CGLM_INLINE vec3s @@ -228,7 +228,7 @@ glms_vec3_norm(vec3s v) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -243,7 +243,7 @@ glms_vec3_add(vec3s a, vec3s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -258,7 +258,7 @@ glms_vec3_adds(vec3s a, float s) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -273,7 +273,7 @@ glms_vec3_sub(vec3s a, vec3s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -286,9 +286,9 @@ glms_vec3_subs(vec3s a, float s) { /*! * @brief multiply two vector (component-wise multiplication) * - * @param a vector1 - * @param b vector2 - * @returns v3 = (a[0] * b[0], a[1] * b[1], a[2] * b[2]) + * @param a vector1 + * @param b vector2 + * @returns v3 = (a[0] * b[0], a[1] * b[1], a[2] * b[2]) */ CGLM_INLINE vec3s @@ -303,7 +303,7 @@ glms_vec3_mul(vec3s a, vec3s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -318,7 +318,7 @@ glms_vec3_scale(vec3s v, float s) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec3s @@ -333,7 +333,7 @@ glms_vec3_scale_as(vec3s v, float s) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2]) + * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2]) */ CGLM_INLINE vec3s @@ -348,7 +348,7 @@ glms_vec3_div(vec3s a, vec3s b) { * * @param[in] v vector * @param[in] s scalar - * @returns result = (a[0]/s, a[1]/s, a[2]/s) + * @returns result = (a[0]/s, a[1]/s, a[2]/s) */ CGLM_INLINE vec3s @@ -365,7 +365,7 @@ glms_vec3_divs(vec3s a, float s) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a + b) + * @returns dest += (a + b) */ CGLM_INLINE vec3s @@ -381,7 +381,7 @@ glms_vec3_addadd(vec3s a, vec3s b, vec3s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a + b) + * @returns dest += (a + b) */ CGLM_INLINE vec3s @@ -397,7 +397,7 @@ glms_vec3_subadd(vec3s a, vec3s b, vec3s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a * b) + * @returns dest += (a * b) */ CGLM_INLINE vec3s @@ -413,7 +413,7 @@ glms_vec3_muladd(vec3s a, vec3s b, vec3s dest) { * * @param[in] a vector * @param[in] s scalar - * @returns dest += (a * b) + * @returns dest += (a * b) */ CGLM_INLINE vec3s @@ -429,7 +429,7 @@ glms_vec3_muladds(vec3s a, float s, vec3s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += max(a, b) + * @returns dest += max(a, b) */ CGLM_INLINE vec3s @@ -445,7 +445,7 @@ glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += min(a, b) + * @returns dest += min(a, b) */ CGLM_INLINE vec3s @@ -458,7 +458,7 @@ glms_vec3_minadd(vec3s a, vec3s b, vec3s dest) { * @brief negate vector components and store result in dest * * @param[in] v vector - * @returns result vector + * @returns result vector */ CGLM_INLINE vec3s @@ -471,7 +471,7 @@ glms_vec3_flipsign(vec3s v) { * @brief negate vector components * * @param[in] v vector - * @returns negated vector + * @returns negated vector */ CGLM_INLINE vec3s @@ -484,7 +484,7 @@ glms_vec3_negate(vec3s v) { * @brief normalize vec3 and store result in same vec * * @param[in] v vector - * @returns normalized vector + * @returns normalized vector */ CGLM_INLINE vec3s @@ -498,7 +498,7 @@ glms_vec3_normalize(vec3s v) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -513,7 +513,7 @@ glms_vec3_cross(vec3s a, vec3s b) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -540,10 +540,10 @@ glms_vec3_angle(vec3s a, vec3s b) { /*! * @brief rotate vec3 around axis by angle using Rodrigues' rotation formula * - * @param[in] v vector - * @param[in] axis axis vector (must be unit vector) - * @param[in] angle angle by radians - * @returns rotated vector + * @param[in] v vector + * @param[in] axis axis vector (must be unit vector) + * @param[in] angle angle by radians + * @returns rotated vector */ CGLM_INLINE vec3s @@ -563,7 +563,7 @@ glms_vec3_rotate(vec3s v, float angle, vec3s axis) { * * @param[in] m affine matrix or rot matrix * @param[in] v vector - * @returns rotated vector + * @returns rotated vector */ CGLM_INLINE vec3s @@ -578,7 +578,7 @@ glms_vec3_rotate_m4(mat4s m, vec3s v) { * * @param[in] m affine matrix or rot matrix * @param[in] v vector - * @returns rotated vector + * @returns rotated vector */ CGLM_INLINE vec3s @@ -593,7 +593,7 @@ glms_vec3_rotate_m3(mat3s m, vec3s v) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns projected vector + * @returns projected vector */ CGLM_INLINE vec3s @@ -608,7 +608,7 @@ glms_vec3_proj(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns center point + * @returns center point */ CGLM_INLINE vec3s @@ -623,7 +623,7 @@ glms_vec3_center(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @return squared distance (distance * distance) + * @return squared distance (distance * distance) */ CGLM_INLINE float @@ -636,7 +636,7 @@ glms_vec3_distance2(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @return distance + * @return distance */ CGLM_INLINE float @@ -649,7 +649,7 @@ glms_vec3_distance(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -664,7 +664,7 @@ glms_vec3_maxv(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -678,7 +678,7 @@ glms_vec3_minv(vec3s a, vec3s b) { * @brief possible orthogonal/perpendicular vector * * @param[in] v vector - * @returns orthogonal/perpendicular vector + * @returns orthogonal/perpendicular vector */ CGLM_INLINE vec3s @@ -691,10 +691,10 @@ glms_vec3_ortho(vec3s v) { /*! * @brief clamp vector's individual members between min and max values * - * @param[in] v vector - * @param[in] minVal minimum value - * @param[in] maxVal maximum value - * @returns clamped vector + * @param[in] v vector + * @param[in] minVal minimum value + * @param[in] maxVal maximum value + * @returns clamped vector */ CGLM_INLINE vec3s @@ -708,10 +708,10 @@ glms_vec3_clamp(vec3s v, float minVal, float maxVal) { * * formula: from + s * (to - from) * - * @param[in] from from value - * @param[in] to to value - * @param[in] t interpolant (amount) clamped between 0 and 1 - * @returns destination + * @param[in] from from value + * @param[in] to to value + * @param[in] t interpolant (amount) clamped between 0 and 1 + * @returns destination */ CGLM_INLINE vec3s @@ -726,9 +726,9 @@ glms_vec3_lerp(vec3s from, vec3s to, float t) { * * this is just convenient wrapper * - * @param[in] a source 1 - * @param[in] b source 2 - * @returns destination + * @param[in] a source 1 + * @param[in] b source 2 + * @returns destination */ CGLM_INLINE vec3s @@ -745,8 +745,7 @@ glms_cross(vec3s a, vec3s b) { * * @param[in] a vector1 * @param[in] b vector2 - * - * @return dot product + * @return dot product */ CGLM_INLINE float @@ -759,8 +758,8 @@ glms_dot(vec3s a, vec3s b) { * * this is just convenient wrapper * - * @param[in] v vector - * @returns normalized vector + * @param[in] v vector + * @returns normalized vector */ CGLM_INLINE vec3s diff --git a/include/cglm/structs/vec4-ext.h b/include/cglm/struct/vec4-ext.h similarity index 79% rename from include/cglm/structs/vec4-ext.h rename to include/cglm/struct/vec4-ext.h index 0b81b49..fdc5059 100644 --- a/include/cglm/structs/vec4-ext.h +++ b/include/cglm/struct/vec4-ext.h @@ -11,19 +11,19 @@ /* Functions: - CGLM_INLINE vec4s glms_vec4_broadcast(float val); - CGLM_INLINE bool glms_vec4_eq(vec4s v, float val); - CGLM_INLINE bool glms_vec4_eq_eps(vec4s v, float val); - CGLM_INLINE bool glms_vec4_eq_all(vec4s v); - CGLM_INLINE bool glms_vec4_eqv(vec4s a, vec4s b); - CGLM_INLINE bool glms_vec4_eqv_eps(vec4s a, vec4s b); - CGLM_INLINE float glms_vec4_max(vec4s v); - CGLM_INLINE float glms_vec4_min(vec4s v); - CGLM_INLINE bool glms_vec4_isnan(vec4s v); - CGLM_INLINE bool glms_vec4_isinf(vec4s v); - CGLM_INLINE bool glms_vec4_isvalid(vec4s v); - CGLM_INLINE vec4s glms_vec4_sign(vec4s v); - CGLM_INLINE vec4s glms_vec4_sqrt(vec4s v); + CGLM_INLINE vec4s glms_vec4_broadcast(float val); + CGLM_INLINE bool glms_vec4_eq(vec4s v, float val); + CGLM_INLINE bool glms_vec4_eq_eps(vec4s v, float val); + CGLM_INLINE bool glms_vec4_eq_all(vec4s v); + CGLM_INLINE bool glms_vec4_eqv(vec4s a, vec4s b); + CGLM_INLINE bool glms_vec4_eqv_eps(vec4s a, vec4s b); + CGLM_INLINE float glms_vec4_max(vec4s v); + CGLM_INLINE float glms_vec4_min(vec4s v); + CGLM_INLINE bool glms_vec4_isnan(vec4s v); + CGLM_INLINE bool glms_vec4_isinf(vec4s v); + CGLM_INLINE bool glms_vec4_isvalid(vec4s v); + CGLM_INLINE vec4s glms_vec4_sign(vec4s v); + CGLM_INLINE vec4s glms_vec4_sqrt(vec4s v); */ #ifndef cglm_vec4s_ext_h @@ -38,7 +38,7 @@ * @brief fill a vector with specified value * * @param val value - * @returns dest + * @returns dest */ CGLM_INLINE vec4s @@ -170,8 +170,8 @@ glms_vec4_isvalid(vec4s v) { * * Important: It returns 0 for zero/NaN input * - * @param v vector - * @returns sign vector + * @param v vector + * @returns sign vector */ CGLM_INLINE vec4s @@ -185,7 +185,7 @@ glms_vec4_sign(vec4s v) { * @brief square root of each vector item * * @param[in] v vector - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s diff --git a/include/cglm/structs/vec4.h b/include/cglm/struct/vec4.h similarity index 70% rename from include/cglm/structs/vec4.h rename to include/cglm/struct/vec4.h index 2f950a7..8ae7dc9 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/struct/vec4.h @@ -15,39 +15,39 @@ GLM_VEC4_ZERO Functions: - CGLM_INLINE vec4s glms_vec4(vec3s v3, float last); - CGLM_INLINE vec3s glms_vec4_copy3(vec4s v); - CGLM_INLINE vec4s glms_vec4_copy(vec4s v); - CGLM_INLINE vec4s glms_vec4_ucopy(vec4s v); - CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len); - CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len); - CGLM_INLINE float glms_vec4_dot(vec4s a, vec4s b); - CGLM_INLINE float glms_vec4_norm2(vec4s v); - CGLM_INLINE float glms_vec4_norm(vec4s v); - CGLM_INLINE vec4s glms_vec4_add(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_adds(vec4s v, float s); - CGLM_INLINE vec4s glms_vec4_sub(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_subs(vec4s v, float s); - CGLM_INLINE vec4s glms_vec4_mul(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_scale(vec4s v, float s); - CGLM_INLINE vec4s glms_vec4_scale_as(vec4s v, float s); - CGLM_INLINE vec4s glms_vec4_div(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_divs(vec4s v, float s); - CGLM_INLINE vec4s glms_vec4_addadd(vec4s a, vec4s b, vec4s dest); - CGLM_INLINE vec4s glms_vec4_subadd(vec4s a, vec4s b, vec4s dest); - CGLM_INLINE vec4s glms_vec4_muladd(vec4s a, vec4s b, vec4s dest); - CGLM_INLINE vec4s glms_vec4_muladds(vec4s a, float s, vec4s dest); - CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest); - CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest); - CGLM_INLINE vec4s glms_vec4_negate(vec4s v); - CGLM_INLINE vec4s glms_vec4_inv(vec4s v); - CGLM_INLINE vec4s glms_vec4_normalize(vec4s v); - CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_maxv(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_minv(vec4s a, vec4s b); - CGLM_INLINE vec4s glms_vec4_clamp(vec4s v, float minVal, float maxVal); - CGLM_INLINE vec4s glms_vec4_lerp(vec4s from, vec4s to, float t); - CGLM_INLINE vec4s glms_vec4_cubic(float s); + CGLM_INLINE vec4s glms_vec4(vec3s v3, float last); + CGLM_INLINE vec3s glms_vec4_copy3(vec4s v); + CGLM_INLINE vec4s glms_vec4_copy(vec4s v); + CGLM_INLINE vec4s glms_vec4_ucopy(vec4s v); + CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len); + CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len); + CGLM_INLINE float glms_vec4_dot(vec4s a, vec4s b); + CGLM_INLINE float glms_vec4_norm2(vec4s v); + CGLM_INLINE float glms_vec4_norm(vec4s v); + CGLM_INLINE vec4s glms_vec4_add(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_adds(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_sub(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_subs(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_mul(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_scale(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_scale_as(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_div(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_divs(vec4s v, float s); + CGLM_INLINE vec4s glms_vec4_addadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_subadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_muladd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_muladds(vec4s a, float s, vec4s dest); + CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest); + CGLM_INLINE vec4s glms_vec4_negate(vec4s v); + CGLM_INLINE vec4s glms_vec4_inv(vec4s v); + CGLM_INLINE vec4s glms_vec4_normalize(vec4s v); + CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_maxv(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_minv(vec4s a, vec4s b); + CGLM_INLINE vec4s glms_vec4_clamp(vec4s v, float minVal, float maxVal); + CGLM_INLINE vec4s glms_vec4_lerp(vec4s from, vec4s to, float t); + CGLM_INLINE vec4s glms_vec4_cubic(float s); */ #ifndef cglm_vec4s_h @@ -72,7 +72,7 @@ * * @param[in] v3 vector3 * @param[in] last last item - * @returns destination + * @returns destination */ CGLM_INLINE vec4s @@ -86,7 +86,7 @@ glms_vec4(vec3s v3, float last) { * @brief copy first 3 members of [a] to [dest] * * @param[in] a source - * @returns destination + * @returns destination */ CGLM_INLINE vec3s @@ -100,7 +100,7 @@ glms_vec4_copy3(vec4s v) { * @brief copy all members of [a] to [dest] * * @param[in] v source - * @returns destination + * @returns destination */ CGLM_INLINE vec4s @@ -116,7 +116,7 @@ glms_vec4_copy(vec4s v) { * alignment is not required * * @param[in] v source - * @returns destination + * @returns destination */ CGLM_INLINE vec4s @@ -136,13 +136,13 @@ glms_vec4_ucopy(vec4s v) { CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { - size_t i; - - for (i = 0; i < len; i++) { - dst[i].x = src[i][0]; - dst[i].y = src[i][1]; - dst[i].z = src[i][2]; - } + size_t i; + + for (i = 0; i < len; i++) { + dst[i].x = src[i][0]; + dst[i].y = src[i][1]; + dst[i].z = src[i][2]; + } } /*! @@ -155,41 +155,41 @@ glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len) { - size_t i; + size_t i; - for (i = 0; i < len; i++) { - dst[i][0] = src[i].x; - dst[i][1] = src[i].y; - dst[i][2] = src[i].z; - } + for (i = 0; i < len; i++) { + dst[i][0] = src[i].x; + dst[i][1] = src[i].y; + dst[i][2] = src[i].z; + } } /*! * @brief make vector zero * * @param[in] v vector - * @returns zero vector + * @returns zero vector */ CGLM_INLINE vec4s glms_vec4_zero() { - vec4s r; - glm_vec4_zero(r.raw); - return r; + vec4s r; + glm_vec4_zero(r.raw); + return r; } /*! * @brief make vector one * * @param[in] v vector - * @returns one vector + * @returns one vector */ CGLM_INLINE vec4s glms_vec4_one() { - vec4s r; - glm_vec4_one(r.raw); - return r; + vec4s r; + glm_vec4_one(r.raw); + return r; } /*! @@ -241,7 +241,7 @@ glms_vec4_norm(vec4s v) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -256,7 +256,7 @@ glms_vec4_add(vec4s a, vec4s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -271,7 +271,7 @@ glms_vec4_adds(vec4s v, float s) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -286,7 +286,7 @@ glms_vec4_sub(vec4s a, vec4s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -301,7 +301,7 @@ glms_vec4_subs(vec4s v, float s) { * * @param a vector1 * @param b vector2 - * @returns dest = (a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]) + * @returns dest = (a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]) */ CGLM_INLINE vec4s @@ -316,7 +316,7 @@ glms_vec4_mul(vec4s a, vec4s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -331,7 +331,7 @@ glms_vec4_scale(vec4s v, float s) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -346,7 +346,7 @@ glms_vec4_scale_as(vec4s v, float s) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2], a[3]/b[3]) + * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2], a[3]/b[3]) */ CGLM_INLINE vec4s @@ -361,7 +361,7 @@ glms_vec4_div(vec4s a, vec4s b) { * * @param[in] v vector * @param[in] s scalar - * @returns destination vector + * @returns destination vector */ CGLM_INLINE vec4s @@ -378,7 +378,7 @@ glms_vec4_divs(vec4s v, float s) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a + b) + * @returns dest += (a + b) */ CGLM_INLINE vec4s @@ -394,7 +394,7 @@ glms_vec4_addadd(vec4s a, vec4s b, vec4s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a - b) + * @returns dest += (a - b) */ CGLM_INLINE vec4s @@ -410,7 +410,7 @@ glms_vec4_subadd(vec4s a, vec4s b, vec4s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += (a * b) + * @returns dest += (a * b) */ CGLM_INLINE vec4s @@ -426,7 +426,7 @@ glms_vec4_muladd(vec4s a, vec4s b, vec4s dest) { * * @param[in] a vector * @param[in] s scalar - * @returns dest += (a * b) + * @returns dest += (a * b) */ CGLM_INLINE vec4s @@ -442,7 +442,7 @@ glms_vec4_muladds(vec4s a, float s, vec4s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += max(a, b) + * @returns dest += max(a, b) */ CGLM_INLINE vec4s @@ -458,7 +458,7 @@ glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest) { * * @param[in] a vector 1 * @param[in] b vector 2 - * @returns dest += min(a, b) + * @returns dest += min(a, b) */ CGLM_INLINE vec4s @@ -471,7 +471,7 @@ glms_vec4_minadd(vec4s a, vec4s b, vec4s dest) { * @brief negate vector components and store result in dest * * @param[in] v vector - * @returns result vector + * @returns result vector */ CGLM_INLINE vec4s @@ -483,8 +483,8 @@ glms_vec4_negate(vec4s v) { /*! * @brief normalize vec4 and store result in same vec * - * @param[in] v vector - * @returns normalized vector + * @param[in] v vector + * @returns normalized vector */ CGLM_INLINE vec4s @@ -511,7 +511,7 @@ glms_vec4_distance(vec4s a, vec4s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination + * @returns destination */ CGLM_INLINE vec4s @@ -526,7 +526,7 @@ glms_vec4_maxv(vec4s a, vec4s b) { * * @param[in] a vector1 * @param[in] b vector2 - * @returns destination + * @returns destination */ CGLM_INLINE vec4s @@ -539,10 +539,10 @@ glms_vec4_minv(vec4s a, vec4s b) { /*! * @brief clamp vector's individual members between min and max values * - * @param[in] v vector - * @param[in] minVal minimum value - * @param[in] maxVal maximum value - * @returns clamped vector + * @param[in] v vector + * @param[in] minVal minimum value + * @param[in] maxVal maximum value + * @returns clamped vector */ CGLM_INLINE vec4s @@ -556,10 +556,10 @@ glms_vec4_clamp(vec4s v, float minVal, float maxVal) { * * formula: from + s * (to - from) * - * @param[in] from from value - * @param[in] to to value - * @param[in] t interpolant (amount) clamped between 0 and 1 - * @returns destination + * @param[in] from from value + * @param[in] to to value + * @param[in] t interpolant (amount) clamped between 0 and 1 + * @returns destination */ CGLM_INLINE vec4s @@ -572,15 +572,15 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) { /*! * @brief helper to fill vec4 as [S^3, S^2, S, 1] * - * @param[in] s parameter - * @returns destination + * @param[in] s parameter + * @returns destination */ CGLM_INLINE vec4s glms_vec4_cubic(float s) { - vec4s r; - glm_vec4_cubic(s, r.raw); - return r; + vec4s r; + glm_vec4_cubic(s, r.raw); + return r; } #endif /* cglm_vec4s_h */ diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h index 2d54aa4..f71c286 100644 --- a/include/cglm/types-struct.h +++ b/include/cglm/types-struct.h @@ -53,13 +53,8 @@ typedef union mat3s { float m10, m11, m12; float m20, m21, m22; }; - struct { - vec3s col0; - vec3s col1; - vec3s col2; - }; #endif - vec3s col[3]; + vec3s col[3]; mat3 raw; } mat3s; @@ -67,18 +62,12 @@ typedef union CGLM_ALIGN_MAT mat4s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { float m00, m01, m02, m03; - float m10, m11, m12, m13; - float m20, m21, m22, m23; - float m30, m31, m32, m33; - }; - struct { - vec4s col0; - vec4s col1; - vec4s col2; - vec4s col3; + float m10, m11, m12, m13; + float m20, m21, m22, m23; + float m30, m31, m32, m33; }; #endif - vec4s col[4]; + vec4s col[4]; mat4 raw; } mat4s; diff --git a/test/src/test_common.c b/test/src/test_common.c index 02ccaec..d734e72 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -49,8 +49,7 @@ test_rand_vec3(vec3 dest) { } vec3s -test_rand_vec3s() -{ +test_rand_vec3s() { vec3s r; test_rand_vec3(r.raw); return r; @@ -67,8 +66,7 @@ test_rand_vec4(vec4 dest) { } vec4s -test_rand_vec4s() -{ +test_rand_vec4s() { vec4s r; test_rand_vec4(r.raw); return r; @@ -137,8 +135,7 @@ test_assert_vec3_eq(vec3 v1, vec3 v2) { } void -test_assert_vec3s_eq(vec3s v1, vec3s v2) -{ +test_assert_vec3s_eq(vec3s v1, vec3s v2) { test_assert_vec3_eq(v1.raw, v2.raw); } @@ -151,8 +148,7 @@ test_assert_vec4_eq(vec4 v1, vec4 v2) { } void -test_assert_vec4s_eq(vec4s v1, vec4s v2) -{ +test_assert_vec4s_eq(vec4s v1, vec4s v2) { test_assert_vec4_eq(v1.raw, v2.raw); } From d322a0ba8ff9b7653465e05f396c55fb173672ea Mon Sep 17 00:00:00 2001 From: acoto87 Date: Wed, 22 May 2019 16:24:11 -0500 Subject: [PATCH 11/11] Replace directly assignment with glm_vec{3,4}_copy functions --- include/cglm/struct/vec3.h | 8 ++------ include/cglm/struct/vec4.h | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/include/cglm/struct/vec3.h b/include/cglm/struct/vec3.h index 06ad75f..91389a2 100644 --- a/include/cglm/struct/vec3.h +++ b/include/cglm/struct/vec3.h @@ -126,9 +126,7 @@ glms_vec3_pack(vec3s dst[], vec3 src[], size_t len) { size_t i; for (i = 0; i < len; i++) { - dst[i].x = src[i][0]; - dst[i].y = src[i][1]; - dst[i].z = src[i][2]; + glm_vec3_copy(src[i], dst[i].raw); } } @@ -145,9 +143,7 @@ glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len) { size_t i; for (i = 0; i < len; i++) { - dst[i][0] = src[i].x; - dst[i][1] = src[i].y; - dst[i][2] = src[i].z; + glm_vec3_copy(src[i].raw, dst[i]); } } diff --git a/include/cglm/struct/vec4.h b/include/cglm/struct/vec4.h index 8ae7dc9..f96725f 100644 --- a/include/cglm/struct/vec4.h +++ b/include/cglm/struct/vec4.h @@ -139,9 +139,7 @@ glms_vec4_pack(vec4s dst[], vec4 src[], size_t len) { size_t i; for (i = 0; i < len; i++) { - dst[i].x = src[i][0]; - dst[i].y = src[i][1]; - dst[i].z = src[i][2]; + glm_vec4_copy(src[i], dst[i].raw); } } @@ -158,9 +156,7 @@ glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len) { size_t i; for (i = 0; i < len; i++) { - dst[i][0] = src[i].x; - dst[i][1] = src[i].y; - dst[i][2] = src[i].z; + glm_vec4_copy(src[i].raw, dst[i]); } }