use mul_rot for rotations to make thrm faster

This commit is contained in:
Recep Aslantas
2018-04-18 14:12:56 +03:00
parent be0e3fc9f2
commit 4dbcd28fdb
3 changed files with 37 additions and 53 deletions

View File

@@ -277,19 +277,18 @@ glm_scale_uni(mat4 m, float s) {
CGLM_INLINE
void
glm_rotate_x(mat4 m, float angle, mat4 dest) {
float cosVal;
float sinVal;
mat4 t = GLM_MAT4_IDENTITY_INIT;
float c, s;
cosVal = cosf(angle);
sinVal = sinf(angle);
c = cosf(angle);
s = sinf(angle);
t[1][1] = cosVal;
t[1][2] = sinVal;
t[2][1] = -sinVal;
t[2][2] = cosVal;
t[1][1] = c;
t[1][2] = s;
t[2][1] = -s;
t[2][2] = c;
glm_mat4_mul(m, t, dest);
glm_mul_rot(m, t, dest);
}
/*!
@@ -303,19 +302,18 @@ glm_rotate_x(mat4 m, float angle, mat4 dest) {
CGLM_INLINE
void
glm_rotate_y(mat4 m, float angle, mat4 dest) {
float cosVal;
float sinVal;
mat4 t = GLM_MAT4_IDENTITY_INIT;
float c, s;
cosVal = cosf(angle);
sinVal = sinf(angle);
c = cosf(angle);
s = sinf(angle);
t[0][0] = cosVal;
t[0][2] = -sinVal;
t[2][0] = sinVal;
t[2][2] = cosVal;
t[0][0] = c;
t[0][2] = -s;
t[2][0] = s;
t[2][2] = c;
glm_mat4_mul(m, t, dest);
glm_mul_rot(m, t, dest);
}
/*!
@@ -329,19 +327,18 @@ glm_rotate_y(mat4 m, float angle, mat4 dest) {
CGLM_INLINE
void
glm_rotate_z(mat4 m, float angle, mat4 dest) {
float cosVal;
float sinVal;
mat4 t = GLM_MAT4_IDENTITY_INIT;
float c, s;
cosVal = cosf(angle);
sinVal = sinf(angle);
c = cosf(angle);
s = sinf(angle);
t[0][0] = cosVal;
t[0][1] = sinVal;
t[1][0] = -sinVal;
t[1][1] = cosVal;
t[0][0] = c;
t[0][1] = s;
t[1][0] = -s;
t[1][1] = c;
glm_mat4_mul(m, t, dest);
glm_mul_rot(m, t, dest);
}
/*!
@@ -387,31 +384,9 @@ glm_rotate_make(mat4 m, float angle, vec3 axis) {
CGLM_INLINE
void
glm_rotate(mat4 m, float angle, vec3 axis) {
mat4 rot, tmp;
mat4 rot;
glm_rotate_make(rot, angle, axis);
glm_vec4_scale(m[0], rot[0][0], tmp[1]);
glm_vec4_scale(m[1], rot[0][1], tmp[0]);
glm_vec4_add(tmp[1], tmp[0], tmp[1]);
glm_vec4_scale(m[2], rot[0][2], tmp[0]);
glm_vec4_add(tmp[1], tmp[0], tmp[1]);
glm_vec4_scale(m[0], rot[1][0], tmp[2]);
glm_vec4_scale(m[1], rot[1][1], tmp[0]);
glm_vec4_add(tmp[2], tmp[0], tmp[2]);
glm_vec4_scale(m[2], rot[1][2], tmp[0]);
glm_vec4_add(tmp[2], tmp[0], tmp[2]);
glm_vec4_scale(m[0], rot[2][0], tmp[3]);
glm_vec4_scale(m[1], rot[2][1], tmp[0]);
glm_vec4_add(tmp[3], tmp[0], tmp[3]);
glm_vec4_scale(m[2], rot[2][2], tmp[0]);
glm_vec4_add(tmp[3], tmp[0], tmp[3]);
glm_vec4_copy(tmp[1], m[0]);
glm_vec4_copy(tmp[2], m[1]);
glm_vec4_copy(tmp[3], m[2]);
glm_mul_rot(m, rot, m);
}
/*!

View File

@@ -15,12 +15,12 @@
#endif
typedef float vec2[2];
typedef float vec3[3];
typedef CGLM_ALIGN(8) float vec3[3];
typedef int ivec3[3];
typedef CGLM_ALIGN(16) float vec4[4];
typedef vec3 mat3[3];
typedef vec4 mat4[4];
typedef CGLM_ALIGN(16) vec4 mat4[4];
typedef vec4 versor;

View File

@@ -91,6 +91,15 @@ test_affine(void **state) {
glm_rotate_z(t2, M_PI_4, t2);
test_assert_mat4_eq(t2, t3);
/* test rotate */
glmc_rotate_make(t1, M_PI_4, (vec3){0, 0, 1});
glm_translate_make(t2, (vec3){34, 57, 36});
glmc_mat4_mul(t2, t1, t3); /* T * R */
glmc_rotate(t2, M_PI_4, (vec3){0, 0, 1});
test_assert_mat4_eq(t3, t2);
/* test scale_uni */
glmc_rotate_make(t1, M_PI_4, GLM_YUP);
glm_translate_make(t2, (vec3){34, 57, 36});