From c63c6c90aceb7e8ead996775dc9c83f699f60514 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Tue, 17 Apr 2018 11:12:18 +0300 Subject: [PATCH] implement rotate_at --- include/cglm/affine.h | 24 ++++++++++++++++++++++-- include/cglm/call/affine.h | 4 ++++ src/affine.c | 6 ++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/cglm/affine.h b/include/cglm/affine.h index 10f190f..39c4290 100644 --- a/include/cglm/affine.h +++ b/include/cglm/affine.h @@ -400,7 +400,7 @@ glm_rotate_make(mat4 m, float angle, vec3 axis) { } /*! - * @brief rotate existing transform matrix around Z axis by angle and axis + * @brief rotate existing transform matrix around given axis by angle * * this name may change in the future, axis must be normalized. * @@ -439,7 +439,7 @@ glm_rotate_ndc(mat4 m, float angle, vec3 axis_ndc) { } /*! - * @brief rotate existing transform matrix around Z axis by angle and axis + * @brief rotate existing transform matrix around given axis by angle * * @param[in, out] m affine transfrom * @param[in] angle angle (radians) @@ -454,6 +454,26 @@ glm_rotate(mat4 m, float angle, vec3 axis) { glm_rotate_ndc(m, angle, axis_ndc); } +/*! + * @brief rotate existing transform + * around given axis by angle at given pivot point (rotation center) + * + * @param[in, out] m affine transfrom + * @param[in] angle angle (radians) + * @param[in] axis axis + */ +CGLM_INLINE +void +glm_rotate_at(mat4 model, vec3 pivot, float angle, vec3 axis) { + vec3 pivotInv; + + glm_vec_inv_to(pivot, pivotInv); + + glm_translate(model, pivot); + glm_rotate(model, angle, axis); + glm_translate(model, pivotInv); +} + /*! * @brief decompose scale vector * diff --git a/include/cglm/call/affine.h b/include/cglm/call/affine.h index 65dfae0..6d6b872 100644 --- a/include/cglm/call/affine.h +++ b/include/cglm/call/affine.h @@ -85,6 +85,10 @@ CGLM_EXPORT void glmc_rotate(mat4 m, float angle, vec3 axis); +CGLM_EXPORT +void +glmc_rotate_at(mat4 model, vec3 pivot, float angle, vec3 axis); + CGLM_EXPORT void glmc_decompose_scalev(mat4 m, vec3 s); diff --git a/src/affine.c b/src/affine.c index 056947f..793d8ee 100644 --- a/src/affine.c +++ b/src/affine.c @@ -116,6 +116,12 @@ glmc_rotate(mat4 m, float angle, vec3 axis) { glm_rotate(m, angle, axis); } +CGLM_EXPORT +void +glmc_rotate_at(mat4 model, vec3 pivot, float angle, vec3 axis) { + glm_rotate_at(model, pivot, angle, axis); +} + CGLM_EXPORT void glmc_decompose_scalev(mat4 m, vec3 s) {