Skip to content

Commit

Permalink
Merge pull request #299 from EasyIP2023/feature/glm_mat4_make
Browse files Browse the repository at this point in the history
mat4: add new function glm_mat4_make
  • Loading branch information
recp committed May 15, 2023
2 parents 9772948 + e17f115 commit 7346e91
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/source/mat4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Functions:
#. :c:func:`glm_mat4_swap_col`
#. :c:func:`glm_mat4_swap_row`
#. :c:func:`glm_mat4_rmc`
#. :c:func:`glm_mat4_make`

Functions documentation
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -302,3 +303,13 @@ Functions documentation
Returns:
scalar value e.g. Matrix1x1
.. c:function:: void glm_mat4_make(float * __restrict src, mat4 dest)
Create mat4 matrix from pointer
| NOTE: **@src** must contain 16 elements.
Parameters:
| *[in]* **src** pointer to an array of floats
| *[out]* **dest** destination matrix4x4
4 changes: 4 additions & 0 deletions include/cglm/call/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ CGLM_EXPORT
float
glmc_mat4_rmc(vec4 r, mat4 m, vec4 c);

CGLM_EXPORT
void
glmc_mat4_make(float * __restrict src, mat4 dest);

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 21 additions & 0 deletions include/cglm/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
CGLM_INLINE void glm_mat4_swap_col(mat4 mat, int col1, int col2);
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);
*/

#ifndef cglm_mat_h
Expand Down Expand Up @@ -781,4 +782,24 @@ glm_mat4_rmc(vec4 r, mat4 m, vec4 c) {
return glm_vec4_dot(r, tmp);
}

/*!
* @brief Create mat4 matrix from pointer
*
* @param[in] src pointer to an array of floats
* @param[out] dest matrix
*/
CGLM_INLINE
void
glm_mat4_make(float * __restrict src, mat4 dest) {
dest[0][0] = src[0]; dest[1][0] = src[4];
dest[0][1] = src[1]; dest[1][1] = src[5];
dest[0][2] = src[2]; dest[1][2] = src[6];
dest[0][3] = src[3]; dest[1][3] = src[7];

dest[2][0] = src[8]; dest[3][0] = src[12];
dest[2][1] = src[9]; dest[3][1] = src[13];
dest[2][2] = src[10]; dest[3][2] = src[14];
dest[2][3] = src[11]; dest[3][3] = src[15];
}

#endif /* cglm_mat_h */
13 changes: 13 additions & 0 deletions include/cglm/struct/mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,17 @@ glms_mat4_(rmc)(vec4s r, mat4s m, vec4s c) {
return glm_mat4_rmc(r.raw, m.raw, c.raw);
}

/*!
* @brief Create mat4 matrix from pointer
*
* @param[in] src pointer to an array of floats
* @param[out] dest matrix
*/
CGLM_INLINE
mat4s
glms_mat4_(make)(float * __restrict src, mat4s dest) {
glm_mat4_make(src, dest.raw);
return dest;
}

#endif /* cglms_mat4s_h */
6 changes: 6 additions & 0 deletions src/mat4.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,9 @@ float
glmc_mat4_rmc(vec4 r, mat4 m, vec4 c) {
return glm_mat4_rmc(r, m, c);
}

CGLM_EXPORT
void
glmc_mat4_make(float * __restrict src, mat4 dest) {
glm_mat4_make(src, dest);
}
18 changes: 18 additions & 0 deletions test/src/test_mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define A_MATRIX {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}
#define A_MATRIX3 {{1,2,3},{5,6,7},{9,10,11}}
#define MAT4_ARRAY {1, 5, 2, 7, 12, 1, 4, 6, 0, 8, 1, 0, 6, 5, 0, 1}

TEST_IMPL(GLM_PREFIX, mat4_ucopy) {
mat4 m1 = A_MATRIX;
Expand Down Expand Up @@ -484,5 +485,22 @@ TEST_IMPL(GLM_PREFIX, mat4_rmc) {
TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, mat4_make) {
mat4 dest;
unsigned int i, j;
float src[16] = MAT4_ARRAY;

GLM(mat4_make)(src, dest);

for (i = 0, j = 0; i < sizeof(src) / sizeof(float); i+=4, j++) {
ASSERT(test_eq(dest[j][0], src[i]))
ASSERT(test_eq(dest[j][1], src[i+1]))
ASSERT(test_eq(dest[j][2], src[i+2]))
ASSERT(test_eq(dest[j][3], src[i+3]))
}

TEST_SUCCESS
}

#undef A_MATRIX
#undef A_MATRIX3
4 changes: 4 additions & 0 deletions test/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ TEST_DECLARE(glm_mat4_inv_precise)
TEST_DECLARE(glm_mat4_swap_col)
TEST_DECLARE(glm_mat4_swap_row)
TEST_DECLARE(glm_mat4_rmc)
TEST_DECLARE(glm_mat4_make)

TEST_DECLARE(glmc_mat4_ucopy)
TEST_DECLARE(glmc_mat4_copy)
Expand All @@ -150,6 +151,7 @@ TEST_DECLARE(glmc_mat4_inv_fast)
TEST_DECLARE(glmc_mat4_swap_col)
TEST_DECLARE(glmc_mat4_swap_row)
TEST_DECLARE(glmc_mat4_rmc)
TEST_DECLARE(glmc_mat4_make)

/* mat3 */
TEST_DECLARE(glm_mat3_copy)
Expand Down Expand Up @@ -965,6 +967,7 @@ TEST_LIST {
TEST_ENTRY(glm_mat4_swap_col)
TEST_ENTRY(glm_mat4_swap_row)
TEST_ENTRY(glm_mat4_rmc)
TEST_ENTRY(glm_mat4_make)

TEST_ENTRY(glmc_mat4_ucopy)
TEST_ENTRY(glmc_mat4_copy)
Expand All @@ -991,6 +994,7 @@ TEST_LIST {
TEST_ENTRY(glmc_mat4_swap_col)
TEST_ENTRY(glmc_mat4_swap_row)
TEST_ENTRY(glmc_mat4_rmc)
TEST_ENTRY(glmc_mat4_make)

/* mat3 */
TEST_ENTRY(glm_mat3_copy)
Expand Down

0 comments on commit 7346e91

Please sign in to comment.