diff --git a/docs/source/mat3.rst b/docs/source/mat3.rst index 34d4ab7..42ff93a 100644 --- a/docs/source/mat3.rst +++ b/docs/source/mat3.rst @@ -35,6 +35,7 @@ Functions: #. :c:func:`glm_mat3_swap_row` #. :c:func:`glm_mat3_rmc` #. :c:func:`glm_mat3_make` +#. :c:func:`glm_mat3_textrans` Functions documentation ~~~~~~~~~~~~~~~~~~~~~~~ @@ -199,3 +200,15 @@ Functions documentation Parameters: | *[in]* **src** pointer to an array of floats | *[out]* **dest** destination matrix3x3 + +.. c:function:: void glm_mat3_textrans(float sx, float sy, float rot, float tx, float ty, mat3 dest) + + Create texture transformation matrix, rotation is in radians CCW/RH + + Parameters: + | *[in]* **sx** scale x + | *[in]* **sy** scale y + | *[in]* **rot** rotation in radians CCW/RH + | *[in]* **tx** translation x + | *[in]* **ty** translation y + | *[out]* **dest** destination matrix3x3 diff --git a/docs/source/mat4.rst b/docs/source/mat4.rst index 0be06ad..16b4246 100644 --- a/docs/source/mat4.rst +++ b/docs/source/mat4.rst @@ -48,6 +48,7 @@ Functions: #. :c:func:`glm_mat4_swap_row` #. :c:func:`glm_mat4_rmc` #. :c:func:`glm_mat4_make` +#. :c:func:`glm_mat4_textrans` Functions documentation ~~~~~~~~~~~~~~~~~~~~~~~ @@ -313,3 +314,15 @@ Functions documentation Parameters: | *[in]* **src** pointer to an array of floats | *[out]* **dest** destination matrix4x4 + +.. c:function:: void glm_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest) + + Create texture transformation matrix, rotation is in radians CCW/RH + + Parameters: + | *[in]* **sx** scale x + | *[in]* **sy** scale y + | *[in]* **rot** rotation in radians CCW/RH + | *[in]* **tx** translation x + | *[in]* **ty** translation y + | *[out]* **dest** destination matrix3x3 diff --git a/include/cglm/call/mat3.h b/include/cglm/call/mat3.h index 3659b6f..47820f9 100644 --- a/include/cglm/call/mat3.h +++ b/include/cglm/call/mat3.h @@ -84,6 +84,10 @@ CGLM_EXPORT void glmc_mat3_make(const float * __restrict src, mat3 dest); +CGLM_EXPORT +void +glmc_mat3_textrans(float sx, float sy, float rot, float tx, float ty, mat3 dest); + #ifdef __cplusplus } #endif diff --git a/include/cglm/call/mat4.h b/include/cglm/call/mat4.h index 5a33f72..f8cd70a 100644 --- a/include/cglm/call/mat4.h +++ b/include/cglm/call/mat4.h @@ -125,6 +125,10 @@ CGLM_EXPORT void glmc_mat4_make(const float * __restrict src, mat4 dest); +CGLM_EXPORT +void +glmc_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest); + #ifdef __cplusplus } #endif diff --git a/include/cglm/mat3.h b/include/cglm/mat3.h index 9d5f0d4..15f1fe3 100644 --- a/include/cglm/mat3.h +++ b/include/cglm/mat3.h @@ -31,6 +31,7 @@ CGLM_INLINE void glm_mat3_swap_row(mat3 mat, int row1, int row2); CGLM_INLINE float glm_mat3_rmc(vec3 r, mat3 m, vec3 c); CGLM_INLINE void glm_mat3_make(float * restrict src, mat3 dest); + CGLM_INLINE void glm_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest); */ #ifndef cglm_mat3_h @@ -448,4 +449,32 @@ glm_mat3_make(const float * __restrict src, mat3 dest) { dest[2][2] = src[8]; } +/*! + * @brief Create mat3 matrix from texture transform parameters + * + * @param[in] sx scale x + * @param[in] sy scale y + * @param[in] rot rotation in radians CCW/RH + * @param[in] tx translate x + * @param[in] ty translate y + * @param[out] dest texture transform matrix + */ +CGLM_INLINE +void +glm_mat3_textrans(float sx, float sy, float rot, float tx, float ty, mat3 dest) { + float c, s; + + c = cosf(rot); + s = sinf(rot); + + glm_mat3_identity(dest); + + dest[0][0] = c * sx; + dest[0][1] = -s * sy; + dest[1][0] = s * sx; + dest[1][1] = c * sy; + dest[2][0] = tx; + dest[2][1] = ty; +} + #endif /* cglm_mat3_h */ diff --git a/include/cglm/mat4.h b/include/cglm/mat4.h index 6563e74..c3fe7fd 100644 --- a/include/cglm/mat4.h +++ b/include/cglm/mat4.h @@ -44,6 +44,7 @@ CGLM_INLINE void glm_mat4_swap_row(mat4 mat, int row1, int row2); CGLM_INLINE float glm_mat4_rmc(vec4 r, mat4 m, vec4 c); CGLM_INLINE void glm_mat4_make(float * restrict src, mat4 dest); + CGLM_INLINE void glm_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest); */ #ifndef cglm_mat_h @@ -799,4 +800,32 @@ glm_mat4_make(const float * __restrict src, mat4 dest) { dest[2][3] = src[11]; dest[3][3] = src[15]; } +/*! + * @brief Create mat4 matrix from texture transform parameters + * + * @param[in] sx scale x + * @param[in] sy scale y + * @param[in] rot rotation in radians CCW/RH + * @param[in] tx translate x + * @param[in] ty translate y + * @param[out] dest texture transform matrix + */ +CGLM_INLINE +void +glm_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest) { + float c, s; + + c = cosf(rot); + s = sinf(rot); + + glm_mat4_identity(dest); + + dest[0][0] = c * sx; + dest[0][1] = -s * sy; + dest[1][0] = s * sx; + dest[1][1] = c * sy; + dest[3][0] = tx; + dest[3][1] = ty; +} + #endif /* cglm_mat_h */ diff --git a/include/cglm/struct/mat3.h b/include/cglm/struct/mat3.h index 4bfdde8..2fae073 100644 --- a/include/cglm/struct/mat3.h +++ b/include/cglm/struct/mat3.h @@ -29,6 +29,7 @@ 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); CGLM_INLINE mat3s glms_mat3_make(const float * __restrict src); + CGLM_INLINE mat3s glms_mat3_textrans(float sx, float sy, float rot, float tx, float ty); */ #ifndef cglms_mat3s_h @@ -300,4 +301,22 @@ glms_mat3_(make)(const float * __restrict src) { return r; } +/*! + * @brief Create mat3 matrix from texture transform parameters + * + * @param[in] sx scale x + * @param[in] sy scale y + * @param[in] rot rotation in radians CCW/RH + * @param[in] tx translate x + * @param[in] ty translate y + * @return texture transform matrix + */ +CGLM_INLINE +mat3s +glms_mat3_(textrans)(float sx, float sy, float rot, float tx, float ty) { + mat3s r; + glm_mat3_textrans(sx, sy, rot, tx, ty, r.raw); + return r; +} + #endif /* cglms_mat3s_h */ diff --git a/include/cglm/struct/mat4.h b/include/cglm/struct/mat4.h index 243439e..663a5fd 100644 --- a/include/cglm/struct/mat4.h +++ b/include/cglm/struct/mat4.h @@ -43,6 +43,7 @@ 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); CGLM_INLINE mat4s glms_mat4_make(const float * __restrict src); + CGLM_INLINE mat4s glms_mat4_textrans(float sx, float sy, float rot, float tx, float ty); */ #ifndef cglms_mat4s_h @@ -474,4 +475,22 @@ glms_mat4_(make)(const float * __restrict src) { return r; } +/*! + * @brief Create mat4 matrix from texture transform parameters + * + * @param[in] sx scale x + * @param[in] sy scale y + * @param[in] rot rotation in radians CCW/RH + * @param[in] tx translate x + * @param[in] ty translate y + * @return texture transform matrix + */ +CGLM_INLINE +mat4s +glms_mat4_(textrans)(float sx, float sy, float rot, float tx, float ty) { + mat4s r; + glm_mat4_textrans(sx, sy, rot, tx, ty, r.raw); + return r; +} + #endif /* cglms_mat4s_h */ diff --git a/src/mat3.c b/src/mat3.c index a325a4c..01a9442 100644 --- a/src/mat3.c +++ b/src/mat3.c @@ -109,3 +109,9 @@ void glmc_mat3_make(const float * __restrict src, mat3 dest) { glm_mat3_make(src, dest); } + +CGLM_EXPORT +void +glmc_mat3_textrans(float sx, float sy, float rot, float tx, float ty, mat3 dest) { + glm_mat3_textrans(sx, sy, rot, tx, ty, dest); +} diff --git a/src/mat4.c b/src/mat4.c index c4d1111..e792bef 100644 --- a/src/mat4.c +++ b/src/mat4.c @@ -169,3 +169,9 @@ void glmc_mat4_make(const float * __restrict src, mat4 dest) { glm_mat4_make(src, dest); } + +CGLM_EXPORT +void +glmc_mat4_textrans(float sx, float sy, float rot, float tx, float ty, mat4 dest) { + glm_mat4_textrans(sx, sy, rot, tx, ty, dest); +} diff --git a/test/src/test_mat3.h b/test/src/test_mat3.h index f890359..c6902ab 100644 --- a/test/src/test_mat3.h +++ b/test/src/test_mat3.h @@ -327,4 +327,26 @@ TEST_IMPL(GLM_PREFIX, mat3_make) { TEST_SUCCESS } +TEST_IMPL(GLM_PREFIX, mat3_textrans) { + mat3 m, expected; + float sx = 2.0f, sy = 3.0f, rot = GLM_PI_4f; + float tx = 10.0f, ty = 20.0f; + + GLM(mat3_textrans)(sx, sy, rot, tx, ty, m); + + ASSERT(test_eq(m[0][0], cosf(rot) * sx)) + ASSERT(test_eq(m[0][1],-sinf(rot) * sy)) + ASSERT(test_eq(m[1][0], sinf(rot) * sx)) + ASSERT(test_eq(m[1][1], cosf(rot) * sy)) + ASSERT(test_eq(m[2][0], tx)) + ASSERT(test_eq(m[2][1], ty)) + + GLM(mat3_textrans)(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, m); + GLM(mat3_identity)(expected); + + ASSERTIFY(test_assert_mat3_eq(m, expected)) + + TEST_SUCCESS +} + #undef A_MATRIX diff --git a/test/src/test_mat4.h b/test/src/test_mat4.h index 4524742..eb9cfae 100644 --- a/test/src/test_mat4.h +++ b/test/src/test_mat4.h @@ -504,5 +504,27 @@ TEST_IMPL(GLM_PREFIX, mat4_make) { TEST_SUCCESS } +TEST_IMPL(GLM_PREFIX, mat4_textrans) { + mat4 m, expected; + float sx = 2.0f, sy = 3.0f, rot = GLM_PI_4f; + float tx = 10.0f, ty = 20.0f; + + GLM(mat4_textrans)(sx, sy, rot, tx, ty, m); + + ASSERT(test_eq(m[0][0], cosf(rot) * sx)) + ASSERT(test_eq(m[0][1],-sinf(rot) * sy)) + ASSERT(test_eq(m[1][0], sinf(rot) * sx)) + ASSERT(test_eq(m[1][1], cosf(rot) * sy)) + ASSERT(test_eq(m[3][0], tx)) + ASSERT(test_eq(m[3][1], ty)) + + GLM(mat4_textrans)(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, m); + GLM(mat4_identity)(expected); + + ASSERTIFY(test_assert_mat4_eq(m, expected)) + + TEST_SUCCESS +} + #undef A_MATRIX #undef A_MATRIX3 diff --git a/test/tests.h b/test/tests.h index effe137..ce3215d 100644 --- a/test/tests.h +++ b/test/tests.h @@ -136,6 +136,7 @@ TEST_DECLARE(glm_mat4_swap_col) TEST_DECLARE(glm_mat4_swap_row) TEST_DECLARE(glm_mat4_rmc) TEST_DECLARE(glm_mat4_make) +TEST_DECLARE(glm_mat4_textrans) TEST_DECLARE(glmc_mat4_ucopy) TEST_DECLARE(glmc_mat4_copy) @@ -218,6 +219,7 @@ TEST_DECLARE(glm_mat3_swap_col) TEST_DECLARE(glm_mat3_swap_row) TEST_DECLARE(glm_mat3_rmc) TEST_DECLARE(glm_mat3_make) +TEST_DECLARE(glm_mat3_textrans) TEST_DECLARE(glmc_mat3_copy) TEST_DECLARE(glmc_mat3_identity) @@ -1355,7 +1357,8 @@ TEST_LIST { TEST_ENTRY(glm_mat4_swap_row) TEST_ENTRY(glm_mat4_rmc) TEST_ENTRY(glm_mat4_make) - + TEST_ENTRY(glm_mat4_textrans) + TEST_ENTRY(glmc_mat4_ucopy) TEST_ENTRY(glmc_mat4_copy) TEST_ENTRY(glmc_mat4_identity) @@ -1437,6 +1440,7 @@ TEST_LIST { TEST_ENTRY(glm_mat3_swap_row) TEST_ENTRY(glm_mat3_rmc) TEST_ENTRY(glm_mat3_make) + TEST_ENTRY(glm_mat3_textrans) TEST_ENTRY(glmc_mat3_copy) TEST_ENTRY(glmc_mat3_identity)