From b8b37f6c41a7685f202278cba76fc85b0e2225b1 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Wed, 27 Jul 2011 16:23:38 +0100 Subject: [PATCH] quaternion: fix init_from_array and get_rotation_axis In cogl_quaternion_init_from_array we were passing the address of the x component as the destination for memcpy, but that was wrong at least because w is actually the first member in the structure. Another concern raised was whether it was safe to assume that there was no padding within the CoglQuaternion struct with some compilers so we also switch to explicitly indexing each element of the array we want to copy. In practice I think it's pretty safe to assume that padding will only be introduced to ensure members are naturally aligned, but being explicit is readable and it can't hurt to be extra cautious. Another good catch in bug #655228 was that in cogl_quaternion_get_rotation_axis we had a copy and paste error at the end where we finally extract the axis and we were repeatedly calculating just the x component. Now we calculate the y and z components too. Thanks to Bug #655228 for identifying these issues. https://bugzilla.gnome.org/show_bug.cgi?id=655228 Reviewed-by: Neil Roberts --- cogl/cogl-quaternion.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cogl/cogl-quaternion.c b/cogl/cogl-quaternion.c index ff5972064..9403fc7ca 100644 --- a/cogl/cogl-quaternion.c +++ b/cogl/cogl-quaternion.c @@ -121,7 +121,10 @@ void cogl_quaternion_init_from_array (CoglQuaternion *quaternion, const float *array) { - memcpy (&quaternion->x, array, sizeof (float) * 4); + quaternion->w = array[0]; + quaternion->x = array[1]; + quaternion->y = array[2]; + quaternion->z = array[3]; } void @@ -389,8 +392,8 @@ cogl_quaternion_get_rotation_axis (const CoglQuaternion *quaternion, one_over_sin_angle_over_2 = 1.0f / sqrtf (sin_half_angle_sqr); vector->x = quaternion->x * one_over_sin_angle_over_2; - vector->x = quaternion->x * one_over_sin_angle_over_2; - vector->x = quaternion->x * one_over_sin_angle_over_2; + vector->y = quaternion->y * one_over_sin_angle_over_2; + vector->z = quaternion->z * one_over_sin_angle_over_2; } void