istereo2

annotate libs/vmath/quat_c.c @ 2:81d35769f546

added the tunnel effect source
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Sep 2015 05:51:51 +0300
parents
children
rev   line source
nuclear@2 1 /*
nuclear@2 2 libvmath - a vector math library
nuclear@2 3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@2 4
nuclear@2 5 This program is free software: you can redistribute it and/or modify
nuclear@2 6 it under the terms of the GNU Lesser General Public License as published
nuclear@2 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@2 8 (at your option) any later version.
nuclear@2 9
nuclear@2 10 This program is distributed in the hope that it will be useful,
nuclear@2 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@2 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@2 13 GNU Lesser General Public License for more details.
nuclear@2 14
nuclear@2 15 You should have received a copy of the GNU Lesser General Public License
nuclear@2 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@2 17 */
nuclear@2 18
nuclear@2 19
nuclear@2 20 #include <stdio.h>
nuclear@2 21 #include <math.h>
nuclear@2 22 #include "quat.h"
nuclear@2 23
nuclear@2 24 void quat_print(FILE *fp, quat_t q)
nuclear@2 25 {
nuclear@2 26 fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
nuclear@2 27 }
nuclear@2 28
nuclear@2 29 quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
nuclear@2 30 {
nuclear@2 31 quat_t rq;
nuclear@2 32 scalar_t half_angle = angle * 0.5;
nuclear@2 33 scalar_t sin_half = sin(half_angle);
nuclear@2 34
nuclear@2 35 rq.w = cos(half_angle);
nuclear@2 36 rq.x = x * sin_half;
nuclear@2 37 rq.y = y * sin_half;
nuclear@2 38 rq.z = z * sin_half;
nuclear@2 39
nuclear@2 40 return quat_mul(q, rq);
nuclear@2 41 }
nuclear@2 42
nuclear@2 43 quat_t quat_rotate_quat(quat_t q, quat_t rotq)
nuclear@2 44 {
nuclear@2 45 return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
nuclear@2 46 }
nuclear@2 47
nuclear@2 48 quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
nuclear@2 49 {
nuclear@2 50 quat_t res;
nuclear@2 51 scalar_t angle = acos(q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z);
nuclear@2 52 scalar_t a = sin((1.0f - t) * angle);
nuclear@2 53 scalar_t b = sin(t * angle);
nuclear@2 54 scalar_t c = sin(angle);
nuclear@2 55
nuclear@2 56 res.x = (q1.x * a + q2.x * b) / c;
nuclear@2 57 res.y = (q1.y * a + q2.y * b) / c;
nuclear@2 58 res.z = (q1.z * a + q2.z * b) / c;
nuclear@2 59 res.w = (q1.w * a + q2.w * b) / c;
nuclear@2 60 return quat_normalize(res);
nuclear@2 61 }