merge pull request #505 from snowyukitty/fix/non-unit-quaternion-matrices

Fix non-unit quaternion rotation matrices
This commit is contained in:
Recep Aslantas authored and GitHub committed 2026-10-24 23:48:26 +03:00
commit 1e65d6b52e
4 files changed
+239 -44

No files matched your search

+40 -40
View File
@@ -482,22 +482,22 @@ glm_quat_mul(versor p, versor q, versor dest) {
CGLM_INLINE CGLM_INLINE
void void
glm_quat_mat4(versor q, mat4 dest) { glm_quat_mat4(versor q, mat4 dest) {
CGLM_ALIGN(16) versor p;
float w, x, y, z, float w, x, y, z,
xx, yy, zz, xx, yy, zz,
xy, yz, xz, xy, yz, xz,
wx, wy, wz, norm, s; wx, wy, wz;
norm = glm_quat_norm(q); glm_quat_normalize_to(q, p);
s = norm > 0.0f ? 2.0f / norm : 0.0f;
x = q[0]; x = p[0];
y = q[1]; y = p[1];
z = q[2]; z = p[2];
w = q[3]; w = p[3];
xx = s * x * x; xy = s * x * y; wx = s * w * x; xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x;
yy = s * y * y; yz = s * y * z; wy = s * w * y; yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y;
zz = s * z * z; xz = s * x * z; wz = s * w * z; zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z;
dest[0][0] = 1.0f - yy - zz; dest[0][0] = 1.0f - yy - zz;
dest[1][1] = 1.0f - xx - zz; dest[1][1] = 1.0f - xx - zz;
@@ -529,22 +529,22 @@ glm_quat_mat4(versor q, mat4 dest) {
CGLM_INLINE CGLM_INLINE
void void
glm_quat_mat4t(versor q, mat4 dest) { glm_quat_mat4t(versor q, mat4 dest) {
CGLM_ALIGN(16) versor p;
float w, x, y, z, float w, x, y, z,
xx, yy, zz, xx, yy, zz,
xy, yz, xz, xy, yz, xz,
wx, wy, wz, norm, s; wx, wy, wz;
norm = glm_quat_norm(q); glm_quat_normalize_to(q, p);
s = norm > 0.0f ? 2.0f / norm : 0.0f;
x = q[0]; x = p[0];
y = q[1]; y = p[1];
z = q[2]; z = p[2];
w = q[3]; w = p[3];
xx = s * x * x; xy = s * x * y; wx = s * w * x; xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x;
yy = s * y * y; yz = s * y * z; wy = s * w * y; yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y;
zz = s * z * z; xz = s * x * z; wz = s * w * z; zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z;
dest[0][0] = 1.0f - yy - zz; dest[0][0] = 1.0f - yy - zz;
dest[1][1] = 1.0f - xx - zz; dest[1][1] = 1.0f - xx - zz;
@@ -576,22 +576,22 @@ glm_quat_mat4t(versor q, mat4 dest) {
CGLM_INLINE CGLM_INLINE
void void
glm_quat_mat3(versor q, mat3 dest) { glm_quat_mat3(versor q, mat3 dest) {
CGLM_ALIGN(16) versor p;
float w, x, y, z, float w, x, y, z,
xx, yy, zz, xx, yy, zz,
xy, yz, xz, xy, yz, xz,
wx, wy, wz, norm, s; wx, wy, wz;
norm = glm_quat_norm(q); glm_quat_normalize_to(q, p);
s = norm > 0.0f ? 2.0f / norm : 0.0f;
x = q[0]; x = p[0];
y = q[1]; y = p[1];
z = q[2]; z = p[2];
w = q[3]; w = p[3];
xx = s * x * x; xy = s * x * y; wx = s * w * x; xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x;
yy = s * y * y; yz = s * y * z; wy = s * w * y; yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y;
zz = s * z * z; xz = s * x * z; wz = s * w * z; zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z;
dest[0][0] = 1.0f - yy - zz; dest[0][0] = 1.0f - yy - zz;
dest[1][1] = 1.0f - xx - zz; dest[1][1] = 1.0f - xx - zz;
@@ -615,22 +615,22 @@ glm_quat_mat3(versor q, mat3 dest) {
CGLM_INLINE CGLM_INLINE
void void
glm_quat_mat3t(versor q, mat3 dest) { glm_quat_mat3t(versor q, mat3 dest) {
CGLM_ALIGN(16) versor p;
float w, x, y, z, float w, x, y, z,
xx, yy, zz, xx, yy, zz,
xy, yz, xz, xy, yz, xz,
wx, wy, wz, norm, s; wx, wy, wz;
norm = glm_quat_norm(q); glm_quat_normalize_to(q, p);
s = norm > 0.0f ? 2.0f / norm : 0.0f;
x = q[0]; x = p[0];
y = q[1]; y = p[1];
z = q[2]; z = p[2];
w = q[3]; w = p[3];
xx = s * x * x; xy = s * x * y; wx = s * w * x; xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x;
yy = s * y * y; yz = s * y * z; wy = s * w * y; yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y;
zz = s * z * z; xz = s * x * z; wz = s * w * z; zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z;
dest[0][0] = 1.0f - yy - zz; dest[0][0] = 1.0f - yy - zz;
dest[1][1] = 1.0f - xx - zz; dest[1][1] = 1.0f - xx - zz;
+176 -4
View File
@@ -504,9 +504,10 @@ TEST_IMPL(GLM_PREFIX, quat_mul) {
TEST_IMPL(GLM_PREFIX, quat_mat4) { TEST_IMPL(GLM_PREFIX, quat_mat4) {
mat4 m1, m2; mat4 m1, m2;
versor q1, q2, q3; versor q1, q2, q3, scaled, original;
vec3 axis1; vec3 axis1;
vec3 axis2 = {1.9f, 2.3f, 4.5f}; vec3 axis2 = {1.9f, 2.3f, 4.5f};
float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f};
int i; int i;
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
@@ -543,14 +544,47 @@ TEST_IMPL(GLM_PREFIX, quat_mat4) {
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f));
} }
/* Scaling a quaternion must not change its rotation or mutate it. */
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
GLM(rotate_make)(m2, GLM_PI_4f, axis2);
for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) {
glm_vec4_scale(q1, scales[i], scaled);
glm_vec4_copy(scaled, original);
GLM(quat_mat4)(scaled, m1);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
ASSERT(scaled[0] == original[0] && scaled[1] == original[1]);
ASSERT(scaled[2] == original[2] && scaled[3] == original[3]);
}
/* Issue #503: a non-unit half-turn about Z. */
glm_vec4_zero(scaled);
scaled[2] = 2.0f;
GLM(quat_mat4)(scaled, m1);
glm_mat4_identity(m2);
m2[0][0] = m2[1][1] = -1.0f;
ASSERTIFY(test_assert_mat4_eq(m1, m2));
/* Normalizing first avoids overflowing 2 / dot for tiny inputs. */
scaled[2] = 1e-20f;
GLM(quat_mat4)(scaled, m1);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f));
/* Keep the existing identity fallback for a zero quaternion. */
glm_vec4_zero(scaled);
GLM(quat_mat4)(scaled, m1);
glm_mat4_identity(m2);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
TEST_IMPL(GLM_PREFIX, quat_mat4t) { TEST_IMPL(GLM_PREFIX, quat_mat4t) {
mat4 m1, m2; mat4 m1, m2;
versor q1, q2, q3; versor q1, q2, q3, scaled, original;
vec3 axis1; vec3 axis1;
vec3 axis2 = {1.9f, 2.3f, 4.5f}; vec3 axis2 = {1.9f, 2.3f, 4.5f};
float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f};
int i; int i;
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
@@ -594,15 +628,52 @@ TEST_IMPL(GLM_PREFIX, quat_mat4t) {
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f));
} }
/* Scaling a quaternion must not change its rotation or mutate it. */
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
GLM(rotate_make)(m2, GLM_PI_4f, axis2);
for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) {
glm_vec4_scale(q1, scales[i], scaled);
glm_vec4_copy(scaled, original);
GLM(quat_mat4t)(scaled, m1);
glm_mat4_transpose(m1);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
ASSERT(scaled[0] == original[0] && scaled[1] == original[1]);
ASSERT(scaled[2] == original[2] && scaled[3] == original[3]);
}
/* Issue #503: a non-unit half-turn about Z. */
glm_vec4_zero(scaled);
scaled[2] = 2.0f;
GLM(quat_mat4t)(scaled, m1);
glm_mat4_transpose(m1);
glm_mat4_identity(m2);
m2[0][0] = m2[1][1] = -1.0f;
ASSERTIFY(test_assert_mat4_eq(m1, m2));
/* Normalizing first avoids overflowing 2 / dot for tiny inputs. */
scaled[2] = 1e-20f;
GLM(quat_mat4t)(scaled, m1);
glm_mat4_transpose(m1);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f));
/* Keep the existing identity fallback for a zero quaternion. */
glm_vec4_zero(scaled);
GLM(quat_mat4t)(scaled, m1);
glm_mat4_transpose(m1);
glm_mat4_identity(m2);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
TEST_IMPL(GLM_PREFIX, quat_mat3) { TEST_IMPL(GLM_PREFIX, quat_mat3) {
mat4 m1, m2; mat4 m1, m2;
mat3 m3; mat3 m3;
versor q1, q2, q3; versor q1, q2, q3, scaled, original;
vec3 axis1; vec3 axis1;
vec3 axis2 = {1.9f, 2.3f, 4.5f}; vec3 axis2 = {1.9f, 2.3f, 4.5f};
float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f};
int i; int i;
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
@@ -648,15 +719,56 @@ TEST_IMPL(GLM_PREFIX, quat_mat3) {
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f));
} }
/* Scaling a quaternion must not change its rotation or mutate it. */
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
GLM(rotate_make)(m2, GLM_PI_4f, axis2);
for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) {
glm_vec4_scale(q1, scales[i], scaled);
glm_vec4_copy(scaled, original);
GLM(quat_mat3)(scaled, m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
ASSERT(scaled[0] == original[0] && scaled[1] == original[1]);
ASSERT(scaled[2] == original[2] && scaled[3] == original[3]);
}
/* Issue #503: a non-unit half-turn about Z. */
glm_vec4_zero(scaled);
scaled[2] = 2.0f;
GLM(quat_mat3)(scaled, m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
glm_mat4_identity(m2);
m2[0][0] = m2[1][1] = -1.0f;
ASSERTIFY(test_assert_mat4_eq(m1, m2));
/* Normalizing first avoids overflowing 2 / dot for tiny inputs. */
scaled[2] = 1e-20f;
GLM(quat_mat3)(scaled, m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f));
/* Keep the existing identity fallback for a zero quaternion. */
glm_vec4_zero(scaled);
GLM(quat_mat3)(scaled, m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
glm_mat4_identity(m2);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
TEST_IMPL(GLM_PREFIX, quat_mat3t) { TEST_IMPL(GLM_PREFIX, quat_mat3t) {
mat4 m1, m2; mat4 m1, m2;
mat3 m3; mat3 m3;
versor q1, q2, q3; versor q1, q2, q3, scaled, original;
vec3 axis1; vec3 axis1;
vec3 axis2 = {1.9f, 2.3f, 4.5f}; vec3 axis2 = {1.9f, 2.3f, 4.5f};
float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f};
int i; int i;
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
@@ -706,6 +818,50 @@ TEST_IMPL(GLM_PREFIX, quat_mat3t) {
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f));
} }
/* Scaling a quaternion must not change its rotation or mutate it. */
GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f);
GLM(rotate_make)(m2, GLM_PI_4f, axis2);
for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) {
glm_vec4_scale(q1, scales[i], scaled);
glm_vec4_copy(scaled, original);
GLM(quat_mat3t)(scaled, m3);
glm_mat3_transpose(m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
ASSERT(scaled[0] == original[0] && scaled[1] == original[1]);
ASSERT(scaled[2] == original[2] && scaled[3] == original[3]);
}
/* Issue #503: a non-unit half-turn about Z. */
glm_vec4_zero(scaled);
scaled[2] = 2.0f;
GLM(quat_mat3t)(scaled, m3);
glm_mat3_transpose(m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
glm_mat4_identity(m2);
m2[0][0] = m2[1][1] = -1.0f;
ASSERTIFY(test_assert_mat4_eq(m1, m2));
/* Normalizing first avoids overflowing 2 / dot for tiny inputs. */
scaled[2] = 1e-20f;
GLM(quat_mat3t)(scaled, m3);
glm_mat3_transpose(m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f));
/* Keep the existing identity fallback for a zero quaternion. */
glm_vec4_zero(scaled);
GLM(quat_mat3t)(scaled, m3);
glm_mat3_transpose(m3);
glm_mat4_identity(m1);
glm_mat4_ins3(m3, m1);
glm_mat4_identity(m2);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
@@ -843,6 +999,12 @@ TEST_IMPL(GLM_PREFIX, quat_look) {
ASSERTIFY(test_assert_mat4_eq(m1, m2)); ASSERTIFY(test_assert_mat4_eq(m1, m2));
/* A scaled orientation must yield the same camera view. */
glm_vec4_scale(q1, -2.0f, q1);
GLM(quat_look)(v1, q1, m1);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
@@ -1006,6 +1168,16 @@ TEST_IMPL(GLM_PREFIX, quat_rotate) {
ASSERT(test_eq(v1[1], 0.0f)) ASSERT(test_eq(v1[1], 0.0f))
ASSERT(test_eq(v1[2], 1.0f)) ASSERT(test_eq(v1[2], 1.0f))
/* Non-unit orientation must not add scaling to the transform. */
glm_mat4_identity(m1);
glm_mat4_identity(m2);
glm_quatv(q1, GLM_PI_2f, GLM_YUP);
glm_vec4_scale(q1, 2.0f, q1);
GLM(quat_rotate)(m1, q1, m1);
glm_rotate(m2, GLM_PI_2f, GLM_YUP);
ASSERTIFY(test_assert_mat4_eq(m1, m2));
TEST_SUCCESS TEST_SUCCESS
} }
+21
View File
@@ -114,6 +114,27 @@ TEST_IMPL(quats_zero_init) {
TEST_SUCCESS TEST_SUCCESS
} }
TEST_IMPL(quats_mat_nonunit) {
versors q = {{0.0f, 0.0f, 2.0f, 0.0f}};
mat4s m4;
mat3s m3;
mat4 expected4 = GLM_MAT4_IDENTITY_INIT;
mat3 expected3 = GLM_MAT3_IDENTITY_INIT;
expected4[0][0] = expected4[1][1] = -1.0f;
expected3[0][0] = expected3[1][1] = -1.0f;
m4 = glms_quat_mat4(q);
ASSERTIFY(test_assert_mat4_eq(m4.raw, expected4));
m4 = glms_quat_mat4t(q);
ASSERTIFY(test_assert_mat4_eq(m4.raw, expected4));
m3 = glms_quat_mat3(q);
ASSERTIFY(test_assert_mat3_eq(m3.raw, expected3));
m3 = glms_quat_mat3t(q);
ASSERTIFY(test_assert_mat3_eq(m3.raw, expected3));
TEST_SUCCESS
}
TEST_IMPL(vec3s_one_init) { TEST_IMPL(vec3s_one_init) {
vec3s vec3_one = GLMS_VEC3_ONE_INIT; vec3s vec3_one = GLMS_VEC3_ONE_INIT;
vec3 vec3_one_a = GLM_VEC3_ONE_INIT; vec3 vec3_one_a = GLM_VEC3_ONE_INIT;
+2
View File
@@ -1228,6 +1228,7 @@ TEST_DECLARE(mat4x2s_zero)
TEST_DECLARE(mat4x3s_zero_init) TEST_DECLARE(mat4x3s_zero_init)
TEST_DECLARE(mat4x3s_zero) TEST_DECLARE(mat4x3s_zero)
TEST_DECLARE(quats_zero_init) TEST_DECLARE(quats_zero_init)
TEST_DECLARE(quats_mat_nonunit)
TEST_DECLARE(vec3s_one_init) TEST_DECLARE(vec3s_one_init)
TEST_DECLARE(vec3s_zero_init) TEST_DECLARE(vec3s_zero_init)
TEST_DECLARE(vec4s_black_init) TEST_DECLARE(vec4s_black_init)
@@ -2447,6 +2448,7 @@ TEST_LIST {
TEST_ENTRY(mat4x3s_zero_init) TEST_ENTRY(mat4x3s_zero_init)
TEST_ENTRY(mat4x3s_zero) TEST_ENTRY(mat4x3s_zero)
TEST_ENTRY(quats_zero_init) TEST_ENTRY(quats_zero_init)
TEST_ENTRY(quats_mat_nonunit)
TEST_ENTRY(vec3s_one_init) TEST_ENTRY(vec3s_one_init)
TEST_ENTRY(vec3s_zero_init) TEST_ENTRY(vec3s_zero_init)
TEST_ENTRY(vec4s_black_init) TEST_ENTRY(vec4s_black_init)