-
Notifications
You must be signed in to change notification settings - Fork 1
/
m4.h
375 lines (342 loc) · 9.74 KB
/
m4.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#pragma once
#include "4dm.h"
namespace fdm
{
namespace m4
{
struct BiVector4
{
float b01;
float b02;
float b03;
float b12;
float b13;
float b23;
BiVector4(float b01 = 0.f, float b02 = 0.f, float b03 = 0.f, float b12 = 0.f, float b13 = 0.f, float b23 = 0.f) : b01(b01), b02(b02), b03(b03), b12(b12), b13(b13), b23(b23) {}
BiVector4(const nlohmann::json& j)
{
reinterpret_cast<void(__thiscall*)(BiVector4*, const nlohmann::json & j)>(
getFuncAddr((int)Func::m4::BiVector4::BiVector4A)
)(this, j);
}
nlohmann::json toJson()
{
nlohmann::json result{};
return reinterpret_cast<nlohmann::json&(__thiscall*)(m4::BiVector4 * self, nlohmann::json * result)>(getFuncAddr((int)Func::m4::BiVector4::toJson))(this, &result);
return result;
}
void normalize()
{
return reinterpret_cast<void(__thiscall*)(BiVector4*)>(
getFuncAddr((int)Func::m4::BiVector4::normalize)
)(this);
}
BiVector4 normalized() const
{
BiVector4 b = *this;
b.normalize();
return b;
}
};
inline glm::vec4 cross(const glm::vec4& u, const glm::vec4& v, const glm::vec4& w)
{
glm::vec4 result{};
return reinterpret_cast<glm::vec4& (__fastcall*)(glm::vec4*, const glm::vec4&, const glm::vec4&, const glm::vec4&)>(
getFuncAddr((int)Func::m4::cross)
)(&result, u, v, w);
return result;
}
inline BiVector4 wedge(const glm::vec4& u, const glm::vec4& v)
{
BiVector4 result{};
result.b01 = u.x * v.y - u.y * v.x;
result.b02 = v.z * u.x - u.z * v.x;
result.b03 = u.x * v.w - u.w * v.x;
result.b12 = v.z * u.y - v.y * u.z;
result.b13 = v.w * u.y - u.w * v.y;
result.b23 = v.w * u.z - u.w * v.z;
return result.normalized();
}
class Rotor
{
public:
float a;
BiVector4 b;
float b0123;
Rotor(const BiVector4& plane, float radians)
{
reinterpret_cast<void(__thiscall*)(Rotor*, const BiVector4&, float)>(
getFuncAddr((int)Func::m4::Rotor::RotorA)
)(this, plane, radians);
}
Rotor(const glm::vec4& from, const glm::vec4& to)
{
reinterpret_cast<void(__thiscall*)(Rotor*, const glm::vec4 & from, const glm::vec4 & to)>(
getFuncAddr((int)Func::m4::Rotor::Rotor)
)(this, from, to);
}
Rotor& operator*=(const Rotor& r)
{
reinterpret_cast<void(__thiscall*)(Rotor*, const Rotor&)>(
getFuncAddr((int)Func::m4::Rotor::operatorMultEq)
)(this, r);
return *this;
}
Rotor operator*(const Rotor& r) const
{
Rotor result = *this;
result *= r;
return result;
}
glm::vec4 rotate(const glm::vec4& v) const
{
glm::vec4 result{};
return reinterpret_cast<glm::vec4&(__thiscall*)(const Rotor*, glm::vec4*, const glm::vec4&)>(
getFuncAddr((int)Func::m4::Rotor::rotate)
)(this, &result, v);
return result;
}
void normalize()
{
return reinterpret_cast<void(__thiscall*)(Rotor*)>(
getFuncAddr((int)Func::m4::Rotor::normalize)
)(this);
}
Rotor normalized() const
{
Rotor r = *this;
r.normalize();
return r;
}
};
class Mat5
{
public:
float value[5][5] = { 0.f };
Mat5(float x = 0.f)
{
reinterpret_cast<void(__thiscall*)(Mat5*, float)>(
getFuncAddr((int)Func::m4::Mat5::Mat5)
)(this, x);
}
Mat5(nlohmann::json& j)
{
reinterpret_cast<void(__thiscall*)(Mat5*, nlohmann::json&)>(
getFuncAddr((int)Func::m4::Mat5::Mat5A)
)(this, j);
}
Mat5(const Rotor& rotor)
{
glm::vec4 x = rotor.rotate({ 1, 0, 0, 0 });
value[0][0] = x[0];
value[0][1] = x[1];
value[0][2] = x[2];
value[0][3] = x[3];
glm::vec4 y = rotor.rotate({ 0, 1, 0, 0 });
value[1][0] = y[0];
value[1][1] = y[1];
value[1][2] = y[2];
value[1][3] = y[3];
glm::vec4 z = rotor.rotate({ 0, 0, 1, 0 });
value[2][0] = z[0];
value[2][1] = z[1];
value[2][2] = z[2];
value[2][3] = z[3];
glm::vec4 w = rotor.rotate({ 0, 0, 0, 1 });
value[3][0] = w[0];
value[3][1] = w[1];
value[3][2] = w[2];
value[3][3] = w[3];
value[4][4] = 1.0;
}
nlohmann::json toJson() const
{
nlohmann::json result{};
return reinterpret_cast<nlohmann::json&(__thiscall*)(const m4::Mat5* self, nlohmann::json* result)>(getFuncAddr((int)Func::m4::Mat5::toJson))(this, &result);
return result;
}
inline static Mat5 identity()
{
return { 1 };
}
Mat5 operator*(const Mat5& other) const
{
Mat5 result;
for (int col = 0; col < 5; ++col)
{
for (int row = 0; row < 5; ++row)
{
for (int k = 0; k < 5; ++k)
{
result.value[col][row] += value[k][row] * other.value[col][k];
}
}
}
return result;
}
Mat5& operator*=(const Mat5& other)
{
Mat5 result = *this * other;
std::memcpy(&value[0][0], &result.value[0][0], sizeof(value));
return *this;
}
glm::vec4 multiply(const glm::vec4& v, float finalComp) const
{
glm::vec4 result{};
return reinterpret_cast<glm::vec4&(__thiscall*)(const Mat5*, glm::vec4*, const glm::vec4&, float)>(
getFuncAddr((int)Func::m4::Mat5::multiply)
)(this, &result, v, finalComp);
return result;
}
glm::vec4 operator*(const glm::vec4& v) const
{
return multiply(v, 1.0f);
}
void translate(const glm::vec4& v)
{
return reinterpret_cast<void(__thiscall*)(Mat5*, const glm::vec4&)>(
getFuncAddr((int)Func::m4::Mat5::translate)
)(this, v);
}
void scale(const glm::vec4& s)
{
return reinterpret_cast<void(__thiscall*)(Mat5*, const glm::vec4&)>(
getFuncAddr((int)Func::m4::Mat5::scale)
)(this, s);
}
float* operator[](int index)
{
return value[index];
}
const float* operator[](int index) const
{
return value[index];
}
// uhh. helpful little function i made
inline static Mat5 inverse(Mat5 m)
{
Mat5 s{ 1 };
Mat5 t{ m };
// Forward elimination
for (int i = 0; i < 5 - 1; ++i) {
int pivot = i;
float pivotsize = t[i][i];
for (int j = i + 1; j < 5; ++j) {
float tmp = t[j][i];
if (fabs(tmp) > fabs(pivotsize)) {
pivotsize = tmp;
pivot = j;
}
}
if (pivotsize == 0.0f) return s;
if (pivot != i) {
for (int j = 0; j < 5; ++j) {
float tmp;
tmp = t[i][j];
t[i][j] = t[pivot][j];
t[pivot][j] = tmp;
tmp = s[i][j];
s[i][j] = s[pivot][j];
s[pivot][j] = tmp;
}
}
for (int j = i + 1; j < 5; ++j) {
float f = t[j][i] / t[i][i];
for (int k = i + 1; k < 5; ++k) {
t[j][k] -= f * t[i][k];
}
for (int k = 0; k < 5; ++k) {
s[j][k] -= f * s[i][k];
}
t[j][i] = 0.0f;
}
}
// Backward substitution
for (int i = 0; i < 5; ++i) {
float f = t[i][i];
if (f == 0.0f) return m4::Mat5{ 1 };
for (int j = 0; j < 5; ++j) {
t[i][j] /= f;
s[i][j] /= f;
}
for (int j = 0; j < i; ++j) {
f = t[j][i];
for (int k = 0; k < 5; ++k) {
t[j][k] -= f * t[i][k];
s[j][k] -= f * s[i][k];
}
}
}
return s;
}
};
inline Mat5 createCamera(const glm::vec4& eye, const glm::vec4& forward, const glm::vec4& up, const glm::vec4& right, const glm::vec4& over)
{
Mat5 result;
return reinterpret_cast<Mat5 & (__fastcall*)(Mat5*, const glm::vec4 & eye, const glm::vec4 & forward, const glm::vec4 & up, const glm::vec4 & right, const glm::vec4 & over)>(
getFuncAddr((int)Func::m4::createCamera)
)(&result, eye, forward, up, right, over);
return result;
/*
Mat5 cameraD{ 1 };
cameraD[0][0] = right.x;
cameraD[1][0] = right.y;
cameraD[2][0] = right.z;
cameraD[3][0] = right.w;
cameraD[0][1] = up.x;
cameraD[1][1] = up.y;
cameraD[2][1] = up.z;
cameraD[3][1] = up.w;
cameraD[0][2] = -forward.x;
cameraD[1][2] = -forward.y;
cameraD[2][2] = -forward.z;
cameraD[3][2] = -forward.w;
cameraD[0][3] = over.x;
cameraD[1][3] = over.y;
cameraD[2][3] = over.z;
cameraD[3][3] = over.w;
Mat5 cameraP{ 1 };
cameraP[4][0] = -eye.x;
cameraP[4][1] = -eye.y;
cameraP[4][2] = -eye.z;
cameraP[4][3] = -eye.w;
return cameraD * cameraP;*/
}
inline glm::vec4 adjustToMaxHorizSpeed(const glm::vec4& vel, const glm::vec4& deltaVel, float maxHorizSpeed)
{
glm::vec4 result;
return reinterpret_cast<glm::vec4 & (__fastcall*)(glm::vec4*, const glm::vec4&, const glm::vec4&, float)>(
getFuncAddr((int)Func::m4::adjustToMaxHorizSpeed)
)(&result, vel, deltaVel, maxHorizSpeed);
}
inline nlohmann::json i64vec3ToJson(const glm::i64vec3& vec)
{
return reinterpret_cast<nlohmann::json(__fastcall*)(const glm::i64vec3 & vec)>(getFuncAddr((int)Func::m4::i64vec3ToJson))(vec);
}
inline nlohmann::json ivec4ToJson(const glm::ivec4& vec)
{
return reinterpret_cast<nlohmann::json(__fastcall*)(const glm::ivec4 & vec)>(getFuncAddr((int)Func::m4::ivec4ToJson))(vec);
}
//inline glm::ivec4 ivec4FromJson(const nlohmann::json& j)
//{
// return reinterpret_cast<glm::ivec4(__fastcall*)(const nlohmann::json & j)>(FUNC_M4_IVEC4FROMJSON)(j);
//}
inline nlohmann::json vec4ToJson(const glm::vec4& vec)
{
return reinterpret_cast<nlohmann::json(__fastcall*)(const glm::vec4 & vec)>(getFuncAddr((int)Func::m4::vec4ToJson))(vec);
}
inline glm::vec4 vec4FromJson(const nlohmann::json& j)
{
return reinterpret_cast<glm::vec4(__fastcall*)(const nlohmann::json & j)>(getFuncAddr((int)Func::m4::vec4FromJson))(j);
}
inline void printMat5(const Mat5& m)
{
printf("%f;%f;%f;%f;%f;\n%f;%f;%f;%f;%f;\n%f;%f;%f;%f;%f;\n%f;%f;%f;%f;%f;\n%f;%f;%f;%f;%f;\n\n",
m[0][0], m[0][1], m[0][2], m[0][3], m[0][4],
m[1][0], m[1][1], m[1][2], m[1][3], m[1][4],
m[2][0], m[2][1], m[2][2], m[2][3], m[2][4],
m[3][0], m[3][1], m[3][2], m[3][3], m[3][4],
m[4][0], m[4][1], m[4][2], m[4][3], m[4][4]);
}
}
}