nuclear@27: /* nuclear@27: libvmath - a vector math library nuclear@27: Copyright (C) 2004-2011 John Tsiombikas nuclear@27: nuclear@27: This program is free software: you can redistribute it and/or modify nuclear@27: it under the terms of the GNU Lesser General Public License as published nuclear@27: by the Free Software Foundation, either version 3 of the License, or nuclear@27: (at your option) any later version. nuclear@27: nuclear@27: This program is distributed in the hope that it will be useful, nuclear@27: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@27: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@27: GNU Lesser General Public License for more details. nuclear@27: nuclear@27: You should have received a copy of the GNU Lesser General Public License nuclear@27: along with this program. If not, see . nuclear@27: */ nuclear@27: nuclear@27: #include "vector.h" nuclear@27: #include "matrix.h" nuclear@27: nuclear@27: #ifdef __cplusplus nuclear@27: extern "C" { nuclear@27: #endif /* __cplusplus */ nuclear@27: nuclear@28: static VMATH_INLINE quat_t quat_mul(quat_t q1, quat_t q2) nuclear@27: { nuclear@27: quat_t res; nuclear@27: vec3_t v1 = quat_vec(q1); nuclear@27: vec3_t v2 = quat_vec(q2); nuclear@27: nuclear@27: res.w = q1.w * q2.w - v3_dot(v1, v2); nuclear@27: /* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */ nuclear@27: res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y); nuclear@27: res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z); nuclear@27: res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x); nuclear@27: return res; nuclear@27: } nuclear@27: nuclear@28: static VMATH_INLINE quat_t quat_conjugate(quat_t q) nuclear@27: { nuclear@27: q.x = -q.x; nuclear@27: q.y = -q.y; nuclear@27: q.z = -q.z; nuclear@27: return q; nuclear@27: } nuclear@27: nuclear@28: static VMATH_INLINE quat_t quat_inverse(quat_t q) nuclear@27: { nuclear@27: scalar_t lensq = quat_length_sq(q); nuclear@27: q = quat_conjugate(q); nuclear@27: q.x /= lensq; nuclear@27: q.y /= lensq; nuclear@27: q.z /= lensq; nuclear@27: q.w /= lensq; nuclear@27: return q; nuclear@27: } nuclear@27: nuclear@28: static VMATH_INLINE void quat_to_mat3(mat3_t res, quat_t q) nuclear@27: { nuclear@27: 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, nuclear@27: 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, nuclear@27: 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); nuclear@27: } nuclear@27: nuclear@28: static VMATH_INLINE void quat_to_mat4(mat4_t res, quat_t q) nuclear@27: { nuclear@27: 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, nuclear@27: 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, nuclear@27: 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, nuclear@27: 0, 0, 0, 1); nuclear@27: } nuclear@27: nuclear@27: #ifdef __cplusplus nuclear@27: } /* extern "C" */ nuclear@27: nuclear@28: VMATH_INLINE Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t) nuclear@27: { nuclear@27: return slerp(a, b, t); nuclear@27: } nuclear@27: #endif /* __cplusplus */