-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix4x4.h
217 lines (180 loc) · 7.01 KB
/
Matrix4x4.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once
#include <ostream>
#include <immintrin.h>
#define _MM_BLEND(x, y, z, w) ((x) | ((y) << 1) | ((z) << 2) | ((w) << 3))
class Vector3;
class Vector3Aligned;
/*
*
* rotation z axis matrix:
* ---- ----
* | cos(z), sin(z), 0, 0 |
* | -sin(z), cos(z), 0, 0 |
* | 0, 0, 1, 0 |
* | 0, 0, 0, 1 |
* ---- ----
*
* rotation y axis matrix:
* ---- ----
* | cos(y), 0, -sin(y), 0 |
* | 0, 1, 0, 0 |
* | sin(y), 0, cos(y), 0 |
* | 0, 0, 0, 1 |
* ---- ----
*
* rotation x axis matrix:
* ---- ----
* | 1, 0, 0, 0 |
* | 0, cos(x), sin(x), 0 |
* | 0, -sin(x), cos(x), 0 |
* | 0, 0, 0, 1 |
* ---- ----
*
* affine transform axis matrix: = mRX * mRY * mRZ + mTranslation
* ---- ----
* | cos(y) * cos(z), sin(z), -sin(y), 0 | Right
* | -sin(z), cos(z) * cos(x), sin(x), 0 | Up
* | sin(y), -sin(x), cos(x) * cos(y), 0 | Forward
* | tx, ty, tz, 1 | Pos
* ---- ----
*
* reference = http://mathworld.wolfram.com/RotationMatrix.html
*/
class Matrix4x4
{
public:
union
{
struct
{
float _00, _01, _02, _03;
float _10, _11, _12, _13;
float _20, _21, _22, _23;
float _30, _31, _32, _33;
};
float m[4][4];
float mm[16];
struct
{
__m128 m0, m1, m2, m3;
};
};
Matrix4x4();
Matrix4x4(const Matrix4x4& mOther);
Matrix4x4(const __m128& m0, const __m128& m1, const __m128& m2, const __m128& m3);
Matrix4x4(float _00, float _01, float _02, float _03,
float _10, float _11, float _12, float _13,
float _20, float _21, float _22, float _23,
float _30, float _31, float _32, float _33);
void InitPerspectiveLH(float flAspect, float flFov, float zNear, float zFar);
void InitPerspectiveRH(float flAspect, float flFov, float zNear, float zFar);
void InitAxisAngle(const Vector3& vAxis, float theta);
void InitTransform(const Vector3& vAngles, const Vector3& vPosition);
void InitTransform(const Vector3& vAngles, const Vector3& vScale, const Vector3& vPosition);
void InitTransform(const Vector3& vForward, const Vector3& vRight, const Vector3& vUp, const Vector3& vPosition);
void InitTransform(const Vector3& vForward, const Vector3& vRight, const Vector3& vUp, const Vector3& vScale, const Vector3& vPosition);
static Matrix4x4 PointTo(const Vector3Aligned& vPosition, Vector3Aligned& vEye, const Vector3Aligned& vUp) noexcept;
inline Matrix4x4& operator*(const Matrix4x4& matrix);
Matrix4x4 Matrix4x4::operator+ (const Matrix4x4& m) const
{
__m128 R0 = _mm_add_ps(m0, m.m0);
__m128 R1 = _mm_add_ps(m1, m.m1);
__m128 R2 = _mm_add_ps(m2, m.m2);
__m128 R3 = _mm_add_ps(m3, m.m3);
return Matrix4x4(R0, R1, R2, R3);
}
inline float* operator[](int i);
inline const float* operator[](int i) const;
inline float& operator()(int row, int col);
inline float operator()(int row, int col) const;
inline float ThetaOfAxisAngle() const;
inline void Transpose();
inline void Rotate(const Vector3& v, float theta, Vector3& vRotate);
inline void Transform(const Vector3& v, Vector3& vTransform) const;
inline void Scale(float flScale);
inline void Scale(const Vector3& vScale);
inline Vector3& GetAxis(int i);
inline friend std::ostream& operator<<(std::ostream& os, const Matrix4x4& m);
};
typedef Matrix4x4 VMatrix;
#include "Vector3.h"
inline Matrix4x4& Matrix4x4::operator*(const Matrix4x4& matrix)
{
m[0][0] = m[0][0] * matrix.m[0][0] + m[1][0] * matrix.m[0][1] + m[2][0] * matrix.m[0][2] + m[3][0] * matrix.m[0][3];
m[0][1] = m[0][1] * matrix.m[0][0] + m[1][1] * matrix.m[0][1] + m[2][1] * matrix.m[0][2] + m[3][1] * matrix.m[0][3];
m[0][2] = m[0][2] * matrix.m[0][0] + m[1][2] * matrix.m[0][1] + m[2][2] * matrix.m[0][2] + m[3][2] * matrix.m[0][3];
m[0][3] = m[0][3] * matrix.m[0][0] + m[1][3] * matrix.m[0][1] + m[2][3] * matrix.m[0][2] + m[3][3] * matrix.m[0][3];
m[1][0] = m[0][0] * matrix.m[1][0] + m[1][0] * matrix.m[1][1] + m[2][0] * matrix.m[1][2] + m[3][0] * matrix.m[1][3];
m[1][1] = m[0][1] * matrix.m[1][0] + m[1][1] * matrix.m[1][1] + m[2][1] * matrix.m[1][2] + m[3][1] * matrix.m[1][3];
m[1][2] = m[0][2] * matrix.m[1][0] + m[1][2] * matrix.m[1][1] + m[2][2] * matrix.m[1][2] + m[3][2] * matrix.m[1][3];
m[1][3] = m[0][3] * matrix.m[1][0] + m[1][3] * matrix.m[1][1] + m[2][3] * matrix.m[1][2] + m[3][3] * matrix.m[1][3];
m[2][0] = m[0][0] * matrix.m[2][0] + m[1][0] * matrix.m[2][1] + m[2][0] * matrix.m[2][2] + m[3][0] * matrix.m[2][3];
m[2][1] = m[0][1] * matrix.m[2][0] + m[1][1] * matrix.m[2][1] + m[2][1] * matrix.m[2][2] + m[3][1] * matrix.m[2][3];
m[2][2] = m[0][2] * matrix.m[2][0] + m[1][2] * matrix.m[2][1] + m[2][2] * matrix.m[2][2] + m[3][2] * matrix.m[2][3];
m[2][3] = m[0][3] * matrix.m[2][0] + m[1][3] * matrix.m[2][1] + m[2][3] * matrix.m[2][2] + m[3][3] * matrix.m[2][3];
m[3][0] = m[0][0] * matrix.m[3][0] + m[1][0] * matrix.m[3][1] + m[2][0] * matrix.m[3][2] + m[3][0] * matrix.m[3][3];
m[3][1] = m[0][1] * matrix.m[3][0] + m[1][1] * matrix.m[3][1] + m[2][1] * matrix.m[3][2] + m[3][1] * matrix.m[3][3];
m[3][2] = m[0][2] * matrix.m[3][0] + m[1][2] * matrix.m[3][1] + m[2][2] * matrix.m[3][2] + m[3][2] * matrix.m[3][3];
m[3][3] = m[0][3] * matrix.m[3][0] + m[1][3] * matrix.m[3][1] + m[2][3] * matrix.m[3][2] + m[3][3] * matrix.m[3][3];
return *this;
}
inline float* Matrix4x4::operator[](int i)
{
return m[i];
}
inline const float* Matrix4x4::operator[](int i) const
{
return m[i];
}
inline float& Matrix4x4::operator()(int row, int col)
{
return m[row][col];
}
inline float Matrix4x4::operator()(int row, int col) const
{
return m[row][col];
}
inline void Matrix4x4::Transpose()
{
__m128 _Tmp3, _Tmp2, _Tmp1, _Tmp0;
_Tmp0 = _mm_shuffle_ps(m0, m1, 0x44);
_Tmp2 = _mm_shuffle_ps(m0, m1, 0xEE);
_Tmp1 = _mm_shuffle_ps(m2, m3, 0x44);
_Tmp3 = _mm_shuffle_ps(m2, m3, 0xEE);
m0 = _mm_shuffle_ps(_Tmp0, _Tmp1, 0x88);
m1 = _mm_shuffle_ps(_Tmp0, _Tmp1, 0xDD);
m2 = _mm_shuffle_ps(_Tmp2, _Tmp3, 0x88);
m3 = _mm_shuffle_ps(_Tmp2, _Tmp3, 0xDD);
}
inline void Matrix4x4::Rotate(const Vector3& v, float theta, Vector3& vRotate)
{
/*float sin, cos;
Math::SinCos(theta, &sin, &cos);
vRotate = v * cos + (sin * (e x v)) + (1.f - cos) * (e dot v) * e;*/
vRotate.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z;
vRotate.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z;
vRotate.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z;
}
inline void Matrix4x4::Transform(const Vector3& v, Vector3& vTransform) const
{
vTransform.x = m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z + m[3][0];
vTransform.y = m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z + m[3][1];
vTransform.z = m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z + m[3][2];
}
inline void Matrix4x4::Scale(float flScale)
{
m[0][0] *= flScale;
m[1][1] *= flScale;
m[2][2] *= flScale;
}
inline void Matrix4x4::Scale(const Vector3& vScale)
{
m[0][0] *= vScale.x;
m[1][1] *= vScale.y;
m[2][2] *= vScale.z;
}
inline Vector3& Matrix4x4::GetAxis(int i)
{
return *(Vector3*)((*this)[i]);
}
#include "Math.h"