3dphotoshoot
diff libs/vmath/quat.h @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/quat.h Sun May 31 00:40:26 2015 +0300 1.3 @@ -0,0 +1,114 @@ 1.4 +/* 1.5 +libvmath - a vector math library 1.6 +Copyright (C) 2004-2015 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 +#ifndef VMATH_QUATERNION_H_ 1.23 +#define VMATH_QUATERNION_H_ 1.24 + 1.25 +#include <stdio.h> 1.26 +#include "vmath_types.h" 1.27 +#include "vector.h" 1.28 + 1.29 +#ifdef __cplusplus 1.30 +extern "C" { 1.31 +#endif /* __cplusplus */ 1.32 + 1.33 +#define quat_cons(s, x, y, z) v4_cons(x, y, z, s) 1.34 +#define quat_vec(q) v3_cons((q).x, (q).y, (q).z) 1.35 +#define quat_s(q) ((q).w) 1.36 +#define quat_identity() quat_cons(1.0, 0.0, 0.0, 0.0) 1.37 +void quat_print(FILE *fp, quat_t q); 1.38 + 1.39 +#define quat_add v4_add 1.40 +#define quat_sub v4_sub 1.41 +#define quat_neg v4_neg 1.42 + 1.43 +static inline quat_t quat_mul(quat_t q1, quat_t q2); 1.44 + 1.45 +static inline quat_t quat_conjugate(quat_t q); 1.46 + 1.47 +#define quat_length v4_length 1.48 +#define quat_length_sq v4_length_sq 1.49 + 1.50 +#define quat_normalize v4_normalize 1.51 +static inline quat_t quat_inverse(quat_t q); 1.52 + 1.53 +quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z); 1.54 +quat_t quat_rotate_quat(quat_t q, quat_t rotq); 1.55 + 1.56 +static inline void quat_to_mat3(mat3_t res, quat_t q); 1.57 +static inline void quat_to_mat4(mat4_t res, quat_t q); 1.58 + 1.59 +#define quat_lerp quat_slerp 1.60 +quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t); 1.61 + 1.62 + 1.63 +#ifdef __cplusplus 1.64 +} /* extern "C" */ 1.65 + 1.66 +/* Quaternion */ 1.67 +class Quaternion { 1.68 +public: 1.69 + scalar_t s; 1.70 + Vector3 v; 1.71 + 1.72 + Quaternion(); 1.73 + Quaternion(scalar_t s, const Vector3 &v); 1.74 + Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z); 1.75 + Quaternion(const Vector3 &axis, scalar_t angle); 1.76 + Quaternion(const quat_t &quat); 1.77 + 1.78 + Quaternion operator +(const Quaternion &quat) const; 1.79 + Quaternion operator -(const Quaternion &quat) const; 1.80 + Quaternion operator -() const; 1.81 + Quaternion operator *(const Quaternion &quat) const; 1.82 + 1.83 + void operator +=(const Quaternion &quat); 1.84 + void operator -=(const Quaternion &quat); 1.85 + void operator *=(const Quaternion &quat); 1.86 + 1.87 + void reset_identity(); 1.88 + 1.89 + Quaternion conjugate() const; 1.90 + 1.91 + scalar_t length() const; 1.92 + scalar_t length_sq() const; 1.93 + 1.94 + void normalize(); 1.95 + Quaternion normalized() const; 1.96 + 1.97 + Quaternion inverse() const; 1.98 + 1.99 + void set_rotation(const Vector3 &axis, scalar_t angle); 1.100 + void rotate(const Vector3 &axis, scalar_t angle); 1.101 + /* note: this is a totally different operation from the above 1.102 + * this treats the quaternion as signifying direction and rotates 1.103 + * it by a rotation quaternion by rot * q * rot' 1.104 + */ 1.105 + void rotate(const Quaternion &q); 1.106 + 1.107 + Matrix3x3 get_rotation_matrix() const; 1.108 +}; 1.109 + 1.110 +Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t); 1.111 +inline Quaternion lerp(const Quaternion &q1, const Quaternion &q2, scalar_t t); 1.112 + 1.113 +#endif /* __cplusplus */ 1.114 + 1.115 +#include "quat.inl" 1.116 + 1.117 +#endif /* VMATH_QUATERNION_H_ */