-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.hpp
227 lines (181 loc) · 5.61 KB
/
Matrix.hpp
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
218
219
220
221
222
223
224
225
226
227
#pragma once
#ifndef __MATHS_MATRIX_HPP__
#define __MATHS_MATRIX_HPP__
#include "Vector3.hpp"
#include "Vector4.hpp"
#include "Quaternion.hpp"
namespace Math
{
class Matrix : public DirectX::XMMATRIX
{
public:
//--------------------------------------------------------------------------
// Constructors
//
Matrix() : DirectX::XMMATRIX()
{
}
Matrix(DirectX::XMVECTOR _1, DirectX::XMVECTOR _2, DirectX::XMVECTOR _3, DirectX::XMVECTOR _4) : DirectX::XMMATRIX(_1, _2, _3, _4)
{
}
Matrix(float _11, float _12, float _13, float _14,
float _21, float _22, float _23, float _24,
float _31, float _32, float _33, float _34,
float _41, float _42, float _43, float _44) : DirectX::XMMATRIX(_11, _12, _13, _14, _21, _22, _23, _24, _31, _32, _33, _34, _41, _42, _43, _44)
{
}
explicit Matrix(const float* v) : DirectX::XMMATRIX(v)
{
}
explicit Matrix(DirectX::CXMMATRIX m) : DirectX::XMMATRIX(m)
{
}
Matrix(const Matrix& matrix) : DirectX::XMMATRIX(matrix)
{
}
//--------------------------------------------------------------------------
// Assignment
//
Matrix& operator = (DirectX::CXMMATRIX m)
{
if (&m != this)
{
*static_cast<DirectX::XMMATRIX*>(this) = m;
}
return *this;
}
Matrix& operator = (const Matrix& m)
{
if (&m != this)
{
*static_cast<DirectX::XMMATRIX*>(this) = m;
}
return *this;
}
//--------------------------------------------------------------------------
// Accessors
//
float operator () (uint_t row, uint_t column) const;
Vector4 row(uint_t i) const;
Vector4 column(uint_t i) const;
//--------------------------------------------------------------------------
// Arithmetic Operators
//
Matrix operator * (const Matrix& m) const
{
return static_cast<Matrix>(DirectX::XMMatrixMultiply(*this, m));
}
Matrix operator *= (const Matrix& m)
{
*this = (DirectX::XMMatrixMultiply(*this, m));
return *this;
}
//--------------------------------------------------------------------------
// Computation
//
float determinant() const
{
return DirectX::XMVectorGetX(DirectX::XMMatrixDeterminant(*this));
}
bool decompose(Quaternion& rotation, Vector3& translation, Vector3& scale);
void invert(float* determinant = nullptr)
{
DirectX::XMVECTOR det;
*this = DirectX::XMMatrixInverse(&det, *this);
if (determinant)
{
*determinant = DirectX::XMVectorGetX(det);
}
}
Matrix inverse(float* determinant = nullptr) const
{
DirectX::XMVECTOR det;
DirectX::XMMATRIX mat = DirectX::XMMatrixInverse(&det, *this);
if (determinant)
{
*determinant = DirectX::XMVectorGetX(det);
}
return static_cast<Matrix>(mat);
}
void transpose()
{
*this = DirectX::XMMatrixTranspose(*this);
}
Matrix transpose() const
{
return static_cast<Matrix>(DirectX::XMMatrixTranspose(*this));
}
Vector3 transform(const Vector3& v) const
{
return Vector3(DirectX::XMVector3Transform(v, *this));
}
Vector4 transform(const Vector4& v) const
{
return Vector4(DirectX::XMVector4Transform(v, *this));
}
bool is_identity() const
{
return DirectX::XMMatrixIsIdentity(*this);
}
//--------------------------------------------------------------------------
// Constants
//
static const Matrix IDENTITY;
//--------------------------------------------------------------------------
// Auxilliary Functions
//
static Matrix rotation_axis(const Vector3& axis, float angle)
{
return static_cast<Matrix>(DirectX::XMMatrixRotationAxis(axis, angle));
}
static Matrix rotation_roll_pitch_yaw(float pitch, float yaw, float roll)
{
return static_cast<Matrix>(DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll));
}
static Matrix rotation_quaternion(const Quaternion& q)
{
return static_cast<Matrix>(DirectX::XMMatrixRotationQuaternion(q));
}
static Matrix scaling(float x, float y, float z)
{
return static_cast<Matrix>(DirectX::XMMatrixScaling(x, y, z));
}
static Matrix scaling(const Vector3& v)
{
return static_cast<Matrix>(DirectX::XMMatrixScalingFromVector(v));
}
static Matrix translation(float x, float y, float z)
{
return static_cast<Matrix>(DirectX::XMMatrixTranslation(x, y, z));
}
static Matrix translation(const Vector3& v)
{
return static_cast<Matrix>(DirectX::XMMatrixTranslationFromVector(v));
}
static Matrix affine_transformation(const Vector3& translation, const Quaternion& rotation)
{
return static_cast<Matrix>(DirectX::XMMatrixAffineTransformation(DirectX::XMVectorSplatOne(), DirectX::XMVectorZero(), rotation, translation));
}
static Matrix affine_transformation(const Vector3& translation, const Quaternion& rotation, const Vector3& scale, const Vector3& rotation_centre = Vector3::ZERO)
{
return static_cast<Matrix>(DirectX::XMMatrixAffineTransformation(scale, rotation_centre, rotation, translation));
}
static Matrix look_at(const Vector3& position, const Vector3& target, const Vector3& up)
{
return static_cast<Matrix>(XMMatrixLookAtLH(position, target, up));
}
static Matrix orthographic(float width, float height, float nearZ, float farZ)
{
return static_cast<Matrix>(DirectX::XMMatrixOrthographicLH(width, height, nearZ, farZ));
}
static Matrix perspective(float width, float height, float nearZ, float farZ)
{
return static_cast<Matrix>(DirectX::XMMatrixPerspectiveLH(width, height, nearZ, farZ));
}
static Matrix perspective_fov(float fov, float aspect, float nearZ, float farZ)
{
return static_cast<Matrix>(DirectX::XMMatrixPerspectiveFovLH(fov, aspect, nearZ, farZ));
}
};
} // namespace Math
#endif // __MATHS_MATRIX_HPP__