forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_mat.h
174 lines (139 loc) · 3.73 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
#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 {
vec4 a, b, c, d;
void loadIdentity();
mat4 inverse();
mat4 operator*(const mat4 &t) const;
void setScaleTrans(float scaleX, float scaleY, float scaleZ);
void setRotateTrans(float rotateX, float rotateY, float rotateZ);
void setTranslateTrans(float x, float y, float z);
void setCameraTrans(const vec3 &target, const vec3 &up);
void setCameraTrans(const vec3 &position, const quat &q);
void setPerspectiveTrans(const perspective &p);
void getOrient(vec3 *direction, vec3 *up, vec3 *side) const;
float *ptr();
const float *ptr() const;
private:
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);
};
inline float *mat4::ptr() {
return &a.x;
}
inline const float *mat4::ptr() const {
return &a.x;
}
///! 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