istereo

view 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 source
1 #include <stdio.h>
2 #include <math.h>
3 #include "quat.h"
5 void quat_print(FILE *fp, quat_t q)
6 {
7 fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
8 }
10 quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
11 {
12 quat_t rq;
13 scalar_t half_angle = angle * 0.5;
14 scalar_t sin_half = sin(half_angle);
16 rq.w = cos(half_angle);
17 rq.x = x * sin_half;
18 rq.y = y * sin_half;
19 rq.z = z * sin_half;
21 return quat_mul(q, rq);
22 }
24 quat_t quat_rotate_quat(quat_t q, quat_t rotq)
25 {
26 return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
27 }
29 quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
30 {
31 quat_t res;
32 scalar_t angle = acos(q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z);
33 scalar_t a = sin((1.0f - t) * angle);
34 scalar_t b = sin(t * angle);
35 scalar_t c = sin(angle);
37 res.x = (q1.x * a + q2.x * b) / c;
38 res.y = (q1.y * a + q2.y * b) / c;
39 res.z = (q1.z * a + q2.z * b) / c;
40 res.w = (q1.w * a + q2.w * b) / c;
41 return quat_normalize(res);
42 }