-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
179 lines (144 loc) · 5.66 KB
/
Matrix.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
#ifndef _cmatrix_h
#define _cmatrix_h
#include "base.h"
#include "plane.h"
#include "vector3.h"
class CPlane;
// Left Handed, Column-Major matrix class (each column contains a vector)
// No need to return a Matrix on SSE funcs, as long as eax is the dest reg
__declspec(align(16)) class CMatrix
{
public:
float e[4][4];
// Default constructor, should be optimised out in release builds
CMatrix() {};
// This constructor sets all the elements to the specifed value
CMatrix(const float Value);
// This constructor sets all the elements from the explicit values
CMatrix(const float e00, const float e01, const float e02, const float e03,
const float e10, const float e11, const float e12, const float e13,
const float e20, const float e21, const float e22, const float e23,
const float e30, const float e31, const float e32, const float e33);
// Casting operators, used to cast to a D3D specific matrix
operator D3DXMATRIX*() const;
operator const D3DXMATRIX*() const;
// Matrix Multiply *this * Matrix, leaves *this intact
// Returns: new CMatrix containing the resulting matrix
CMatrix operator*(const CMatrix &m) const;
// Multiply *this by Matrix, storing result in *this
// Returns: *this
CMatrix& operator*=(const CMatrix &m);
// Determinant: Calculates the determinant of a matrix
// Returns: float Determinant
static float Determinant(const CMatrix &m);
// Translation: Builds a translation matrix in New matrix from the specified values
// Returns: New matrix
static CMatrix Translation(const float x, const float y, const float z);
// LookAtLH: Builds a Left Handed Look at matrix in New matrix
// Returns: New matrix
static CMatrix LookAtLH(const CVector &Eye, const CVector &At, const CVector &Up);
// RotationAxis: Builds a rotation matrix in New matrix that rotates Angle radians around the axis specified by the Vector Axis
// Returns: New matrix
static CMatrix RotationAxis(const CVector &Axis, const float Angle);
// Identity: Builds an identity matrix in New matrix
// Returns: New matrix
static CMatrix Identity();
// Perspective: Builds a left handed perspective matrix based on a FOV in New matrix
// Returns: New matrix
static CMatrix PerspectiveFovLH(const float FOV, const float Aspect, const float zNear, const float zFar);
// YawPitchRoll: Builds a matrix in New matrix that rotates around all 3 axis by the specified amounts (in radians)
// Returns: New matrix
static CMatrix YawPitchRoll(const float Yaw, const float Pitch, const float Roll);
// Scaling: Builds a scaling matrix in New matrix
// Returns: New matrix
static CMatrix Scaling(const float sx, const float sy, const float sz);
// RotationX: Builds a matrix that rotates around the X Axis in New matrix
// Returns: New matrix
static CMatrix RotationX(const float Angle);
// RotationY: As above, but around the Y Axis
static CMatrix RotationY(const float Angle);
// RotationZ: As above, but around the Z Axis
static CMatrix RotationZ(const float Angle);
// Multiply: Multiplies LH by RH, storing result in New matrix
// Returns: New matrix
static CMatrix FPU_Multiply(const CMatrix &LH, const CMatrix &RH);
static CMatrix SSE_Multiply(const CMatrix &LH, const CMatrix &RH);
static CMatrix (*Multiply)(const CMatrix &LH, const CMatrix &RH);
// Inverse: Makes New matrix = inverse of New matrix
// Returns: New matrix
static CMatrix Inverse(const CMatrix &m);
static CMatrix Inverse(const CMatrix &m, const float det);
// Transpose: Makes New matrix = transpsoe of New matrix
// Returns: New matrix
static CMatrix FPU_Transpose(const CMatrix &m);
static CMatrix SSE_Transpose(const CMatrix &m);
static CMatrix (*Transpose)(const CMatrix &m);
// ZeroMatrix: Zeros out all elements of the matrix
// Returns: New matrix full of zeros
static CMatrix FPU_ZeroMatrix();
static CMatrix SSE_ZeroMatrix();
static CMatrix (*ZeroMatrix)();
// Reflect: Builds a matrix to reflect about the given plane, assumes plane is normalised
// Returns: New reflection matrix
static CMatrix Reflect(const CPlane &p);
// Shadow: Builds a matrix that flattens geometry onto a plane, assumses a normalised plane
static CMatrix Shadow(const CPlane &plane, const CVector &light);
// Transform: Transform nbCoords number of vectors pointed at by *vec with the matrix in New matrix
// Resturns: Nothing
void Transform(CVector *vec, const int nbCoords) const;
// TransformCoords: Transform nbCoords number of vectors pointed at by *vec with the matrix in New matrix
// projecting result back into w = 1
// Returns: Nothing
void TransformCoords(CVector *vec, const nbCoords) const;
// operators new and delete aligned to 16 byte boundaries
// Taken from MS D3DMATRIXA16
void* operator new(size_t s)
{
LPBYTE p = ::new BYTE[s + 16];
if (p)
{
BYTE offset = (BYTE)(16 - ((UINT_PTR)p & 15));
p += offset;
p[-1] = offset;
}
return p;
};
void* operator new[](size_t s)
{
LPBYTE p = ::new BYTE[s + 16];
if (p)
{
BYTE offset = (BYTE)(16 - ((UINT_PTR)p & 15));
p += offset;
p[-1] = offset;
}
return p;
};
void operator delete(void* p)
{
if(p)
{
BYTE* pb = static_cast<BYTE*>(p);
pb -= pb[-1];
::delete [] pb;
}
};
void operator delete[](void* p)
{
if(p)
{
BYTE* pb = static_cast<BYTE*>(p);
pb -= pb[-1];
::delete [] pb;
}
};
};
// Inline the functions if not in debug build
#ifndef _DEBUG
#define MAT_INLINE __forceinline
#include "matrix.inl"
#else
#define MAT_INLINE
#endif
// matrix.h included
#endif