goat3d

diff libs/vmath/quat.inl @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents
children 9ba3e2fb8a33
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/quat.inl	Sun Sep 29 08:20:19 2013 +0300
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 +libvmath - a vector math library
     1.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +#include "vector.h"
    1.23 +#include "matrix.h"
    1.24 +
    1.25 +#ifdef __cplusplus
    1.26 +extern "C" {
    1.27 +#endif	/* __cplusplus */
    1.28 +
    1.29 +static inline quat_t quat_mul(quat_t q1, quat_t q2)
    1.30 +{
    1.31 +	quat_t res;
    1.32 +	vec3_t v1 = quat_vec(q1);
    1.33 +	vec3_t v2 = quat_vec(q2);
    1.34 +
    1.35 +	res.w = q1.w * q2.w - v3_dot(v1, v2);
    1.36 +	/* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */
    1.37 +	res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
    1.38 +	res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
    1.39 +	res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
    1.40 +	return res;
    1.41 +}
    1.42 +
    1.43 +static inline quat_t quat_conjugate(quat_t q)
    1.44 +{
    1.45 +	q.x = -q.x;
    1.46 +	q.y = -q.y;
    1.47 +	q.z = -q.z;
    1.48 +	return q;
    1.49 +}
    1.50 +
    1.51 +static inline quat_t quat_inverse(quat_t q)
    1.52 +{
    1.53 +	scalar_t lensq = quat_length_sq(q);
    1.54 +	q = quat_conjugate(q);
    1.55 +	q.x /= lensq;
    1.56 +	q.y /= lensq;
    1.57 +	q.z /= lensq;
    1.58 +	q.w /= lensq;
    1.59 +	return q;
    1.60 +}
    1.61 +
    1.62 +static inline void quat_to_mat3(mat3_t res, quat_t q)
    1.63 +{
    1.64 +	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.65 +				 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.66 +				 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.67 +}
    1.68 +
    1.69 +static inline void quat_to_mat4(mat4_t res, quat_t q)
    1.70 +{
    1.71 +	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.72 +				 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.73 +				 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.74 +				 0, 0, 0, 1);
    1.75 +}
    1.76 +
    1.77 +#ifdef __cplusplus
    1.78 +}	/* extern "C" */
    1.79 +
    1.80 +inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t)
    1.81 +{
    1.82 +	return slerp(a, b, t);
    1.83 +}
    1.84 +#endif	/* __cplusplus */