aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfgang Draxinger <Wolfgang.Draxinger@draxit.de>2015-07-31 00:30:19 +0200
committerWolfgang Draxinger <Wolfgang.Draxinger@draxit.de>2015-07-31 00:30:19 +0200
commit06b68f6caa214d9f98ada28db837c21e08ed2927 (patch)
treea744078ef0e0fe75ae13d9b25798a962c411d86b
parent543faf6415918e926c1ddc3e04348eed44d5ed37 (diff)
downloadlinmath.h-06b68f6caa214d9f98ada28db837c21e08ed2927.tar.gz
linmath.h-06b68f6caa214d9f98ada28db837c21e08ed2927.tar.bz2
fixed out-of-bounds write in quat_mul_vec3 by replacing with a more elegant method
-rw-r--r--linmath.h22
1 files changed, 16 insertions, 6 deletions
diff --git a/linmath.h b/linmath.h
index a1c66fa..d0e1f00 100644
--- a/linmath.h
+++ b/linmath.h
@@ -476,12 +476,22 @@ static inline void quat_rotate(quat r, float angle, vec3 axis) {
#define quat_norm vec4_norm
static inline void quat_mul_vec3(vec3 r, quat q, vec3 v)
{
- quat v_ = {v[0], v[1], v[2], 0.f};
-
- quat_conj(r, q);
- quat_norm(r, r);
- quat_mul(r, v_, r);
- quat_mul(r, q, r);
+/*
+ * Method by Fabian 'ryg' Giessen (of Farbrausch)
+t = 2 * cross(q.xyz, v)
+v' = v + q.w * t + cross(q.xyz, t)
+ */
+ vec3 t = {q[0], q[1], q[2]};
+ vec3 u = {q[0], q[1], q[2]};
+
+ vec3_mul_cross(t, t, v);
+ vec3_scale(t, t, 2);
+
+ vec3_mul_cross(u, u, t);
+ vec3_scale(t, t, q[3]);
+
+ vec3_add(r, v, t);
+ vec3_add(r, r, u);
}
static inline void mat4x4_from_quat(mat4x4 M, quat q)
{