-
Notifications
You must be signed in to change notification settings - Fork 8
/
m_mat.h
191 lines (152 loc) · 3.99 KB
/
m_mat.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef M_MAT_HDR
#define M_MAT_HDR
#include "m_vec.h"
namespace m {
struct quat;
struct perspective {
perspective();
float fov;
float width;
float height;
float nearp;
float farp;
};
inline perspective::perspective()
: fov(0.0f)
, width(0.0f)
, height(0.0f)
, nearp(0.0f)
, farp(0.0f)
{
}
///! mat4x4
struct mat4 {
static const mat4 kIdentity;
vec4 a, b, c, d;
constexpr mat4() = default;
constexpr mat4(const vec4 &a, const vec4 &b, const vec4 &c, const vec4 &d);
static mat4 identity();
mat4 inverse() const;
mat4 operator*(const mat4 &t) const;
static mat4 project(float angle, float nearClip, float farClip, float bias = 0);
static mat4 project(const perspective &p);
static mat4 scale(const vec3 &s);
static mat4 rotate(const vec3 &r);
static mat4 translate(const vec3 &t);
static mat4 lookat(const vec3 &target, const vec3 &up);
static mat4 lookat(const vec3 &position, const quat &q);
float *ptr();
const float *ptr() const;
private:
#ifdef __SSE2__
#else
static float det2x2(float a, float b, float c, float d);
static float det3x3(float a1, float a2, float a3,
float b1, float b2, float b3,
float c1, float c2, float c3);
#endif
};
inline constexpr mat4::mat4(const vec4 &a, const vec4 &b, const vec4 &c, const vec4 &d)
: a(a)
, b(b)
, c(c)
, d(d)
{
}
inline float *mat4::ptr() {
return &a[0];
}
inline const float *mat4::ptr() const {
return &a[0];
}
///! mat3x3
struct mat3x3 {
vec3 a, b, c;
mat3x3();
mat3x3(const vec3 &a, const vec3 &b, const vec3 &c);
explicit mat3x3(const quat &q);
explicit mat3x3(const quat &q, const vec3 &scale);
protected:
void convertQuaternion(const quat &q);
};
inline mat3x3::mat3x3() = default;
inline mat3x3::mat3x3(const vec3 &a, const vec3 &b, const vec3 &c)
: a(a)
, b(b)
, c(c)
{
}
inline mat3x3::mat3x3(const quat &q) {
convertQuaternion(q);
}
inline mat3x3::mat3x3(const quat &q, const vec3 &scale) {
convertQuaternion(q);
a *= scale;
b *= scale;
c *= scale;
}
///! mat3x4
struct mat3x4 {
vec4 a, b, c;
mat3x4();
mat3x4(const vec4 &a, const vec4 &b, const vec4 &c);
explicit mat3x4(const mat3x3 &rot, const vec3 &trans);
explicit mat3x4(const quat &rot, const vec3 &trans);
explicit mat3x4(const quat &rot, const vec3 &trans, const vec3 &scale);
void invert(const mat3x4 &o);
mat3x4 &operator*=(const mat3x4 &o);
mat3x4 &operator+=(const mat3x4 &o);
mat3x4 &operator*=(float k);
friend mat3x4 operator*(const mat3x4 &l, const mat3x4 &r);
friend mat3x4 operator+(const mat3x4 &l, const mat3x4 &r);
friend mat3x4 operator*(const mat3x4 &l, float k);
};
inline mat3x4::mat3x4() = default;
inline mat3x4::mat3x4(const vec4 &a, const vec4 &b, const vec4 &c)
: a(a)
, b(b)
, c(c)
{
}
inline mat3x4::mat3x4(const mat3x3 &rot, const vec3 &trans)
: a(vec4(rot.a, trans.x))
, b(vec4(rot.b, trans.y))
, c(vec4(rot.c, trans.z))
{
}
inline mat3x4::mat3x4(const quat &rot, const vec3 &trans)
: mat3x4(mat3x3(rot), trans)
{
}
inline mat3x4::mat3x4(const quat &rot, const vec3 &trans, const vec3 &scale)
: mat3x4(mat3x3(rot, scale), trans)
{
}
inline mat3x4 &mat3x4::operator*=(const mat3x4 &o) {
return (*this = *this * o);
}
inline mat3x4 &mat3x4::operator*=(float k) {
a *= k;
b *= k;
c *= k;
return *this;
}
inline mat3x4 &mat3x4::operator+=(const mat3x4 &o) {
a += o.a;
b += o.b;
c += o.c;
return *this;
}
inline mat3x4 operator*(const mat3x4 &l, const mat3x4 &r) {
return { (r.a*l.a.x + r.b*l.a.y + r.c*l.a.z).addw(l.a.w),
(r.a*l.b.x + r.b*l.b.y + r.c*l.b.z).addw(l.b.w),
(r.a*l.c.x + r.b*l.c.y + r.c*l.c.z).addw(l.c.w) };
}
inline mat3x4 operator+(const mat3x4 &l, const mat3x4 &r) {
return (mat3x4(l) += r);
}
inline mat3x4 operator*(const mat3x4 &l, float k) {
return (mat3x4(l) *= k);
}
}
#endif