diff --git a/include/cglm/call/quat.h b/include/cglm/call/quat.h index 0dff506..e40bcdf 100644 --- a/include/cglm/call/quat.h +++ b/include/cglm/call/quat.h @@ -51,6 +51,10 @@ CGLM_EXPORT void glmc_quat_mat4(versor q, mat4 dest); +CGLM_EXPORT +void +glmc_quat_mat3(versor q, mat3 dest); + CGLM_EXPORT void glmc_quat_slerp(versor q, diff --git a/include/cglm/quat.h b/include/cglm/quat.h index 63236b1..1a2cfe3 100644 --- a/include/cglm/quat.h +++ b/include/cglm/quat.h @@ -216,6 +216,42 @@ glm_quat_mat4(versor q, mat4 dest) { dest[3][3] = 1.0f; } +/*! + * @brief convert quaternion to mat3 + * + * @param[in] q quaternion + * @param[out] dest result matrix + */ +CGLM_INLINE +void +glm_quat_mat3(versor q, mat3 dest) { + float w, x, y, z; + float xx, yy, zz; + float xy, yz, xz; + float wx, wy, wz; + + w = q[0]; + x = q[1]; + y = q[2]; + z = q[3]; + + xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x; + yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y; + zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z; + + dest[0][0] = 1.0f - yy - zz; + dest[1][1] = 1.0f - xx - zz; + dest[2][2] = 1.0f - xx - yy; + + dest[0][1] = xy + wz; + dest[1][2] = yz + wx; + dest[2][0] = xz + wy; + + dest[1][0] = xy - wz; + dest[2][1] = yz - wx; + dest[0][2] = xz - wy; +} + /*! * @brief interpolates between two quaternions * using spherical linear interpolation (SLERP) diff --git a/src/quat.c b/src/quat.c index b26d112..9955ce1 100644 --- a/src/quat.c +++ b/src/quat.c @@ -62,6 +62,12 @@ glmc_quat_mat4(versor q, mat4 dest) { glm_quat_mat4(q, dest); } +CGLM_EXPORT +void +glmc_quat_mat3(versor q, mat3 dest) { + glm_quat_mat3(q, dest); +} + CGLM_EXPORT void glmc_quat_slerp(versor q,