forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_quat.h
66 lines (47 loc) · 1.27 KB
/
m_quat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef M_QUAT_HDR
#define M_QUAT_HDR
#include "m_vec.h"
namespace m {
struct mat4;
struct quat : vec4 {
constexpr quat();
constexpr quat(const float (&vals)[4]);
constexpr quat(float x, float y, float z, float w);
quat(float angle, const vec3 &vec);
constexpr quat conjugate() const;
// get all 3 axis of the quaternion
void getOrient(vec3 *direction, vec3 *up, vec3 *side) const;
// get matrix of this quaternion
void getMatrix(mat4 *mat) const;
friend quat operator*(const quat &l, const vec3 &v);
friend quat operator*(const quat &l, const quat &r);
friend quat operator*(const quat &l, float k);
quat normalize() const;
};
inline constexpr quat::quat() = default;
inline constexpr quat::quat(const float (&vals)[4])
: vec4(vals)
{
}
inline constexpr quat::quat(float x, float y, float z, float w)
: vec4(x, y, z, w)
{
}
inline quat::quat(float angle, const vec3 &vec) {
float s;
float c;
m::sincos(angle / 2.0f, s, c);
const vec3 normal = vec.normalized();
x = s * normal.x;
y = s * normal.y;
z = s * normal.z;
w = c;
}
inline constexpr quat quat::conjugate() const {
return quat(-x, -y, -z, -w);
}
inline quat quat::normalize() const {
return *this * (1.0f / abs());
}
}
#endif