CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/lin/matrix4x4.h
Views: 1401
1
#ifndef _MATH_LIN_MATRIX4X4_H
2
#define _MATH_LIN_MATRIX4X4_H
3
4
#include "Common/Math/lin/vec3.h"
5
6
namespace Lin {
7
8
class Quaternion;
9
10
class Matrix4x4 {
11
public:
12
union {
13
struct {
14
float xx, xy, xz, xw;
15
float yx, yy, yz, yw;
16
float zx, zy, zz, zw;
17
float wx, wy, wz, ww;
18
};
19
float m[16];
20
};
21
22
const Vec3 right() const {return Vec3(xx, xy, xz);}
23
const Vec3 up() const {return Vec3(yx, yy, yz);}
24
const Vec3 front() const {return Vec3(zx, zy, zz);}
25
const Vec3 move() const {return Vec3(wx, wy, wz);}
26
27
const float &operator[](int i) const {
28
return *(((const float *)this) + i);
29
}
30
float &operator[](int i) {
31
return *(((float *)this) + i);
32
}
33
Matrix4x4 operator * (const Matrix4x4 &other) const ;
34
void operator *= (const Matrix4x4 &other) {
35
*this = *this * other;
36
}
37
const float *getReadPtr() const {
38
return (const float *)this;
39
}
40
void empty() {
41
memset(this, 0, 16 * sizeof(float));
42
}
43
static Matrix4x4 identity() {
44
Matrix4x4 id;
45
id.setIdentity();
46
return id;
47
}
48
void setIdentity() {
49
empty();
50
xx = yy = zz = ww = 1.0f;
51
}
52
void setTranslation(const Vec3 &trans) {
53
setIdentity();
54
wx = trans.x;
55
wy = trans.y;
56
wz = trans.z;
57
}
58
59
Matrix4x4 transpose() const;
60
61
// Exact angles to avoid any artifacts.
62
void setRotationZ90() {
63
empty();
64
float c = 0.0f;
65
float s = 1.0f;
66
xx = c; xy = s;
67
yx = -s; yy = c;
68
zz = 1.0f;
69
ww = 1.0f;
70
}
71
void setRotationZ180() {
72
empty();
73
float c = -1.0f;
74
float s = 0.0f;
75
xx = c; xy = s;
76
yx = -s; yy = c;
77
zz = 1.0f;
78
ww = 1.0f;
79
}
80
void setRotationZ270() {
81
empty();
82
float c = 0.0f;
83
float s = -1.0f;
84
xx = c; xy = s;
85
yx = -s; yy = c;
86
zz = 1.0f;
87
ww = 1.0f;
88
}
89
90
void setOrtho(float left, float right, float bottom, float top, float near, float far);
91
void setOrthoD3D(float left, float right, float bottom, float top, float near, float far);
92
void setOrthoVulkan(float left, float right, float top, float bottom, float near, float far);
93
94
void setViewFrame(const Vec3 &pos, const Vec3 &right, const Vec3 &forward, const Vec3 &up);
95
void toText(char *buffer, int len) const;
96
void print() const;
97
98
void translateAndScale(const Vec3 &trans, const Vec3 &scale) {
99
xx = xx * scale.x + xw * trans.x;
100
xy = xy * scale.y + xw * trans.y;
101
xz = xz * scale.z + xw * trans.z;
102
103
yx = yx * scale.x + yw * trans.x;
104
yy = yy * scale.y + yw * trans.y;
105
yz = yz * scale.z + yw * trans.z;
106
107
zx = zx * scale.x + zw * trans.x;
108
zy = zy * scale.y + zw * trans.y;
109
zz = zz * scale.z + zw * trans.z;
110
111
wx = wx * scale.x + ww * trans.x;
112
wy = wy * scale.y + ww * trans.y;
113
wz = wz * scale.z + ww * trans.z;
114
}
115
};
116
117
} // namespace Lin
118
119
#endif // _MATH_LIN_MATRIX4X4_H
120
121
122