Merge pull request #467 from recp/textrans

build texture transform matrix helper
This commit is contained in:
Recep Aslantas
2025-04-05 13:52:15 +03:00
committed by GitHub
13 changed files with 191 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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_mat3_textrans(float sx, float sy, float rot, float tx, float ty, mat3 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 */

View File

@@ -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 */

View File

@@ -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 */

View File

@@ -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 */

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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

View File

@@ -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,6 +1357,7 @@ 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)
@@ -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)