-
Notifications
You must be signed in to change notification settings - Fork 13
/
mat4.c
723 lines (628 loc) · 20.3 KB
/
mat4.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#include <stdlib.h>
#include <math.h>
#include "gl-matrix.h"
mat4_t mat4_create(mat4_t mat) {
mat4_t dest = calloc(sizeof(double), 16);
if (mat) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
return dest;
}
mat4_t mat4_set(mat4_t mat, mat4_t dest) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
return dest;
}
mat4_t mat4_identity(mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
dest[0] = 1;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
dest[4] = 0;
dest[5] = 1;
dest[6] = 0;
dest[7] = 0;
dest[8] = 0;
dest[9] = 0;
dest[10] = 1;
dest[11] = 0;
dest[12] = 0;
dest[13] = 0;
dest[14] = 0;
dest[15] = 1;
return dest;
}
mat4_t mat4_transpose(mat4_t mat, mat4_t dest) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (!dest || mat == dest) {
double a01 = mat[1], a02 = mat[2], a03 = mat[3],
a12 = mat[6], a13 = mat[7],
a23 = mat[11];
mat[1] = mat[4];
mat[2] = mat[8];
mat[3] = mat[12];
mat[4] = a01;
mat[6] = mat[9];
mat[7] = mat[13];
mat[8] = a02;
mat[9] = a12;
mat[11] = mat[14];
mat[12] = a03;
mat[13] = a13;
mat[14] = a23;
return mat;
}
dest[0] = mat[0];
dest[1] = mat[4];
dest[2] = mat[8];
dest[3] = mat[12];
dest[4] = mat[1];
dest[5] = mat[5];
dest[6] = mat[9];
dest[7] = mat[13];
dest[8] = mat[2];
dest[9] = mat[6];
dest[10] = mat[10];
dest[11] = mat[14];
dest[12] = mat[3];
dest[13] = mat[7];
dest[14] = mat[11];
dest[15] = mat[15];
return dest;
}
double mat4_determinant(mat4_t mat) {
// Cache the matrix values (makes for huge speed increases!)
double a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
return (a30 * a21 * a12 * a03 - a20 * a31 * a12 * a03 - a30 * a11 * a22 * a03 + a10 * a31 * a22 * a03 +
a20 * a11 * a32 * a03 - a10 * a21 * a32 * a03 - a30 * a21 * a02 * a13 + a20 * a31 * a02 * a13 +
a30 * a01 * a22 * a13 - a00 * a31 * a22 * a13 - a20 * a01 * a32 * a13 + a00 * a21 * a32 * a13 +
a30 * a11 * a02 * a23 - a10 * a31 * a02 * a23 - a30 * a01 * a12 * a23 + a00 * a31 * a12 * a23 +
a10 * a01 * a32 * a23 - a00 * a11 * a32 * a23 - a20 * a11 * a02 * a33 + a10 * a21 * a02 * a33 +
a20 * a01 * a12 * a33 - a00 * a21 * a12 * a33 - a10 * a01 * a22 * a33 + a00 * a11 * a22 * a33);
}
mat4_t mat4_inverse(mat4_t mat, mat4_t dest) {
if (!dest) { dest = mat; }
// Cache the matrix values (makes for huge speed increases!)
double a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
b00 = a00 * a11 - a01 * a10,
b01 = a00 * a12 - a02 * a10,
b02 = a00 * a13 - a03 * a10,
b03 = a01 * a12 - a02 * a11,
b04 = a01 * a13 - a03 * a11,
b05 = a02 * a13 - a03 * a12,
b06 = a20 * a31 - a21 * a30,
b07 = a20 * a32 - a22 * a30,
b08 = a20 * a33 - a23 * a30,
b09 = a21 * a32 - a22 * a31,
b10 = a21 * a33 - a23 * a31,
b11 = a22 * a33 - a23 * a32,
d = (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06),
invDet;
// Calculate the determinant
if (!d) { return NULL; }
invDet = 1 / d;
dest[0] = (a11 * b11 - a12 * b10 + a13 * b09) * invDet;
dest[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
dest[2] = (a31 * b05 - a32 * b04 + a33 * b03) * invDet;
dest[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
dest[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
dest[5] = (a00 * b11 - a02 * b08 + a03 * b07) * invDet;
dest[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
dest[7] = (a20 * b05 - a22 * b02 + a23 * b01) * invDet;
dest[8] = (a10 * b10 - a11 * b08 + a13 * b06) * invDet;
dest[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
dest[10] = (a30 * b04 - a31 * b02 + a33 * b00) * invDet;
dest[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
dest[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
dest[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet;
dest[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
dest[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet;
return dest;
}
mat4_t mat4_toRotationMat(mat4_t mat, mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = 0;
dest[13] = 0;
dest[14] = 0;
dest[15] = 1;
return dest;
}
mat3_t mat4_toMat3(mat4_t mat, mat3_t dest) {
if (!dest) { dest = mat3_create(NULL); }
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[4];
dest[4] = mat[5];
dest[5] = mat[6];
dest[6] = mat[8];
dest[7] = mat[9];
dest[8] = mat[10];
return dest;
}
mat3_t mat4_toInverseMat3(mat4_t mat, mat3_t dest) {
// Cache the matrix values (makes for huge speed increases!)
double a00 = mat[0], a01 = mat[1], a02 = mat[2],
a10 = mat[4], a11 = mat[5], a12 = mat[6],
a20 = mat[8], a21 = mat[9], a22 = mat[10],
b01 = a22 * a11 - a12 * a21,
b11 = -a22 * a10 + a12 * a20,
b21 = a21 * a10 - a11 * a20,
d = a00 * b01 + a01 * b11 + a02 * b21,
id;
if (!d) { return NULL; }
id = 1 / d;
if (!dest) { dest = mat3_create(NULL); }
dest[0] = b01 * id;
dest[1] = (-a22 * a01 + a02 * a21) * id;
dest[2] = (a12 * a01 - a02 * a11) * id;
dest[3] = b11 * id;
dest[4] = (a22 * a00 - a02 * a20) * id;
dest[5] = (-a12 * a00 + a02 * a10) * id;
dest[6] = b21 * id;
dest[7] = (-a21 * a00 + a01 * a20) * id;
dest[8] = (a11 * a00 - a01 * a10) * id;
return dest;
}
mat4_t mat4_multiply(mat4_t mat, mat4_t mat2, mat4_t dest) {
if (!dest) { dest = mat; }
// Cache the matrix values (makes for huge speed increases!)
double a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
b00 = mat2[0], b01 = mat2[1], b02 = mat2[2], b03 = mat2[3],
b10 = mat2[4], b11 = mat2[5], b12 = mat2[6], b13 = mat2[7],
b20 = mat2[8], b21 = mat2[9], b22 = mat2[10], b23 = mat2[11],
b30 = mat2[12], b31 = mat2[13], b32 = mat2[14], b33 = mat2[15];
dest[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
dest[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
dest[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
dest[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
dest[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
dest[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
dest[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
dest[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
dest[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
dest[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
dest[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
dest[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
dest[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
dest[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
dest[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
dest[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
return dest;
}
mat4_t mat4_multiplyVec3(mat4_t mat, vec3_t vec, mat4_t dest) {
if (!dest) { dest = vec; }
double x = vec[0], y = vec[1], z = vec[2];
dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
return dest;
}
mat4_t mat4_multiplyVec4(mat4_t mat, vec4_t vec, mat4_t dest) {
if (!dest) { dest = vec; }
double x = vec[0], y = vec[1], z = vec[2], w = vec[3];
dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12] * w;
dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13] * w;
dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14] * w;
dest[3] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15] * w;
return dest;
}
mat4_t mat4_translate(mat4_t mat, vec3_t vec, mat4_t dest) {
double x = vec[0], y = vec[1], z = vec[2],
a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23;
if (!dest || mat == dest) {
mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
return mat;
}
a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03;
dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13;
dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23;
dest[12] = a00 * x + a10 * y + a20 * z + mat[12];
dest[13] = a01 * x + a11 * y + a21 * z + mat[13];
dest[14] = a02 * x + a12 * y + a22 * z + mat[14];
dest[15] = a03 * x + a13 * y + a23 * z + mat[15];
return dest;
}
mat4_t mat4_scale(mat4_t mat, vec3_t vec, mat4_t dest) {
double x = vec[0], y = vec[1], z = vec[2];
if (!dest || mat == dest) {
mat[0] *= x;
mat[1] *= x;
mat[2] *= x;
mat[3] *= x;
mat[4] *= y;
mat[5] *= y;
mat[6] *= y;
mat[7] *= y;
mat[8] *= z;
mat[9] *= z;
mat[10] *= z;
mat[11] *= z;
return mat;
}
dest[0] = mat[0] * x;
dest[1] = mat[1] * x;
dest[2] = mat[2] * x;
dest[3] = mat[3] * x;
dest[4] = mat[4] * y;
dest[5] = mat[5] * y;
dest[6] = mat[6] * y;
dest[7] = mat[7] * y;
dest[8] = mat[8] * z;
dest[9] = mat[9] * z;
dest[10] = mat[10] * z;
dest[11] = mat[11] * z;
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
return dest;
}
mat4_t mat4_rotate(mat4_t mat, double angle, vec3_t axis, mat4_t dest) {
double x = axis[0], y = axis[1], z = axis[2],
len = sqrt(x * x + y * y + z * z),
s, c, t,
a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23,
b00, b01, b02,
b10, b11, b12,
b20, b21, b22;
if (!len) { return NULL; }
if (len != 1) {
len = 1 / len;
x *= len;
y *= len;
z *= len;
}
s = sin(angle);
c = cos(angle);
t = 1 - c;
a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
// Construct the elements of the rotation matrix
b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
if (!dest) {
dest = mat;
} else if (mat != dest) { // If the source and destination differ, copy the unchanged last row
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
// Perform rotation-specific matrix multiplication
dest[0] = a00 * b00 + a10 * b01 + a20 * b02;
dest[1] = a01 * b00 + a11 * b01 + a21 * b02;
dest[2] = a02 * b00 + a12 * b01 + a22 * b02;
dest[3] = a03 * b00 + a13 * b01 + a23 * b02;
dest[4] = a00 * b10 + a10 * b11 + a20 * b12;
dest[5] = a01 * b10 + a11 * b11 + a21 * b12;
dest[6] = a02 * b10 + a12 * b11 + a22 * b12;
dest[7] = a03 * b10 + a13 * b11 + a23 * b12;
dest[8] = a00 * b20 + a10 * b21 + a20 * b22;
dest[9] = a01 * b20 + a11 * b21 + a21 * b22;
dest[10] = a02 * b20 + a12 * b21 + a22 * b22;
dest[11] = a03 * b20 + a13 * b21 + a23 * b22;
return dest;
}
mat4_t mat4_rotateX(mat4_t mat, double angle, mat4_t dest) {
double s = sin(angle),
c = cos(angle),
a10 = mat[4],
a11 = mat[5],
a12 = mat[6],
a13 = mat[7],
a20 = mat[8],
a21 = mat[9],
a22 = mat[10],
a23 = mat[11];
if (!dest) {
dest = mat;
} else if (mat != dest) { // If the source and destination differ, copy the unchanged rows
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
// Perform axis-specific matrix multiplication
dest[4] = a10 * c + a20 * s;
dest[5] = a11 * c + a21 * s;
dest[6] = a12 * c + a22 * s;
dest[7] = a13 * c + a23 * s;
dest[8] = a10 * -s + a20 * c;
dest[9] = a11 * -s + a21 * c;
dest[10] = a12 * -s + a22 * c;
dest[11] = a13 * -s + a23 * c;
return dest;
}
mat4_t mat4_rotateY(mat4_t mat, double angle, mat4_t dest) {
double s = sin(angle),
c = cos(angle),
a00 = mat[0],
a01 = mat[1],
a02 = mat[2],
a03 = mat[3],
a20 = mat[8],
a21 = mat[9],
a22 = mat[10],
a23 = mat[11];
if (!dest) {
dest = mat;
} else if (mat != dest) { // If the source and destination differ, copy the unchanged rows
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
// Perform axis-specific matrix multiplication
dest[0] = a00 * c + a20 * -s;
dest[1] = a01 * c + a21 * -s;
dest[2] = a02 * c + a22 * -s;
dest[3] = a03 * c + a23 * -s;
dest[8] = a00 * s + a20 * c;
dest[9] = a01 * s + a21 * c;
dest[10] = a02 * s + a22 * c;
dest[11] = a03 * s + a23 * c;
return dest;
}
mat4_t mat4_rotateZ(mat4_t mat, double angle, mat4_t dest) {
double s = sin(angle),
c = cos(angle),
a00 = mat[0],
a01 = mat[1],
a02 = mat[2],
a03 = mat[3],
a10 = mat[4],
a11 = mat[5],
a12 = mat[6],
a13 = mat[7];
if (!dest) {
dest = mat;
} else if (mat != dest) { // If the source and destination differ, copy the unchanged last row
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
// Perform axis-specific matrix multiplication
dest[0] = a00 * c + a10 * s;
dest[1] = a01 * c + a11 * s;
dest[2] = a02 * c + a12 * s;
dest[3] = a03 * c + a13 * s;
dest[4] = a00 * -s + a10 * c;
dest[5] = a01 * -s + a11 * c;
dest[6] = a02 * -s + a12 * c;
dest[7] = a03 * -s + a13 * c;
return dest;
}
mat4_t mat4_frustum(double left, double right, double bottom, double top, double near, double far, mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
double rl = (right - left),
tb = (top - bottom),
fn = (far - near);
dest[0] = (near * 2) / rl;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
dest[4] = 0;
dest[5] = (near * 2) / tb;
dest[6] = 0;
dest[7] = 0;
dest[8] = (right + left) / rl;
dest[9] = (top + bottom) / tb;
dest[10] = -(far + near) / fn;
dest[11] = -1;
dest[12] = 0;
dest[13] = 0;
dest[14] = -(far * near * 2) / fn;
dest[15] = 0;
return dest;
}
mat4_t mat4_perspective(double fovy, double aspect, double near, double far, mat4_t dest) {
double top = near * tan(fovy * 3.14159265358979323846 / 360.0),
right = top * aspect;
return mat4_frustum(-right, right, -top, top, near, far, dest);
}
mat4_t mat4_ortho(double left, double right, double bottom, double top, double near, double far, mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
double rl = (right - left),
tb = (top - bottom),
fn = (far - near);
dest[0] = 2 / rl;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
dest[4] = 0;
dest[5] = 2 / tb;
dest[6] = 0;
dest[7] = 0;
dest[8] = 0;
dest[9] = 0;
dest[10] = -2 / fn;
dest[11] = 0;
dest[12] = -(left + right) / rl;
dest[13] = -(top + bottom) / tb;
dest[14] = -(far + near) / fn;
dest[15] = 1;
return dest;
}
mat4_t mat4_lookAt(vec3_t eye, vec3_t center, vec3_t up, mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
double x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
eyex = eye[0],
eyey = eye[1],
eyez = eye[2],
upx = up[0],
upy = up[1],
upz = up[2],
centerx = center[0],
centery = center[1],
centerz = center[2];
if (eyex == centerx && eyey == centery && eyez == centerz) {
return mat4_identity(dest);
}
//vec3.direction(eye, center, z);
z0 = eyex - centerx;
z1 = eyey - centery;
z2 = eyez - centerz;
// normalize (no check needed for 0 because of early return)
len = 1 / sqrt(z0 * z0 + z1 * z1 + z2 * z2);
z0 *= len;
z1 *= len;
z2 *= len;
//vec3.normalize(vec3.cross(up, z, x));
x0 = upy * z2 - upz * z1;
x1 = upz * z0 - upx * z2;
x2 = upx * z1 - upy * z0;
len = sqrt(x0 * x0 + x1 * x1 + x2 * x2);
if (!len) {
x0 = 0;
x1 = 0;
x2 = 0;
} else {
len = 1 / len;
x0 *= len;
x1 *= len;
x2 *= len;
}
//vec3.normalize(vec3.cross(z, x, y));
y0 = z1 * x2 - z2 * x1;
y1 = z2 * x0 - z0 * x2;
y2 = z0 * x1 - z1 * x0;
len = sqrt(y0 * y0 + y1 * y1 + y2 * y2);
if (!len) {
y0 = 0;
y1 = 0;
y2 = 0;
} else {
len = 1 / len;
y0 *= len;
y1 *= len;
y2 *= len;
}
dest[0] = x0;
dest[1] = y0;
dest[2] = z0;
dest[3] = 0;
dest[4] = x1;
dest[5] = y1;
dest[6] = z1;
dest[7] = 0;
dest[8] = x2;
dest[9] = y2;
dest[10] = z2;
dest[11] = 0;
dest[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
dest[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
dest[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
dest[15] = 1;
return dest;
}
mat4_t mat4_fromRotationTranslation(quat_t quat, vec3_t vec, mat4_t dest) {
if (!dest) { dest = mat4_create(NULL); }
// Quaternion math
double x = quat[0], y = quat[1], z = quat[2], w = quat[3],
x2 = x + x,
y2 = y + y,
z2 = z + z,
xx = x * x2,
xy = x * y2,
xz = x * z2,
yy = y * y2,
yz = y * z2,
zz = z * z2,
wx = w * x2,
wy = w * y2,
wz = w * z2;
dest[0] = 1 - (yy + zz);
dest[1] = xy + wz;
dest[2] = xz - wy;
dest[3] = 0;
dest[4] = xy - wz;
dest[5] = 1 - (xx + zz);
dest[6] = yz + wx;
dest[7] = 0;
dest[8] = xz + wy;
dest[9] = yz - wx;
dest[10] = 1 - (xx + yy);
dest[11] = 0;
dest[12] = vec[0];
dest[13] = vec[1];
dest[14] = vec[2];
dest[15] = 1;
return dest;
}