Cogl 2.0 Reference Manual | ||||
---|---|---|---|---|
Top | Description |
Quaternions (Rotations)Quaternions (Rotations) — Functions for initializing and manipulating quaternions. |
CoglQuaternion; void cogl_quaternion_init_identity (CoglQuaternion *quaternion
); void cogl_quaternion_init (CoglQuaternion *quaternion
,float angle
,float x
,float y
,float z
); void cogl_quaternion_init_from_quaternion (CoglQuaternion *quaternion
,CoglQuaternion *src
); void cogl_quaternion_init_from_angle_vector (CoglQuaternion *quaternion
,float angle
,const float *axis3f
); void cogl_quaternion_init_from_array (CoglQuaternion *quaternion
,const float *array
); void cogl_quaternion_init_from_x_rotation (CoglQuaternion *quaternion
,float angle
); void cogl_quaternion_init_from_y_rotation (CoglQuaternion *quaternion
,float angle
); void cogl_quaternion_init_from_z_rotation (CoglQuaternion *quaternion
,float angle
); void cogl_quaternion_init_from_euler (CoglQuaternion *quaternion
,const CoglEuler *euler
); CoglBool cogl_quaternion_equal (const void *v1
,const void *v2
); CoglQuaternion * cogl_quaternion_copy (const CoglQuaternion *src
); void cogl_quaternion_free (CoglQuaternion *quaternion
); float cogl_quaternion_get_rotation_angle (const CoglQuaternion *quaternion
); void cogl_quaternion_get_rotation_axis (const CoglQuaternion *quaternion
,float *vector3
); void cogl_quaternion_normalize (CoglQuaternion *quaternion
); float cogl_quaternion_dot_product (const CoglQuaternion *a
,const CoglQuaternion *b
); void cogl_quaternion_invert (CoglQuaternion *quaternion
); void cogl_quaternion_multiply (CoglQuaternion *result
,const CoglQuaternion *left
,const CoglQuaternion *right
); void cogl_quaternion_pow (CoglQuaternion *quaternion
,float exponent
); void cogl_quaternion_slerp (CoglQuaternion *result
,const CoglQuaternion *a
,const CoglQuaternion *b
,float t
); void cogl_quaternion_nlerp (CoglQuaternion *result
,const CoglQuaternion *a
,const CoglQuaternion *b
,float t
); void cogl_quaternion_squad (CoglQuaternion *result
,const CoglQuaternion *prev
,const CoglQuaternion *a
,const CoglQuaternion *b
,const CoglQuaternion *next
,float t
); const CoglQuaternion * cogl_get_static_identity_quaternion (void
); const CoglQuaternion * cogl_get_static_zero_quaternion (void
);
Quaternions have become a standard form for representing 3D rotations and have some nice properties when compared with other representation such as (roll,pitch,yaw) Euler angles. They can be used to interpolate between different rotations and they don't suffer from a problem called "Gimbal lock" where two of the axis of rotation may become aligned and you loose a degree of freedom. .
typedef struct { float w; float x; float y; float z; } CoglQuaternion;
A quaternion is comprised of a scalar component and a 3D vector
component. The scalar component is normally referred to as w and the
vector might either be referred to as v or a (for axis) or expanded
with the individual components: (x, y, z) A full quaternion would
then be written as [w (x, y, z)]
.
Quaternions can be considered to represent an axis and angle pair although sadly these numbers are buried somewhat under some maths...
For the curious you can see here that a given axis (a) and angle (𝜃) pair are represented in a quaternion as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[w=cos(𝜃/2) ( x=sin(𝜃/2)*a.x, y=sin(𝜃/2)*a.y, z=sin(𝜃/2)*a.x )] |
Unit Quaternions: When using Quaternions to represent spatial orientations for 3D graphics it's always assumed you have a unit quaternion. The magnitude of a quaternion is defined as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
sqrt (w² + x² + y² + z²) |
and a unit quaternion satisfies this equation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Thankfully most of the time we don't actually have to worry about the maths that goes on behind the scenes but if you are curious to learn more here are some external references:
based on the angle of rotation it is cos(𝜃/2) | |
based on the angle of rotation and x component of the axis of rotation it is sin(𝜃/2)*axis.x | |
based on the angle of rotation and y component of the axis of rotation it is sin(𝜃/2)*axis.y | |
based on the angle of rotation and z component of the axis of rotation it is sin(𝜃/2)*axis.z |
void cogl_quaternion_init_identity (CoglQuaternion *quaternion
);
Initializes the quaternion with the canonical quaternion identity [1 (0, 0, 0)] which represents no rotation. Multiplying a quaternion with this identity leaves the quaternion unchanged.
You might also want to consider using
cogl_get_static_identity_quaternion()
.
|
An uninitialized CoglQuaternion |
Since 2.0
void cogl_quaternion_init (CoglQuaternion *quaternion
,float angle
,float x
,float y
,float z
);
Initializes a quaternion that rotates angle
degrees around the
axis vector (x
, y
, z
). The axis vector does not need to be
normalized.
|
An uninitialized CoglQuaternion |
|
The angle you want to rotate around the given axis |
|
The x component of your axis vector about which you want to rotate. |
|
The y component of your axis vector about which you want to rotate. |
|
The z component of your axis vector about which you want to rotate. |
Returns : |
A normalized, unit quaternion representing an orientation
rotated angle degrees around the axis vector (x , y , z ) |
Since 2.0
void cogl_quaternion_init_from_quaternion (CoglQuaternion *quaternion
,CoglQuaternion *src
);
|
A CoglQuaternion |
|
A CoglQuaternion with which to initialize quaternion
|
Since 2.0
void cogl_quaternion_init_from_angle_vector (CoglQuaternion *quaternion
,float angle
,const float *axis3f
);
Initializes a quaternion that rotates angle
degrees around the
given axis
vector. The axis vector does not need to be
normalized.
|
An uninitialized CoglQuaternion |
|
The angle to rotate around axis3f
|
|
your 3 component axis vector about which you want to rotate. |
Returns : |
A normalized, unit quaternion representing an orientation
rotated angle degrees around the given axis vector. |
Since 2.0
void cogl_quaternion_init_from_array (CoglQuaternion *quaternion
,const float *array
);
Initializes a [w (x, y,z)] quaternion directly from an array of 4 floats: [w,x,y,z].
|
A CoglQuaternion |
|
An array of 4 floats w,(x,y,z) |
Since 2.0
void cogl_quaternion_init_from_x_rotation (CoglQuaternion *quaternion
,float angle
);
XXX: check which direction this rotates
|
An uninitialized CoglQuaternion |
|
The angle to rotate around the x axis |
Since 2.0
void cogl_quaternion_init_from_y_rotation (CoglQuaternion *quaternion
,float angle
);
|
An uninitialized CoglQuaternion |
|
The angle to rotate around the y axis |
Since 2.0
void cogl_quaternion_init_from_z_rotation (CoglQuaternion *quaternion
,float angle
);
|
An uninitialized CoglQuaternion |
|
The angle to rotate around the z axis |
Since 2.0
void cogl_quaternion_init_from_euler (CoglQuaternion *quaternion
,const CoglEuler *euler
);
|
A CoglQuaternion |
|
A CoglEuler with which to initialize the quaternion |
Since 2.0
CoglBool cogl_quaternion_equal (const void *v1
,const void *v2
);
Compares that all the components of quaternions a
and b
are
equal.
An epsilon value is not used to compare the float components, but the == operator is at least used so that 0 and -0 are considered equal.
|
A CoglQuaternion |
|
A CoglQuaternion |
Returns : |
TRUE if the quaternions are equal else FALSE . |
Since 2.0
CoglQuaternion * cogl_quaternion_copy (const CoglQuaternion *src
);
Allocates a new CoglQuaternion on the stack and initializes it with
the same values as src
.
|
A CoglQuaternion |
Returns : |
A newly allocated CoglQuaternion which should be freed
using cogl_quaternion_free()
|
Since 2.0
void cogl_quaternion_free (CoglQuaternion *quaternion
);
Frees a CoglQuaternion that was previously allocated via
cogl_quaternion_copy()
.
|
A CoglQuaternion |
Since 2.0
float cogl_quaternion_get_rotation_angle (const CoglQuaternion *quaternion
);
|
A CoglQuaternion |
Since 2.0
void cogl_quaternion_get_rotation_axis (const CoglQuaternion *quaternion
,float *vector3
);
|
A CoglQuaternion |
|
an allocated 3-float array. [out] |
Since 2.0
void cogl_quaternion_normalize (CoglQuaternion *quaternion
);
|
A CoglQuaternion |
Since 2.0
float cogl_quaternion_dot_product (const CoglQuaternion *a
,const CoglQuaternion *b
);
|
A CoglQuaternion |
|
A CoglQuaternion |
Since 2.0
void cogl_quaternion_invert (CoglQuaternion *quaternion
);
|
A CoglQuaternion |
Since 2.0
void cogl_quaternion_multiply (CoglQuaternion *result
,const CoglQuaternion *left
,const CoglQuaternion *right
);
This combines the rotations of two quaternions into result
. The
operation is not commutative so the order is important because AxB
!= BxA. Cogl follows the standard convention for quaternions here
so the rotations are applied right
to left
. This is similar to the
combining of matrices.
a
quaternion in-place, so
result
can be equal to a
but can't be equal to b
.
|
The destination CoglQuaternion |
|
The second CoglQuaternion rotation to apply |
|
The first CoglQuaternion rotation to apply |
Since 2.0
void cogl_quaternion_pow (CoglQuaternion *quaternion
,float exponent
);
|
A CoglQuaternion |
|
the exponent |
Since 2.0
void cogl_quaternion_slerp (CoglQuaternion *result
,const CoglQuaternion *a
,const CoglQuaternion *b
,float t
);
Performs a spherical linear interpolation between two quaternions.
Noteable properties:
cogl_quaternion_nlerp()
|
The destination CoglQuaternion |
|
The first CoglQuaternion |
|
The second CoglQuaternion |
|
The factor in the range [0,1] used to interpolate between
quaternion a and b . |
void cogl_quaternion_nlerp (CoglQuaternion *result
,const CoglQuaternion *a
,const CoglQuaternion *b
,float t
);
Performs a normalized linear interpolation between two quaternions.
That is it does a linear interpolation of the quaternion components
and then normalizes the result. This will follow the shortest arc
between the two orientations (just like the slerp()
function) but
will not progress at a constant speed. Unlike slerp()
nlerp is
commutative which is useful if you are blending animations
together. (I.e. nlerp (tmp, a, b) followed by nlerp (result, tmp,
d) is the same as nlerp (tmp, a, d) followed by nlerp (result, tmp,
b)). Finally nlerp is cheaper than slerp so it can be a good choice
if you don't need the constant speed property of the slerp()
function.
Notable properties:
cogl_quaternion_slerp()
|
The destination CoglQuaternion |
|
The first CoglQuaternion |
|
The second CoglQuaternion |
|
The factor in the range [0,1] used to interpolate between
quaterion a and b . |
void cogl_quaternion_squad (CoglQuaternion *result
,const CoglQuaternion *prev
,const CoglQuaternion *a
,const CoglQuaternion *b
,const CoglQuaternion *next
,float t
);
|
The destination CoglQuaternion |
|
A CoglQuaternion used before a
|
|
The first CoglQuaternion |
|
The second CoglQuaternion |
|
A CoglQuaternion that will be used after b
|
|
The factor in the range [0,1] used to interpolate between
quaternion a and b . |
Since 2.0
const CoglQuaternion * cogl_get_static_identity_quaternion
(void
);
Returns a pointer to a singleton quaternion constant describing the canonical identity [1 (0, 0, 0)] which represents no rotation.
If you multiply a quaternion with the identity quaternion you will get back the same value as the original quaternion.
Returns : |
A pointer to an identity quaternion |
Since 2.0
const CoglQuaternion * cogl_get_static_zero_quaternion (void
);
Returns : |
a pointer to a singleton quaternion constant describing a rotation of 180 degrees around a degenerate axis: [0 (0, 0, 0)] |
Since 2.0