-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGLMTX33.H
executable file
·357 lines (281 loc) · 10.6 KB
/
DGLMTX33.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
#ifndef LIBDGL_DGLMTX33_H
#define LIBDGL_DGLMTX33_H
#include <math.h>
#include "dglcmn.h"
#include "dglfixp.h"
#include "dglvec2.h"
#ifdef __cplusplus
extern "C" {
#endif
#define _M33_11 0
#define _M33_12 3
#define _M33_13 6
#define _M33_21 1
#define _M33_22 4
#define _M33_23 7
#define _M33_31 2
#define _M33_32 5
#define _M33_33 8
typedef struct {
fixed m[9];
} MTX33;
static MTX33 mtx33(
fixed m11, fixed m12, fixed m13,
fixed m21, fixed m22, fixed m23,
fixed m31, fixed m32, fixed m33
);
static void mtx33_set(
MTX33 *m,
fixed m11, fixed m12, fixed m13,
fixed m21, fixed m22, fixed m23,
fixed m31, fixed m32, fixed m33
);
static MTX33 mtx33_from_angles(float x, float y, float z);
static MTX33 mtx33_rotation_x(float radians);
static MTX33 mtx33_rotation_y(float radians);
static MTX33 mtx33_rotation_z(float radians);
static MTX33 mtx33_add(MTX33 a, MTX33 b);
static MTX33 mtx33_sub(MTX33 a, MTX33 b);
static MTX33 mtx33_mul(MTX33 a, MTX33 b);
static MTX33 mtx33_scale(MTX33 m, fixed scale);
static fixed mtx33_determinant(MTX33 m);
static MTX33 mtx33_inverse(MTX33 m);
static MTX33 mtx33_transpose(MTX33 m);
static VEC2 mtx33_transform(MTX33 m, VEC2 v);
static MTX33 mtx33_translation_2d(fixed x, fixed y);
static MTX33 mtx33_scaling_2d(fixed x, fixed y);
static MTX33 mtx33_rotation_2d(float radians);
static VEC2 mtx33_transform_2d(MTX33 m, VEC2 v);
#define IDENTITY_MTX33 mtx33(1.0f, 0.0f, 0.0f, \
0.0f, 1.0f, 0.0f, \
0.0f, 0.0f, 1.0f)
// --------------------------------------------------------------------------
static MTX33 mtx33(
fixed m11, fixed m12, fixed m13,
fixed m21, fixed m22, fixed m23,
fixed m31, fixed m32, fixed m33
) {
MTX33 result;
result.m[_M33_11] = m11;
result.m[_M33_12] = m12;
result.m[_M33_13] = m13;
result.m[_M33_21] = m21;
result.m[_M33_22] = m22;
result.m[_M33_23] = m23;
result.m[_M33_31] = m31;
result.m[_M33_32] = m32;
result.m[_M33_33] = m33;
return result;
}
static void mtx33_set(
MTX33 *m,
fixed m11, fixed m12, fixed m13,
fixed m21, fixed m22, fixed m23,
fixed m31, fixed m32, fixed m33
) {
m->m[_M33_11] = m11;
m->m[_M33_12] = m12;
m->m[_M33_13] = m13;
m->m[_M33_21] = m21;
m->m[_M33_22] = m22;
m->m[_M33_23] = m23;
m->m[_M33_31] = m31;
m->m[_M33_32] = m32;
m->m[_M33_33] = m33;
}
static MTX33 mtx33_from_angles(float x, float y, float z) {
MTX33 rx, ry, rz;
rx = mtx33_rotation_x(x);
ry = mtx33_rotation_y(y);
rz = mtx33_rotation_z(z);
return mtx33_mul(mtx33_mul(rz, ry), rx);
}
static MTX33 mtx33_rotation_x(fixed radians) {
MTX33 result;
fixed s, c;
s = FTOFIX(sin(radians));
c = FTOFIX(cos(radians));
result.m[_M33_11] = FP_1;
result.m[_M33_12] = 0;
result.m[_M33_13] = 0;
result.m[_M33_21] = 0;
result.m[_M33_22] = c;
result.m[_M33_23] = -s;
result.m[_M33_31] = 0;
result.m[_M33_32] = s;
result.m[_M33_33] = c;
return result;
}
static MTX33 mtx33_rotation_y(float radians) {
MTX33 result;
fixed s, c;
s = FTOFIX(sin(radians));
c = FTOFIX(cos(radians));
result.m[_M33_11] = c;
result.m[_M33_12] = 0;
result.m[_M33_13] = s;
result.m[_M33_21] = 0;
result.m[_M33_22] = FP_1;
result.m[_M33_23] = 0;
result.m[_M33_31] = -s;
result.m[_M33_32] = 0;
result.m[_M33_33] = c;
return result;
}
static MTX33 mtx33_rotation_z(float radians) {
MTX33 result;
fixed s, c;
s = FTOFIX(sin(radians));
c = FTOFIX(cos(radians));
result.m[_M33_11] = c;
result.m[_M33_12] = -s;
result.m[_M33_13] = 0;
result.m[_M33_21] = s;
result.m[_M33_22] = c;
result.m[_M33_23] = 0;
result.m[_M33_31] = 0;
result.m[_M33_32] = 0;
result.m[_M33_33] = FP_1;
return result;
}
static MTX33 mtx33_add(MTX33 a, MTX33 b) {
MTX33 result;
result.m[_M33_11] = a.m[_M33_11] + b.m[_M33_11];
result.m[_M33_12] = a.m[_M33_12] + b.m[_M33_12];
result.m[_M33_13] = a.m[_M33_13] + b.m[_M33_13];
result.m[_M33_21] = a.m[_M33_21] + b.m[_M33_21];
result.m[_M33_22] = a.m[_M33_22] + b.m[_M33_22];
result.m[_M33_23] = a.m[_M33_23] + b.m[_M33_23];
result.m[_M33_31] = a.m[_M33_31] + b.m[_M33_31];
result.m[_M33_32] = a.m[_M33_32] + b.m[_M33_32];
result.m[_M33_33] = a.m[_M33_33] + b.m[_M33_33];
return result;
}
static MTX33 mtx33_sub(MTX33 a, MTX33 b) {
MTX33 result;
result.m[_M33_11] = a.m[_M33_11] - b.m[_M33_11];
result.m[_M33_12] = a.m[_M33_12] - b.m[_M33_12];
result.m[_M33_13] = a.m[_M33_13] - b.m[_M33_13];
result.m[_M33_21] = a.m[_M33_21] - b.m[_M33_21];
result.m[_M33_22] = a.m[_M33_22] - b.m[_M33_22];
result.m[_M33_23] = a.m[_M33_23] - b.m[_M33_23];
result.m[_M33_31] = a.m[_M33_31] - b.m[_M33_31];
result.m[_M33_32] = a.m[_M33_32] - b.m[_M33_32];
result.m[_M33_33] = a.m[_M33_33] - b.m[_M33_33];
return result;
}
static MTX33 mtx33_mul(MTX33 a, MTX33 b) {
MTX33 result;
result.m[_M33_11] = fix_mul(a.m[_M33_11], b.m[_M33_11]) + fix_mul(a.m[_M33_12], b.m[_M33_21]) + fix_mul(a.m[_M33_13], b.m[_M33_31]);
result.m[_M33_12] = fix_mul(a.m[_M33_11], b.m[_M33_12]) + fix_mul(a.m[_M33_12], b.m[_M33_22]) + fix_mul(a.m[_M33_13], b.m[_M33_32]);
result.m[_M33_13] = fix_mul(a.m[_M33_11], b.m[_M33_13]) + fix_mul(a.m[_M33_12], b.m[_M33_23]) + fix_mul(a.m[_M33_13], b.m[_M33_33]);
result.m[_M33_21] = fix_mul(a.m[_M33_21], b.m[_M33_11]) + fix_mul(a.m[_M33_22], b.m[_M33_21]) + fix_mul(a.m[_M33_23], b.m[_M33_31]);
result.m[_M33_22] = fix_mul(a.m[_M33_21], b.m[_M33_12]) + fix_mul(a.m[_M33_22], b.m[_M33_22]) + fix_mul(a.m[_M33_23], b.m[_M33_32]);
result.m[_M33_23] = fix_mul(a.m[_M33_21], b.m[_M33_13]) + fix_mul(a.m[_M33_22], b.m[_M33_23]) + fix_mul(a.m[_M33_23], b.m[_M33_33]);
result.m[_M33_31] = fix_mul(a.m[_M33_31], b.m[_M33_11]) + fix_mul(a.m[_M33_32], b.m[_M33_21]) + fix_mul(a.m[_M33_33], b.m[_M33_31]);
result.m[_M33_32] = fix_mul(a.m[_M33_31], b.m[_M33_12]) + fix_mul(a.m[_M33_32], b.m[_M33_22]) + fix_mul(a.m[_M33_33], b.m[_M33_32]);
result.m[_M33_33] = fix_mul(a.m[_M33_31], b.m[_M33_13]) + fix_mul(a.m[_M33_32], b.m[_M33_23]) + fix_mul(a.m[_M33_33], b.m[_M33_33]);
return result;
}
static MTX33 mtx33_scale(MTX33 m, fixed scale) {
MTX33 result;
result.m[_M33_11] = fix_mul(m.m[_M33_11], scale);
result.m[_M33_12] = fix_mul(m.m[_M33_12], scale);
result.m[_M33_13] = fix_mul(m.m[_M33_13], scale);
result.m[_M33_21] = fix_mul(m.m[_M33_21], scale);
result.m[_M33_22] = fix_mul(m.m[_M33_22], scale);
result.m[_M33_23] = fix_mul(m.m[_M33_23], scale);
result.m[_M33_31] = fix_mul(m.m[_M33_31], scale);
result.m[_M33_32] = fix_mul(m.m[_M33_32], scale);
result.m[_M33_33] = fix_mul(m.m[_M33_33], scale);
return result;
}
static fixed mtx33_determinant(MTX33 m) {
return
fix_mul(m.m[_M33_11], fix_mul(m.m[_M33_22], m.m[_M33_33])) +
fix_mul(m.m[_M33_12], fix_mul(m.m[_M33_23], m.m[_M33_31])) +
fix_mul(m.m[_M33_13], fix_mul(m.m[_M33_21], m.m[_M33_32])) -
fix_mul(m.m[_M33_11], fix_mul(m.m[_M33_23], m.m[_M33_32])) -
fix_mul(m.m[_M33_12], fix_mul(m.m[_M33_21], m.m[_M33_33])) -
fix_mul(m.m[_M33_13], fix_mul(m.m[_M33_22], m.m[_M33_31]));
}
static MTX33 mtx33_inverse(MTX33 m) {
fixed d;
MTX33 result;
d = mtx33_determinant(m);
if (d == 0)
return IDENTITY_MTX33;
else {
d = fix_div(FP_1, d);
result.m[_M33_11] = fix_mul(d, (fix_mul(m.m[_M33_22], m.m[_M33_33]) - fix_mul(m.m[_M33_32], m.m[_M33_23])));
result.m[_M33_21] = fix_mul(d, (fix_mul(m.m[_M33_31], m.m[_M33_23]) - fix_mul(m.m[_M33_21], m.m[_M33_33])));
result.m[_M33_31] = fix_mul(d, (fix_mul(m.m[_M33_21], m.m[_M33_32]) - fix_mul(m.m[_M33_31], m.m[_M33_22])));
result.m[_M33_21] = fix_mul(d, (fix_mul(m.m[_M33_32], m.m[_M33_13]) - fix_mul(m.m[_M33_12], m.m[_M33_33])));
result.m[_M33_22] = fix_mul(d, (fix_mul(m.m[_M33_11], m.m[_M33_33]) - fix_mul(m.m[_M33_31], m.m[_M33_13])));
result.m[_M33_23] = fix_mul(d, (fix_mul(m.m[_M33_31], m.m[_M33_12]) - fix_mul(m.m[_M33_11], m.m[_M33_32])));
result.m[_M33_31] = fix_mul(d, (fix_mul(m.m[_M33_12], m.m[_M33_23]) - fix_mul(m.m[_M33_22], m.m[_M33_13])));
result.m[_M33_32] = fix_mul(d, (fix_mul(m.m[_M33_21], m.m[_M33_13]) - fix_mul(m.m[_M33_11], m.m[_M33_23])));
result.m[_M33_33] = fix_mul(d, (fix_mul(m.m[_M33_11], m.m[_M33_22]) - fix_mul(m.m[_M33_21], m.m[_M33_12])));
return result;
}
}
static MTX33 mtx33_transpose(MTX33 m) {
MTX33 result;
result.m[_M33_11] = m.m[_M33_11];
result.m[_M33_12] = m.m[_M33_21];
result.m[_M33_13] = m.m[_M33_31];
result.m[_M33_21] = m.m[_M33_12];
result.m[_M33_22] = m.m[_M33_22];
result.m[_M33_23] = m.m[_M33_32];
result.m[_M33_31] = m.m[_M33_13];
result.m[_M33_32] = m.m[_M33_23];
result.m[_M33_33] = m.m[_M33_33];
return result;
}
static VEC2 mtx33_transform(MTX33 m, VEC2 v) {
VEC2 result;
result.x = fix_mul(v.x, m.m[_M33_11]) + fix_mul(v.y, m.m[_M33_12]) + m.m[_M33_13];
result.y = fix_mul(v.x, m.m[_M33_21]) + fix_mul(v.y, m.m[_M33_22]) + m.m[_M33_23];
return result;
}
static MTX33 mtx33_translation_2d(fixed x, fixed y) {
MTX33 result;
result.m[_M33_11] = FP_1;
result.m[_M33_12] = 0;
result.m[_M33_13] = 0;
result.m[_M33_21] = 0;
result.m[_M33_22] = FP_1;
result.m[_M33_23] = 0;
result.m[_M33_31] = x;
result.m[_M33_32] = y;
result.m[_M33_33] = FP_1;
return result;
}
static MTX33 mtx33_scaling_2d(fixed x, fixed y) {
MTX33 result;
result.m[_M33_11] = x;
result.m[_M33_12] = 0;
result.m[_M33_13] = 0;
result.m[_M33_21] = 0;
result.m[_M33_22] = y;
result.m[_M33_23] = 0;
result.m[_M33_31] = 0;
result.m[_M33_32] = 0;
result.m[_M33_33] = FP_1;
return result;
}
static MTX33 mtx33_rotation_2d(float radians) {
return mtx33_rotation_z(radians);
}
static VEC2 mtx33_transform_2d(MTX33 m, VEC2 v) {
VEC2 result;
result.x = fix_mul(v.x, m.m[_M33_11]) + fix_mul(v.y, m.m[_M33_12]) + m.m[_M33_13];
result.y = fix_mul(v.x, m.m[_M33_21]) + fix_mul(v.y, m.m[_M33_22]) + m.m[_M33_23];
result.x += m.m[_M33_31];
result.y += m.m[_M33_32];
return result;
}
#ifdef __cplusplus
}
#endif
#endif