istereo

diff libs/vmath/quat_c.c @ 28:c0ae8e668447

added vmath library
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:42 +0300
parents
children ff055bff6a15
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/quat_c.c	Thu Sep 08 08:30:42 2011 +0300
     1.3 @@ -0,0 +1,42 @@
     1.4 +#include <stdio.h>
     1.5 +#include <math.h>
     1.6 +#include "quat.h"
     1.7 +
     1.8 +void quat_print(FILE *fp, quat_t q)
     1.9 +{
    1.10 +	fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
    1.11 +}
    1.12 +
    1.13 +quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
    1.14 +{
    1.15 +	quat_t rq;
    1.16 +	scalar_t half_angle = angle * 0.5;
    1.17 +	scalar_t sin_half = sin(half_angle);
    1.18 +
    1.19 +	rq.w = cos(half_angle);
    1.20 +	rq.x = x * sin_half;
    1.21 +	rq.y = y * sin_half;
    1.22 +	rq.z = z * sin_half;
    1.23 +
    1.24 +	return quat_mul(q, rq);
    1.25 +}
    1.26 +
    1.27 +quat_t quat_rotate_quat(quat_t q, quat_t rotq)
    1.28 +{
    1.29 +	return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
    1.30 +}
    1.31 +
    1.32 +quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
    1.33 +{
    1.34 +	quat_t res;
    1.35 +	scalar_t angle = acos(q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z);
    1.36 +	scalar_t a = sin((1.0f - t) * angle);
    1.37 +	scalar_t b = sin(t * angle);
    1.38 +	scalar_t c = sin(angle);
    1.39 +
    1.40 +	res.x = (q1.x * a + q2.x * b) / c;
    1.41 +	res.y = (q1.y * a + q2.y * b) / c;
    1.42 +	res.z = (q1.z * a + q2.z * b) / c;
    1.43 +	res.w = (q1.w * a + q2.w * b) / c;
    1.44 +	return quat_normalize(res);
    1.45 +}