[Cogl] [PATCH] quaternions: Allow multiplication into 'a' arg

Robert Bragg robert at sixbynine.org
Mon Jul 9 10:03:10 PDT 2012


From: Robert Bragg <robert at linux.intel.com>

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.
---
 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 a6a275a..70cfe3e 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 6501603..a1b3cfb 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.
  *
+ * <note>It is possible to multiply the @a quaternion in-place, so
+ * @result can be equal to @a but can't be equal to @b.</note>
+ *
  * Since: 2.0
  */
 void
-- 
1.7.7.6



More information about the Cogl mailing list