diff --git a/include/cglm-mat3.h b/include/cglm-mat3.h index b8ff319..da27989 100644 --- a/include/cglm-mat3.h +++ b/include/cglm-mat3.h @@ -152,4 +152,37 @@ glm_mat3_scale(mat3 m, float s) { m[2][0] *= s; m[2][1] *= s; m[2][2] *= s; } +/*! + * @brief inverse mat3 and store in dest + * + * @param[in] mat matrix + * @param[out] dest inverse matrix + */ +CGLM_INLINE +void +glm_mat3_inv(mat3 mat, mat3 dest) { + float det; + float a, b, c, + d, e, f, + g, h, i; + + a = mat[0][0], b = mat[0][1], c = mat[0][2], + d = mat[1][0], e = mat[1][1], f = mat[1][2], + g = mat[2][0], h = mat[2][1], i = mat[2][2]; + + dest[0][0] = e * i - f * h; + dest[0][1] = -(b * i - h * c); + dest[0][2] = b * f - e * c; + dest[1][0] = -(d * i - g * f); + dest[1][1] = a * i - c * g; + dest[1][2] = -(a * f - d * c); + dest[2][0] = d * h - g * e; + dest[2][1] = -(a * h - g * b); + dest[2][2] = a * e - b * d; + + det = 1.0f / (a * dest[0][0] + b * dest[1][0] + c * dest[2][0]); + + glm_mat3_scale(dest, det); +} + #endif /* cglm_mat3_h */