From 961832164bfe82ee4359a1eb41c76534ddccc280 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Mon, 9 Jul 2012 17:55:58 +0100 Subject: [PATCH] quaternions: Allow multiplication into 'a' arg When multiplying two quaternions, we now implicitly copy the components of the 'a' argument so that the result can be reliably written back to the 'a' argument quaternion without conflicting with the multiplication itself. This is consistent with the cogl_matrix_multiply() api which allows the 'result' and 'a' arguments to point to the same matrix. In debug builds Cogl will assert that the 'b' and 'result' arguments don't point to the same quaternion. Reviewed-by: Neil Roberts (cherry picked from commit 207527313a8957789390069e84189254cf41e88f) --- cogl/cogl-quaternion.c | 15 +++++++++++---- cogl/cogl-quaternion.h | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cogl/cogl-quaternion.c b/cogl/cogl-quaternion.c index a6a275aef..70cfe3eb9 100644 --- a/cogl/cogl-quaternion.c +++ b/cogl/cogl-quaternion.c @@ -434,11 +434,18 @@ cogl_quaternion_multiply (CoglQuaternion *result, const CoglQuaternion *a, const CoglQuaternion *b) { - result->w = a->w * b->w - a->x * b->x - a->y * b->y - a->z * b->z; + float w = a->w; + float x = a->x; + float y = a->y; + float z = a->z; - result->x = a->w * b->x + a->x * b->w + a->y * b->z - a->z * b->y; - result->y = a->w * b->y + a->y * b->w + a->z * b->x - a->x * b->z; - result->z = a->w * b->z + a->z * b->w + a->x * b->y - a->y * b->x; + _COGL_RETURN_IF_FAIL (b != result); + + result->w = w * b->w - x * b->x - y * b->y - z * b->z; + + result->x = w * b->x + x * b->w + y * b->z - z * b->y; + result->y = w * b->y + y * b->w + z * b->x - x * b->z; + result->z = w * b->z + z * b->w + x * b->y - y * b->x; } void diff --git a/cogl/cogl-quaternion.h b/cogl/cogl-quaternion.h index 650160382..a1b3cfb9d 100644 --- a/cogl/cogl-quaternion.h +++ b/cogl/cogl-quaternion.h @@ -394,6 +394,9 @@ cogl_quaternion_invert (CoglQuaternion *quaternion); * so the rotations are applied @right to @left. This is similar to the * combining of matrices. * + * It is possible to multiply the @a quaternion in-place, so + * @result can be equal to @a but can't be equal to @b. + * * Since: 2.0 */ void