istereo

diff libs/vmath/quat.inl @ 29:fb4c9641059f

added more forgotten files
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:31:15 +0300
parents
children ff055bff6a15
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/quat.inl	Thu Sep 08 08:31:15 2011 +0300
     1.3 @@ -0,0 +1,63 @@
     1.4 +#include "vector.h"
     1.5 +#include "matrix.h"
     1.6 +
     1.7 +#ifdef __cplusplus
     1.8 +extern "C" {
     1.9 +#endif	/* __cplusplus */
    1.10 +
    1.11 +static inline quat_t quat_mul(quat_t q1, quat_t q2)
    1.12 +{
    1.13 +	quat_t res;
    1.14 +	vec3_t v1 = quat_vec(q1);
    1.15 +	vec3_t v2 = quat_vec(q2);
    1.16 +
    1.17 +	res.w = q1.w * q2.w - v3_dot(v1, v2);
    1.18 +	/* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */
    1.19 +	res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
    1.20 +	res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
    1.21 +	res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
    1.22 +	return res;
    1.23 +}
    1.24 +
    1.25 +static inline quat_t quat_conjugate(quat_t q)
    1.26 +{
    1.27 +	q.x = -q.x;
    1.28 +	q.y = -q.y;
    1.29 +	q.z = -q.z;
    1.30 +	return q;
    1.31 +}
    1.32 +
    1.33 +static inline quat_t quat_inverse(quat_t q)
    1.34 +{
    1.35 +	scalar_t lensq = quat_length_sq(q);
    1.36 +	q = quat_conjugate(q);
    1.37 +	q.x /= lensq;
    1.38 +	q.y /= lensq;
    1.39 +	q.z /= lensq;
    1.40 +	q.w /= lensq;
    1.41 +	return q;
    1.42 +}
    1.43 +
    1.44 +static inline void quat_to_mat3(mat3_t res, quat_t q)
    1.45 +{
    1.46 +	m3_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z,   2.0 * q.z * q.x - 2.0 * q.w * q.y,
    1.47 +				 2.0 * q.x * q.y - 2.0 * q.w * q.z,   1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x,
    1.48 +				 2.0 * q.z * q.x + 2.0 * q.w * q.y,   2.0 * q.y * q.z - 2.0 * q.w * q.x,   1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y);
    1.49 +}
    1.50 +
    1.51 +static inline void quat_to_mat4(mat4_t res, quat_t q)
    1.52 +{
    1.53 +	m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z,   2.0 * q.z * q.x - 2.0 * q.w * q.y,   0,
    1.54 +				 2.0 * q.x * q.y - 2.0 * q.w * q.z,   1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x,   0,
    1.55 +				 2.0 * q.z * q.x + 2.0 * q.w * q.y,   2.0 * q.y * q.z - 2.0 * q.w * q.x,   1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
    1.56 +				 0, 0, 0, 1);
    1.57 +}
    1.58 +
    1.59 +#ifdef __cplusplus
    1.60 +}	/* extern "C" */
    1.61 +
    1.62 +inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t)
    1.63 +{
    1.64 +	return slerp(a, b, t);
    1.65 +}
    1.66 +#endif	/* __cplusplus */