-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat4x4.cpp
152 lines (132 loc) · 4.81 KB
/
mat4x4.cpp
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
//--------------------------------------------------//
// from https://www.youtube.com/watch?v=ih20l3pJoeU //
//--------------------------------------------------//
#include "mat4x4.h"
#include <qmath.h>
mat4x4::mat4x4() {}
vec3d mat4x4::Matrix_MultiplyVector(mat4x4& m, vec3d& i) {
vec3d v;
v.x = i.x * m.m[0][0] + i.y * m.m[1][0] + i.z * m.m[2][0] + i.w * m.m[3][0];
v.y = i.x * m.m[0][1] + i.y * m.m[1][1] + i.z * m.m[2][1] + i.w * m.m[3][1];
v.z = i.x * m.m[0][2] + i.y * m.m[1][2] + i.z * m.m[2][2] + i.w * m.m[3][2];
v.w = i.x * m.m[0][3] + i.y * m.m[1][3] + i.z * m.m[2][3] + i.w * m.m[3][3];
return v;
}
mat4x4 mat4x4::Matrix_MakeIdentity() {
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
matrix.m[1][1] = 1.0f;
matrix.m[2][2] = 1.0f;
matrix.m[3][3] = 1.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_MakeRotationX(float fAngleRad) {
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
matrix.m[1][1] = cosf(fAngleRad);
matrix.m[1][2] = sinf(fAngleRad);
matrix.m[2][1] = -sinf(fAngleRad);
matrix.m[2][2] = cosf(fAngleRad);
matrix.m[3][3] = 1.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_MakeRotationY(float fAngleRad)
{
mat4x4 matrix;
matrix.m[0][0] = cosf(fAngleRad);
matrix.m[0][2] = sinf(fAngleRad);
matrix.m[2][0] = -sinf(fAngleRad);
matrix.m[1][1] = 1.0f;
matrix.m[2][2] = cosf(fAngleRad);
matrix.m[3][3] = 1.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_MakeRotationZ(float fAngleRad)
{
mat4x4 matrix;
matrix.m[0][0] = cosf(fAngleRad);
matrix.m[0][1] = sinf(fAngleRad);
matrix.m[1][0] = -sinf(fAngleRad);
matrix.m[1][1] = cosf(fAngleRad);
matrix.m[2][2] = 1.0f;
matrix.m[3][3] = 1.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_MakeTranslation(float x, float y, float z)
{
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
matrix.m[1][1] = 1.0f;
matrix.m[2][2] = 1.0f;
matrix.m[3][3] = 1.0f;
matrix.m[3][0] = x;
matrix.m[3][1] = y;
matrix.m[3][2] = z;
return matrix;
}
mat4x4 mat4x4::Matrix_MakeProjection(float fFovDegrees, float fAspectRatio, float fNear, float fFar)
{
float fFovRad = 1.0f / tanf(fFovDegrees * 0.5f / 180.0f * 3.14159f);
mat4x4 matrix;
matrix.m[0][0] = fAspectRatio * fFovRad;
matrix.m[1][1] = fFovRad;
matrix.m[2][2] = fFar / (fFar - fNear);
matrix.m[3][2] = (-fFar * fNear) / (fFar - fNear);
matrix.m[2][3] = 1.0f;
matrix.m[3][3] = 0.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_MultiplyMatrix(mat4x4& m1, mat4x4& m2)
{
mat4x4 matrix;
for (int c = 0; c < 4; c++)
for (int r = 0; r < 4; r++)
matrix.m[r][c] = m1.m[r][0] * m2.m[0][c]
+ m1.m[r][1] * m2.m[1][c]
+ m1.m[r][2] * m2.m[2][c]
+ m1.m[r][3] * m2.m[3][c];
return matrix;
}
mat4x4 mat4x4::Matrix_PointAt(vec3d& pos, vec3d& target, vec3d& up)
{
// Calculate new forward direction
vec3d newForward = target.Vector_Sub(pos);
newForward = newForward.Vector_Normalise();
// Calculate new Up direction
vec3d a = newForward.Vector_Mul(up.Vector_DotProduct(newForward));
vec3d newUp = up.Vector_Sub(a);
newUp = newUp.Vector_Normalise();
// New Right direction is easy, its just cross product
vec3d newRight = newUp.Vector_CrossProduct(newForward);
// Construct Dimensioning and Translation Matrix
mat4x4 matrix;
matrix.m[0][0] = newRight.x;
matrix.m[0][1] = newRight.y;
matrix.m[0][2] = newRight.z;
matrix.m[0][3] = 0.0f;
matrix.m[1][0] = newUp.x;
matrix.m[1][1] = newUp.y;
matrix.m[1][2] = newUp.z;
matrix.m[1][3] = 0.0f;
matrix.m[2][0] = newForward.x;
matrix.m[2][1] = newForward.y;
matrix.m[2][2] = newForward.z;
matrix.m[2][3] = 0.0f;
matrix.m[3][0] = pos.x;
matrix.m[3][1] = pos.y;
matrix.m[3][2] = pos.z;
matrix.m[3][3] = 1.0f;
return matrix;
}
mat4x4 mat4x4::Matrix_QuickInverse(mat4x4& m) // Only for Rotation/Translation Matrices
{
mat4x4 matrix;
matrix.m[0][0] = m.m[0][0]; matrix.m[0][1] = m.m[1][0]; matrix.m[0][2] = m.m[2][0]; matrix.m[0][3] = 0.0f;
matrix.m[1][0] = m.m[0][1]; matrix.m[1][1] = m.m[1][1]; matrix.m[1][2] = m.m[2][1]; matrix.m[1][3] = 0.0f;
matrix.m[2][0] = m.m[0][2]; matrix.m[2][1] = m.m[1][2]; matrix.m[2][2] = m.m[2][2]; matrix.m[2][3] = 0.0f;
matrix.m[3][0] = -(m.m[3][0] * matrix.m[0][0] + m.m[3][1] * matrix.m[1][0] + m.m[3][2] * matrix.m[2][0]);
matrix.m[3][1] = -(m.m[3][0] * matrix.m[0][1] + m.m[3][1] * matrix.m[1][1] + m.m[3][2] * matrix.m[2][1]);
matrix.m[3][2] = -(m.m[3][0] * matrix.m[0][2] + m.m[3][1] * matrix.m[1][2] + m.m[3][2] * matrix.m[2][2]);
matrix.m[3][3] = 1.0f;
return matrix;
}