From 2ef9c23a6c9a8bd2f96f7310f0a3397b32af4d59 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Thu, 6 Dec 2018 18:01:52 +0300 Subject: [PATCH] vec: normalize cross product helper --- docs/source/vec3.rst | 20 ++++++++++++---- include/cglm/call/vec3.h | 6 ++++- include/cglm/cam.h | 4 +--- include/cglm/vec3.h | 49 ++++++++++++++++++++++++++-------------- include/cglm/vec4.h | 1 - src/vec3.c | 10 ++++++-- 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/docs/source/vec3.rst b/docs/source/vec3.rst index a625f5c..b431528 100644 --- a/docs/source/vec3.rst +++ b/docs/source/vec3.rst @@ -39,7 +39,6 @@ Functions: #. :c:func:`glm_vec3_zero` #. :c:func:`glm_vec3_one` #. :c:func:`glm_vec3_dot` -#. :c:func:`glm_vec3_cross` #. :c:func:`glm_vec3_norm2` #. :c:func:`glm_vec3_norm` #. :c:func:`glm_vec3_add` @@ -65,6 +64,8 @@ Functions: #. :c:func:`glm_vec3_negate_to` #. :c:func:`glm_vec3_normalize` #. :c:func:`glm_vec3_normalize_to` +#. :c:func:`glm_vec3_cross` +#. :c:func:`glm_vec3_crossn` #. :c:func:`glm_vec3_distance2` #. :c:func:`glm_vec3_distance` #. :c:func:`glm_vec3_angle` @@ -125,12 +126,21 @@ Functions documentation .. c:function:: void glm_vec3_cross(vec3 a, vec3 b, vec3 d) - cross product + cross product of two vector (RH) Parameters: - | *[in]* **a** source 1 - | *[in]* **b** source 2 - | *[out]* **d** destination + | *[in]* **a** vector 1 + | *[in]* **b** vector 2 + | *[out]* **dest** destination + +.. c:function:: void glm_vec3_crossn(vec3 a, vec3 b, vec3 dest) + + cross product of two vector (RH) and normalize the result + + Parameters: + | *[in]* **a** vector 1 + | *[in]* **b** vector 2 + | *[out]* **dest** destination .. c:function:: float glm_vec3_norm2(vec3 v) diff --git a/include/cglm/call/vec3.h b/include/cglm/call/vec3.h index 5b77170..9763403 100644 --- a/include/cglm/call/vec3.h +++ b/include/cglm/call/vec3.h @@ -42,7 +42,11 @@ glmc_vec3_dot(vec3 a, vec3 b); CGLM_EXPORT void -glmc_vec3_cross(vec3 a, vec3 b, vec3 d); +glmc_vec3_cross(vec3 a, vec3 b, vec3 dest); + +CGLM_EXPORT +void +glmc_vec3_crossn(vec3 a, vec3 b, vec3 dest); CGLM_EXPORT float diff --git a/include/cglm/cam.h b/include/cglm/cam.h index acfa970..9bb3cdf 100644 --- a/include/cglm/cam.h +++ b/include/cglm/cam.h @@ -323,9 +323,7 @@ glm_lookat(vec3 eye, glm_vec3_sub(center, eye, f); glm_vec3_normalize(f); - glm_vec3_cross(f, up, s); - glm_vec3_normalize(s); - + glm_vec3_crossn(f, up, s); glm_vec3_cross(s, f, u); dest[0][0] = s[0]; diff --git a/include/cglm/vec3.h b/include/cglm/vec3.h index e5994ba..c53c8ab 100644 --- a/include/cglm/vec3.h +++ b/include/cglm/vec3.h @@ -21,7 +21,6 @@ CGLM_INLINE void glm_vec3_zero(vec3 v); CGLM_INLINE void glm_vec3_one(vec3 v); CGLM_INLINE float glm_vec3_dot(vec3 a, vec3 b); - CGLM_INLINE void glm_vec3_cross(vec3 a, vec3 b, vec3 d); CGLM_INLINE float glm_vec3_norm2(vec3 v); CGLM_INLINE float glm_vec3_norm(vec3 v); CGLM_INLINE void glm_vec3_add(vec3 a, vec3 b, vec3 dest); @@ -47,6 +46,8 @@ CGLM_INLINE void glm_vec3_inv_to(vec3 v, vec3 dest); CGLM_INLINE void glm_vec3_normalize(vec3 v); CGLM_INLINE void glm_vec3_normalize_to(vec3 v, vec3 dest); + CGLM_INLINE void glm_vec3_cross(vec3 a, vec3 b, vec3 d); + CGLM_INLINE void glm_vec3_crossn(vec3 a, vec3 b, vec3 dest); CGLM_INLINE float glm_vec3_distance(vec3 a, vec3 b); CGLM_INLINE float glm_vec3_angle(vec3 a, vec3 b); CGLM_INLINE void glm_vec3_rotate(vec3 v, float angle, vec3 axis); @@ -166,22 +167,6 @@ glm_vec3_dot(vec3 a, vec3 b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } -/*! - * @brief vec3 cross product - * - * @param[in] a source 1 - * @param[in] b source 2 - * @param[out] d destination - */ -CGLM_INLINE -void -glm_vec3_cross(vec3 a, vec3 b, vec3 d) { - /* (u2.v3 - u3.v2, u3.v1 - u1.v3, u1.v2 - u2.v1) */ - d[0] = a[1] * b[2] - a[2] * b[1]; - d[1] = a[2] * b[0] - a[0] * b[2]; - d[2] = a[0] * b[1] - a[1] * b[0]; -} - /*! * @brief norm * norm (magnitude) of vec * @@ -521,6 +506,36 @@ glm_vec3_normalize_to(vec3 v, vec3 dest) { glm_vec3_scale(v, 1.0f / norm, dest); } +/*! + * @brief cross product of two vector (RH) + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @param[out] dest destination + */ +CGLM_INLINE +void +glm_vec3_cross(vec3 a, vec3 b, vec3 dest) { + /* (u2.v3 - u3.v2, u3.v1 - u1.v3, u1.v2 - u2.v1) */ + dest[0] = a[1] * b[2] - a[2] * b[1]; + dest[1] = a[2] * b[0] - a[0] * b[2]; + dest[2] = a[0] * b[1] - a[1] * b[0]; +} + +/*! + * @brief cross product of two vector (RH) and normalize the result + * + * @param[in] a vector 1 + * @param[in] b vector 2 + * @param[out] dest destination + */ +CGLM_INLINE +void +glm_vec3_crossn(vec3 a, vec3 b, vec3 dest) { + glm_vec3_cross(a, b, dest); + glm_vec3_normalize(dest); +} + /*! * @brief angle betwen two vector * diff --git a/include/cglm/vec4.h b/include/cglm/vec4.h index 97a915b..0c4f613 100644 --- a/include/cglm/vec4.h +++ b/include/cglm/vec4.h @@ -426,7 +426,6 @@ glm_vec4_divs(vec4 v, float s, vec4 dest) { #endif } - /*! * @brief add two vectors and add result to sum * diff --git a/src/vec3.c b/src/vec3.c index 7b20888..14edaf6 100644 --- a/src/vec3.c +++ b/src/vec3.c @@ -40,8 +40,14 @@ glmc_vec3_dot(vec3 a, vec3 b) { CGLM_EXPORT void -glmc_vec3_cross(vec3 a, vec3 b, vec3 d) { - glm_vec3_cross(a, b, d); +glmc_vec3_cross(vec3 a, vec3 b, vec3 dest) { + glm_vec3_cross(a, b, dest); +} + +CGLM_EXPORT +void +glmc_vec3_crossn(vec3 a, vec3 b, vec3 dest) { + glm_vec3_crossn(a, b, dest); } CGLM_EXPORT