Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1
17#ifndef BJ_MAT_H
18#define BJ_MAT_H
19
20#include <banjo/api.h>
21#include <banjo/math.h>
22#include <banjo/vec.h>
23
24
25struct bj_mat3x3_t { bj_real m[9]; };
26typedef struct bj_mat3x3_t bj_mat3x3;
27typedef struct bj_mat3x3_t bj_mat3;
28#define BJ_M3(c,r) ((c)*3 + (r)) /* 0<=c,r<3 */
29
35struct bj_mat3x2_t { bj_real m[6]; };
36typedef struct bj_mat3x2_t bj_mat3x2;
37#define BJ_M32(c,r) ((c)*2 + (r)) /* 0<=c<3, 0<=r<2 */
38
43struct bj_mat4x4_t { bj_real m[16]; };
44typedef struct bj_mat4x4_t bj_mat4x4;
45typedef struct bj_mat4x4_t bj_mat4;
46#define BJ_M4(c,r) ((c)*4 + (r)) /* 0<=c,r<4) */
47
53struct bj_mat4x3_t { bj_real m[12]; };
54typedef struct bj_mat4x3_t bj_mat4x3;
55#define BJ_M43(c,r) ((c)*3 + (r)) /* 0<=c<4, 0<=r<3 */
56
62 bj_real* m = M->m;
63 m[BJ_M3(0, 0)] = BJ_F(1.0); m[BJ_M3(0, 1)] = BJ_FZERO; m[BJ_M3(0, 2)] = BJ_FZERO;
64 m[BJ_M3(1, 0)] = BJ_FZERO; m[BJ_M3(1, 1)] = BJ_F(1.0); m[BJ_M3(1, 2)] = BJ_FZERO;
65 m[BJ_M3(2, 0)] = BJ_FZERO; m[BJ_M3(2, 1)] = BJ_FZERO; m[BJ_M3(2, 2)] = BJ_F(1.0);
66}
67
75 const bj_mat3* BJ_RESTRICT src
76) {
77 for (int i = 0 ; i < 9 ; ++i) {
78 dst->m[i] = src->m[i];
79 }
80}
81
89 const bj_real* m = M->m;
90 return (bj_vec3) {
91 m[BJ_M3(0, r)], m[BJ_M3(1, r)], m[BJ_M3(2, r)]
92 };
93}
94
102 const bj_real* m = M->m;
103 return (bj_vec3) { m[BJ_M3(c, 0)], m[BJ_M3(c, 1)], m[BJ_M3(c, 2)] };
104}
105
112 const bj_real* a = A->m;
113 bj_real r[9];
114 r[BJ_M3(0,0)] = a[BJ_M3(0,0)];
115 r[BJ_M3(0,1)] = a[BJ_M3(1,0)];
116 r[BJ_M3(0,2)] = a[BJ_M3(2,0)];
117 r[BJ_M3(1,0)] = a[BJ_M3(0,1)];
118 r[BJ_M3(1,1)] = a[BJ_M3(1,1)];
119 r[BJ_M3(1,2)] = a[BJ_M3(2,1)];
120 r[BJ_M3(2,0)] = a[BJ_M3(0,2)];
121 r[BJ_M3(2,1)] = a[BJ_M3(1,2)];
122 r[BJ_M3(2,2)] = a[BJ_M3(2,2)];
123 for (int i = 0 ; i<9 ; ++i) {
124 out->m[i] = r[i];
125 }
126}
127
135 bj_mat3* BJ_RESTRICT out,
136 const bj_mat3* BJ_RESTRICT A,
137 const bj_mat3* BJ_RESTRICT B
138) {
139 for (int i = 0 ; i < 9 ; ++i) {
140 out->m[i] = A->m[i] + B->m[i];
141 }
142}
143
151 bj_mat3* BJ_RESTRICT out,
152 const bj_mat3* BJ_RESTRICT A,
153 const bj_mat3* BJ_RESTRICT B
154) {
155 for (int i = 0 ; i < 9 ; ++i) {
156 out->m[i] = A->m[i] - B->m[i];
157 }
158}
159
167 bj_mat3* BJ_RESTRICT out,
168 const bj_mat3* BJ_RESTRICT A,
169 bj_real k
170) {
171 for (int i = 0 ; i < 9 ; ++i) {
172 out->m[i] = A->m[i] * k;
173 }
174}
175
184 bj_mat3* BJ_RESTRICT out,
185 const bj_mat3* BJ_RESTRICT A,
186 const bj_mat3* BJ_RESTRICT B
187) {
188 const bj_real* a = A->m;
189 const bj_real* b = B->m;
190 bj_real* o = out->m;
191
192 for (int c = 0; c < 3; ++c) {
193 const bj_real b0 = b[BJ_M3(c, 0)];
194 const bj_real b1 = b[BJ_M3(c, 1)];
195 const bj_real b2 = b[BJ_M3(c, 2)];
196 o[BJ_M3(c,0)] = a[BJ_M3(0,0)] * b0 + a[BJ_M3(1,0)] * b1 + a[BJ_M3(2,0)] * b2;
197 o[BJ_M3(c,1)] = a[BJ_M3(0,1)] * b0 + a[BJ_M3(1,1)] * b1 + a[BJ_M3(2,1)] * b2;
198 o[BJ_M3(c,2)] = a[BJ_M3(0,2)] * b0 + a[BJ_M3(1,2)] * b1 + a[BJ_M3(2,2)] * b2;
199 }
200}
201
210 const bj_mat3* BJ_RESTRICT M,
211 bj_vec3 v
212) {
213 const bj_real* m = M->m;
214 return (bj_vec3) {
215 m[BJ_M3(0,0)] * v.x + m[BJ_M3(1,0)] * v.y + m[BJ_M3(2,0)] * v.z,
216 m[BJ_M3(0,1)] * v.x + m[BJ_M3(1,1)] * v.y + m[BJ_M3(2,1)] * v.z,
217 m[BJ_M3(0,2)] * v.x + m[BJ_M3(1,2)] * v.y + m[BJ_M3(2,2)] * v.z
218 };
219}
220
229 const bj_mat3* BJ_RESTRICT M,
230 bj_vec2 p
231) {
232 const bj_real* m = M->m;
233 const bj_real x = p.x;
234 const bj_real y = p.y;
235 const bj_real w = m[BJ_M3(0,2)] * x + m[BJ_M3(1,2)] * y + m[BJ_M3(2,2)];
236 const bj_real rx = m[BJ_M3(0,0)] * x + m[BJ_M3(1,0)] * y + m[BJ_M3(2,0)];
237 const bj_real ry = m[BJ_M3(0,1)] * x + m[BJ_M3(1,1)] * y + m[BJ_M3(2,1)];
238 if (w != BJ_FZERO) {
239 return (bj_vec2) { rx / w, ry / w };
240 }
241 return (bj_vec2) { rx, ry };
242}
243
252 bj_real tx,
253 bj_real ty
254) {
256 M->m[BJ_M3(2,0)] = tx;
257 M->m[BJ_M3(2,1)] = ty;
258}
259
269 bj_real tx,
270 bj_real ty
271) {
272 const bj_vec3 t = { tx, ty, BJ_FZERO };
273 for (int r = 0 ; r<3 ; ++r) {
274 const bj_vec3 row = bj_mat3_row(M, r);
275 M->m[BJ_M3(2,r)] += row.x * t.x + row.y * t.y + row.z * t.z;
276 }
277}
278
287 bj_real sx,
288 bj_real sy
289) {
291 M->m[BJ_M3(0,0)] = sx;
292 M->m[BJ_M3(1,1)] = sy;
293}
294
303 bj_real shx,
304 bj_real shy
305) {
307 M->m[BJ_M3(1,0)] = shy;
308 M->m[BJ_M3(0,1)] = shx;
309}
310
318 bj_real angle
319) {
320 const bj_real s = bj_sin(angle);
321 const bj_real c = bj_cos(angle);
322 bj_real* m = M->m;
323 m[BJ_M3(0,0)] = c;
324 m[BJ_M3(0,1)] = s;
325 m[BJ_M3(0,2)] = BJ_FZERO;
326 m[BJ_M3(1,0)] = -s;
327 m[BJ_M3(1,1)] = c;
328 m[BJ_M3(1,2)] = BJ_FZERO;
329 m[BJ_M3(2,0)] = BJ_FZERO;
330 m[BJ_M3(2,1)] = BJ_FZERO;
331 m[BJ_M3(2,2)] = BJ_F(1.0);
332}
333
340 const bj_mat3* BJ_RESTRICT A
341) {
342 const bj_real* m = A->m;
343 return
344 m[BJ_M3(0,0)] * (m[BJ_M3(1,1)] * m[BJ_M3(2,2)]-m[BJ_M3(2,1)] * m[BJ_M3(1,2)])
345 - m[BJ_M3(1,0)] * (m[BJ_M3(0,1)] * m[BJ_M3(2,2)]-m[BJ_M3(2,1)] * m[BJ_M3(0,2)])
346 + m[BJ_M3(2,0)] * (m[BJ_M3(0,1)] * m[BJ_M3(1,2)]-m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
347}
348
364 bj_mat3* BJ_RESTRICT out,
365 const bj_mat3* BJ_RESTRICT A
366) {
367 const bj_real* m = A->m;
368 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
369 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
370 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
371 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
372 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
373 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
374 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
375 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
376 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
377 const bj_real det = m[BJ_M3(0,0)] * c00 + m[BJ_M3(1,0)] * c01 + m[BJ_M3(2,0)] * c02;
378
379 if (bj_real_is_zero(det)) {
380 return BJ_FALSE;
381 }
382 const bj_real id = BJ_F(1.0) / det;
383 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(1,0)] = c01 * id; out->m[BJ_M3(2,0)] = c02 * id;
384 out->m[BJ_M3(0,1)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(2,1)] = c12 * id;
385 out->m[BJ_M3(0,2)] = c20 * id; out->m[BJ_M3(1,2)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
386 return BJ_TRUE;
387}
388
401 bj_mat3* BJ_RESTRICT out,
402 const bj_mat3* BJ_RESTRICT A
403) {
404 const bj_real* m = A->m;
405 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
406 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
407 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
408 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
409 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
410 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
411 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
412 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
413 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
414 const bj_real id = BJ_F(1.0) / (m[BJ_M3(0,0)] * c00 + m[BJ_M3(1,0)] * c01 + m[BJ_M3(2,0)] * c02);
415
416 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(1,0)] = c01 * id; out->m[BJ_M3(2,0)] = c02 * id;
417 out->m[BJ_M3(0,1)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(2,1)] = c12 * id;
418 out->m[BJ_M3(0,2)] = c20 * id; out->m[BJ_M3(1,2)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
419}
420
421
432 bj_real l, bj_real r,
433 bj_real b, bj_real t
434) {
435 bj_real* m = M->m;
436 m[BJ_M3(0,0)] = BJ_F(2.0)/(r-l);
437 m[BJ_M3(0,1)] = BJ_FZERO;
438 m[BJ_M3(0,2)] = BJ_FZERO;
439
440 m[BJ_M3(1,0)] = BJ_FZERO;
441 m[BJ_M3(1,1)] = BJ_F(-2.0)/(t-b);
442 m[BJ_M3(1,2)] = BJ_FZERO;
443
444 m[BJ_M3(2,0)] = -(r+l)/(r-l);
445 m[BJ_M3(2,1)] = (t+b)/(t-b);
446 m[BJ_M3(2,2)] = BJ_F(1.0);
447}
448
459 bj_real x,
460 bj_real y,
461 bj_real w,
462 bj_real h
463) {
464 bj_real* m = M->m;
465 m[BJ_M3(0,0)] = BJ_F(0.5) * w;
466 m[BJ_M3(0,1)] = BJ_FZERO;
467 m[BJ_M3(0,2)] = BJ_FZERO;
468 m[BJ_M3(1,0)] = BJ_FZERO;
469 m[BJ_M3(1,1)] = BJ_F(0.5) * h;
470 m[BJ_M3(1,2)] = BJ_FZERO;
471 m[BJ_M3(2,0)] = x + BJ_F(0.5) * w;
472 m[BJ_M3(2,1)] = y + BJ_F(0.5) * h;
473 m[BJ_M3(2,2)] = BJ_F(1.0);
474}
475
482) {
483 bj_real* m = M->m;
484 m[BJ_M32(0,0)] = BJ_F(1.0);
485 m[BJ_M32(0,1)] = BJ_FZERO;
486 m[BJ_M32(1,0)] = BJ_FZERO;
487 m[BJ_M32(1,1)] = BJ_F(1.0);
488 m[BJ_M32(2,0)] = BJ_FZERO;
489 m[BJ_M32(2,1)] = BJ_FZERO;
490}
491
500 bj_real tx,
501 bj_real ty
502) {
504 M->m[BJ_M32(2,0)] = tx;
505 M->m[BJ_M32(2,1)] = ty;
506}
507
516 bj_real sx,
517 bj_real sy
518) {
520 M->m[BJ_M32(0,0)] = sx;
521 M->m[BJ_M32(1,1)] = sy;
522}
523
531 bj_real angle
532) {
533 const bj_real c = bj_cos(angle);
534 const bj_real s = bj_sin(angle);
535 bj_real* m = M->m;
536 m[BJ_M32(0,0)] = c;
537 m[BJ_M32(0,1)] = s;
538 m[BJ_M32(1,0)] = -s;
539 m[BJ_M32(1,1)] = c;
540 m[BJ_M32(2,0)] = BJ_FZERO;
541 m[BJ_M32(2,1)] = BJ_FZERO;
542}
543
553 const bj_mat3x2* BJ_RESTRICT A,
554 const bj_mat3x2* BJ_RESTRICT B
555) {
556 const bj_real a00 = A->m[BJ_M32(0,0)];
557 const bj_real a10 = A->m[BJ_M32(0,1)];
558 const bj_real a01 = A->m[BJ_M32(1,0)];
559 const bj_real a11 = A->m[BJ_M32(1,1)];
560 const bj_real a02 = A->m[BJ_M32(2,0)];
561 const bj_real a12 = A->m[BJ_M32(2,1)];
562
563 const bj_real b00 = B->m[BJ_M32(0,0)];
564 const bj_real b10 = B->m[BJ_M32(0,1)];
565 const bj_real b01 = B->m[BJ_M32(1,0)];
566 const bj_real b11 = B->m[BJ_M32(1,1)];
567 const bj_real b02 = B->m[BJ_M32(2,0)];
568 const bj_real b12 = B->m[BJ_M32(2,1)];
569
570 out->m[BJ_M32(0,0)] = a00*b00 + a01*b10;
571 out->m[BJ_M32(0,1)] = a10*b00 + a11*b10;
572 out->m[BJ_M32(1,0)] = a00*b01 + a01*b11;
573 out->m[BJ_M32(1,1)] = a10*b01 + a11*b11;
574 out->m[BJ_M32(2,0)] = a00*b02 + a01*b12 + a02;
575 out->m[BJ_M32(2,1)] = a10*b02 + a11*b12 + a12;
576}
577
578
587 const bj_mat3x2* BJ_RESTRICT M,
588 bj_vec2 p
589) {
590 const bj_real* m = M->m;
591 const bj_real x = p.x;
592 const bj_real y = p.y;
593 return (bj_vec2) {
594 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y + m[BJ_M32(2,0)],
595 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y + m[BJ_M32(2,1)]
596 };
597}
598
607 const bj_mat3x2* BJ_RESTRICT M,
608 bj_vec2 v
609) {
610 const bj_real* m = M->m;
611 const bj_real x = v.x;
612 const bj_real y = v.y;
613 return (bj_vec2) {
614 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y,
615 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y
616 };
617}
618
626 const bj_mat3x2* BJ_RESTRICT A
627) {
628 bj_real* o = M->m;
629 const bj_real* a = A->m;
630 o[BJ_M3(0,0)] = a[BJ_M32(0,0)];
631 o[BJ_M3(0,1)] = a[BJ_M32(0,1)];
632 o[BJ_M3(0,2)] = BJ_FZERO;
633 o[BJ_M3(1,0)] = a[BJ_M32(1,0)];
634 o[BJ_M3(1,1)] = a[BJ_M32(1,1)];
635 o[BJ_M3(1,2)] = BJ_FZERO;
636 o[BJ_M3(2,0)] = a[BJ_M32(2,0)];
637 o[BJ_M3(2,1)] = a[BJ_M32(2,1)];
638 o[BJ_M3(2,2)] = BJ_F(1.0);
639}
640
648 const bj_mat3* BJ_RESTRICT A
649) {
650 const bj_real* a = A->m;
651 bj_real* o = M->m;
652 o[BJ_M32(0,0)] = a[BJ_M3(0,0)];
653 o[BJ_M32(0,1)] = a[BJ_M3(0,1)];
654 o[BJ_M32(1,0)] = a[BJ_M3(1,0)];
655 o[BJ_M32(1,1)] = a[BJ_M3(1,1)];
656 o[BJ_M32(2,0)] = a[BJ_M3(2,0)];
657 o[BJ_M32(2,1)] = a[BJ_M3(2,1)];
658}
659
666) {
667 bj_real* m = M->m;
668 m[BJ_M4(0,0)] = BJ_F(1.0);
669 m[BJ_M4(0,1)] = BJ_FZERO;
670 m[BJ_M4(0,2)] = BJ_FZERO;
671 m[BJ_M4(0,3)] = BJ_FZERO;
672 m[BJ_M4(1,0)] = BJ_FZERO;
673 m[BJ_M4(1,1)] = BJ_F(1.0);
674 m[BJ_M4(1,2)] = BJ_FZERO;
675 m[BJ_M4(1,3)] = BJ_FZERO;
676 m[BJ_M4(2,0)] = BJ_FZERO;
677 m[BJ_M4(2,1)] = BJ_FZERO;
678 m[BJ_M4(2,2)] = BJ_F(1.0);
679 m[BJ_M4(2,3)] = BJ_FZERO;
680 m[BJ_M4(3,0)] = BJ_FZERO;
681 m[BJ_M4(3,1)] = BJ_FZERO;
682 m[BJ_M4(3,2)] = BJ_FZERO;
683 m[BJ_M4(3,3)] = BJ_F(1.0);
684}
685
692 bj_mat4* BJ_RESTRICT dst,
693 const bj_mat4* BJ_RESTRICT src
694) {
695 for(int i = 0 ; i < 16 ; ++i) {
696 dst->m[i] = src->m[i];
697 }
698}
699
707 const bj_mat4* BJ_RESTRICT M,
708 int r
709) {
710 const bj_real* m = M->m;
711 return (bj_vec4) {
712 m[BJ_M4(0,r)],
713 m[BJ_M4(1,r)],
714 m[BJ_M4(2,r)],
715 m[BJ_M4(3,r)]
716 };
717}
718
726 const bj_mat4* BJ_RESTRICT M,
727 int c
728) {
729 const bj_real* m = M->m;
730 return (bj_vec4) {
731 m[BJ_M4(c,0)],
732 m[BJ_M4(c,1)],
733 m[BJ_M4(c,2)],
734 m[BJ_M4(c,3)]
735 };
736}
737
744 bj_mat4* BJ_RESTRICT out,
745 const bj_mat4* BJ_RESTRICT A
746) {
747 const bj_real* a = A->m;
748 bj_real r[16];
749 for(int c = 0 ; c < 4; ++c) {
750 for(int r0 = 0 ; r0 < 4; ++r0) {
751 r[BJ_M4(r0,c)] = a[BJ_M4(c,r0)];
752 }
753 }
754 for(int i = 0 ; i < 16 ; ++i) {
755 out->m[i] = r[i];
756 }
757}
758
766 bj_mat4* BJ_RESTRICT out,
767 const bj_mat4* BJ_RESTRICT A,
768 const bj_mat4* BJ_RESTRICT B
769) {
770
771 for(int i = 0 ; i < 16 ; ++i) {
772 out->m[i] = A->m[i] + B->m[i];
773 }
774}
775
783 bj_mat4* BJ_RESTRICT out,
784 const bj_mat4* BJ_RESTRICT A,
785 const bj_mat4* BJ_RESTRICT B
786) {
787
788 for(int i = 0 ; i < 16 ; ++i) {
789 out->m[i] = A->m[i] - B->m[i];
790 }
791}
792
800 bj_mat4* BJ_RESTRICT out,
801 const bj_mat4* BJ_RESTRICT A,
802 bj_real k
803) {
804
805 for(int i = 0;i<16;++i) {
806 out->m[i] = A->m[i] * k;
807 }
808}
809
819 bj_mat4* BJ_RESTRICT out,
820 const bj_mat4* BJ_RESTRICT A,
821 bj_real sx,
822 bj_real sy,
823 bj_real sz
824) {
825
826 const bj_real* a = A->m;
827 bj_real* m = out->m;
828 m[BJ_M4(0,0)] = a[BJ_M4(0,0)] * sx;
829 m[BJ_M4(0,1)] = a[BJ_M4(0,1)] * sx;
830 m[BJ_M4(0,2)] = a[BJ_M4(0,2)] * sx;
831 m[BJ_M4(0,3)] = a[BJ_M4(0,3)] * sx;
832 m[BJ_M4(1,0)] = a[BJ_M4(1,0)] * sy;
833 m[BJ_M4(1,1)] = a[BJ_M4(1,1)] * sy;
834 m[BJ_M4(1,2)] = a[BJ_M4(1,2)] * sy;
835 m[BJ_M4(1,3)] = a[BJ_M4(1,3)] * sy;
836 m[BJ_M4(2,0)] = a[BJ_M4(2,0)] * sz;
837 m[BJ_M4(2,1)] = a[BJ_M4(2,1)] * sz;
838 m[BJ_M4(2,2)] = a[BJ_M4(2,2)] * sz;
839 m[BJ_M4(2,3)] = a[BJ_M4(2,3)] * sz;
840 m[BJ_M4(3,0)] = a[BJ_M4(3,0)];
841 m[BJ_M4(3,1)] = a[BJ_M4(3,1)];
842 m[BJ_M4(3,2)] = a[BJ_M4(3,2)];
843 m[BJ_M4(3,3)] = a[BJ_M4(3,3)];
844}
845
854 bj_mat4* BJ_RESTRICT out,
855 const bj_mat4* BJ_RESTRICT A,
856 const bj_mat4* BJ_RESTRICT B
857)
858{
859 const bj_real* a = A->m;
860 const bj_real* b = B->m;
861 bj_real* o = out->m;
862
863 for (int c = 0; c < 4; ++c) {
864 const bj_real b0 = b[BJ_M4(c,0)];
865 const bj_real b1 = b[BJ_M4(c,1)];
866 const bj_real b2 = b[BJ_M4(c,2)];
867 const bj_real b3 = b[BJ_M4(c,3)];
868
869 o[BJ_M4(c,0)] = a[BJ_M4(0,0)] * b0
870 + a[BJ_M4(1,0)] * b1
871 + a[BJ_M4(2,0)] * b2
872 + a[BJ_M4(3,0)] * b3;
873 o[BJ_M4(c,1)] = a[BJ_M4(0,1)] * b0
874 + a[BJ_M4(1,1)] * b1
875 + a[BJ_M4(2,1)] * b2
876 + a[BJ_M4(3,1)] * b3;
877 o[BJ_M4(c,2)] = a[BJ_M4(0,2)] * b0
878 + a[BJ_M4(1,2)] * b1
879 + a[BJ_M4(2,2)] * b2
880 + a[BJ_M4(3,2)] * b3;
881 o[BJ_M4(c,3)] = a[BJ_M4(0,3)] * b0
882 + a[BJ_M4(1,3)] * b1
883 + a[BJ_M4(2,3)] * b2
884 + a[BJ_M4(3,3)] * b3;
885 }
886}
887
896 const bj_mat4* BJ_RESTRICT M,
897 bj_vec4 v
898) {
899 const bj_real* m = M->m;
900 return (bj_vec4) {
901 m[BJ_M4(0,0)] * v.x + m[BJ_M4(1,0)] * v.y +
902 m[BJ_M4(2,0)] * v.z + m[BJ_M4(3,0)] * v.w,
903 m[BJ_M4(0,1)] * v.x + m[BJ_M4(1,1)] * v.y +
904 m[BJ_M4(2,1)] * v.z + m[BJ_M4(3,1)] * v.w,
905 m[BJ_M4(0,2)] * v.x + m[BJ_M4(1,2)] * v.y +
906 m[BJ_M4(2,2)] * v.z + m[BJ_M4(3,2)] * v.w,
907 m[BJ_M4(0,3)] * v.x + m[BJ_M4(1,3)] * v.y +
908 m[BJ_M4(2,3)] * v.z + m[BJ_M4(3,3)] * v.w
909 };
910}
911
921 bj_real x,
922 bj_real y,
923 bj_real z
924) {
926 M->m[BJ_M4(3,0)] = x;
927 M->m[BJ_M4(3,1)] = y;
928 M->m[BJ_M4(3,2)] = z;
929}
930
941 bj_real x,
942 bj_real y,
943 bj_real z
944) {
945 const bj_vec4 t = { x, y, z, BJ_FZERO };
946 for (int r = 0 ; r<4 ; ++r) {
947 const bj_vec4 row = bj_mat4_row(M, r);
948 M->m[BJ_M4(3,r)] += bj_vec4_dot(row, t);
949 }
950}
951
959 bj_mat4* BJ_RESTRICT out,
960 bj_vec3 a,
961 bj_vec3 b
962) {
963 bj_real* m = out->m;
964 m[BJ_M4(0,0)] = a.x*b.x;
965 m[BJ_M4(1,0)] = a.y*b.x;
966 m[BJ_M4(2,0)] = a.z*b.x;
967 m[BJ_M4(3,0)] = BJ_FZERO;
968 m[BJ_M4(0,1)] = a.x*b.y;
969 m[BJ_M4(1,1)] = a.y*b.y;
970 m[BJ_M4(2,1)] = a.z*b.y;
971 m[BJ_M4(3,1)] = BJ_FZERO;
972 m[BJ_M4(0,2)] = a.x*b.z;
973 m[BJ_M4(1,2)] = a.y*b.z;
974 m[BJ_M4(2,2)] = a.z*b.z;
975 m[BJ_M4(3,2)] = BJ_FZERO;
976 m[BJ_M4(0,3)] = BJ_FZERO;
977 m[BJ_M4(1,3)] = BJ_FZERO;
978 m[BJ_M4(2,3)] = BJ_FZERO;
979 m[BJ_M4(3,3)] = BJ_FZERO;
980}
981
990 bj_mat4* restrict out,
991 const bj_mat4* restrict M,
992 bj_vec3 axis,
993 bj_real angle
994) {
995 const bj_real s = bj_sin(angle);
996 const bj_real c = bj_cos(angle);
997 const bj_real len = bj_vec3_len(axis);
998 if (len <= BJ_F(1e-4)) {
999 bj_mat4_copy(out, M);
1000 return;
1001 }
1002
1003 const bj_real inv = BJ_F(1.0) / len;
1004 const bj_real ux = axis.x * inv, uy = axis.y * inv, uz = axis.z * inv;
1005 const bj_real t = BJ_F(1.0) - c;
1006
1007 /* 3x3 rotation block */
1008 const bj_real r00 = c + t*ux*ux;
1009 const bj_real r01 = t*ux*uy + s*uz;
1010 const bj_real r02 = t*ux*uz - s*uy;
1011
1012 const bj_real r10 = t*uy*ux - s*uz;
1013 const bj_real r11 = c + t*uy*uy;
1014 const bj_real r12 = t*uy*uz + s*ux;
1015
1016 const bj_real r20 = t*uz*ux + s*uy;
1017 const bj_real r21 = t*uz*uy - s*ux;
1018 const bj_real r22 = c + t*uz*uz;
1019
1020 const bj_real* m = M->m;
1021 bj_real* o = out->m;
1022
1023 /* Columns of M (column-major) */
1024 const bj_real* m0 = &m[BJ_M4(0,0)];
1025 const bj_real* m1 = &m[BJ_M4(1,0)];
1026 const bj_real* m2 = &m[BJ_M4(2,0)];
1027 const bj_real* m3 = &m[BJ_M4(3,0)];
1028
1029 bj_real* o0 = &o[BJ_M4(0,0)];
1030 bj_real* o1 = &o[BJ_M4(1,0)];
1031 bj_real* o2 = &o[BJ_M4(2,0)];
1032 bj_real* o3 = &o[BJ_M4(3,0)];
1033
1034 for (int r = 0; r < 4; ++r) {
1035 const bj_real M0 = m0[r], M1 = m1[r], M2 = m2[r];
1036 o0[r] = M0*r00 + M1*r10 + M2*r20;
1037 o1[r] = M0*r01 + M1*r11 + M2*r21;
1038 o2[r] = M0*r02 + M1*r12 + M2*r22;
1039 o3[r] = m3[r];
1040 }
1041}
1042
1050 bj_mat4* restrict out,
1051 const bj_mat4* restrict M,
1052 bj_real a
1053) {
1054
1055 const bj_real s = bj_sin(a), c = bj_cos(a);
1056 const bj_real* m = M->m; bj_real* o = out->m;
1057
1058 const bj_real* m0 = &m[BJ_M4(0,0)];
1059 const bj_real* m1 = &m[BJ_M4(1,0)];
1060 const bj_real* m2 = &m[BJ_M4(2,0)];
1061 const bj_real* m3 = &m[BJ_M4(3,0)];
1062
1063 bj_real* o0 = &o[BJ_M4(0,0)];
1064 bj_real* o1 = &o[BJ_M4(1,0)];
1065 bj_real* o2 = &o[BJ_M4(2,0)];
1066 bj_real* o3 = &o[BJ_M4(3,0)];
1067
1068 for (int r = 0; r < 4; ++r) {
1069 const bj_real M1 = m1[r], M2 = m2[r];
1070 o0[r] = m0[r];
1071 o1[r] = c*M1 - s*M2;
1072 o2[r] = s*M1 + c*M2;
1073 o3[r] = m3[r];
1074 }
1075}
1076
1084 bj_mat4* restrict out,
1085 const bj_mat4* restrict M,
1086 bj_real a
1087) {
1088 const bj_real s = bj_sin(a), c = bj_cos(a);
1089 const bj_real* m = M->m; bj_real* o = out->m;
1090
1091 const bj_real* m0 = &m[BJ_M4(0,0)];
1092 const bj_real* m1 = &m[BJ_M4(1,0)];
1093 const bj_real* m2 = &m[BJ_M4(2,0)];
1094 const bj_real* m3 = &m[BJ_M4(3,0)];
1095
1096 bj_real* o0 = &o[BJ_M4(0,0)];
1097 bj_real* o1 = &o[BJ_M4(1,0)];
1098 bj_real* o2 = &o[BJ_M4(2,0)];
1099 bj_real* o3 = &o[BJ_M4(3,0)];
1100
1101 for (int r = 0; r < 4; ++r) {
1102 const bj_real M0 = m0[r], M2 = m2[r];
1103 o0[r] = c*M0 + s*M2;
1104 o1[r] = m1[r];
1105 o2[r] = -s*M0 + c*M2;
1106 o3[r] = m3[r];
1107 }
1108}
1109
1117 bj_mat4* restrict out,
1118 const bj_mat4* restrict M,
1119 bj_real a
1120) {
1121 const bj_real s = bj_sin(a), c = bj_cos(a);
1122 const bj_real* m = M->m; bj_real* o = out->m;
1123
1124 const bj_real* m0 = &m[BJ_M4(0,0)];
1125 const bj_real* m1 = &m[BJ_M4(1,0)];
1126 const bj_real* m2 = &m[BJ_M4(2,0)];
1127 const bj_real* m3 = &m[BJ_M4(3,0)];
1128
1129 bj_real* o0 = &o[BJ_M4(0,0)];
1130 bj_real* o1 = &o[BJ_M4(1,0)];
1131 bj_real* o2 = &o[BJ_M4(2,0)];
1132 bj_real* o3 = &o[BJ_M4(3,0)];
1133
1134 for (int r = 0; r < 4; ++r) {
1135 const bj_real M0 = m0[r], M1 = m1[r];
1136 o0[r] = c*M0 - s*M1;
1137 o1[r] = s*M0 + c*M1;
1138 o2[r] = m2[r];
1139 o3[r] = m3[r];
1140 }
1141}
1142
1153 const bj_mat4* BJ_RESTRICT M,
1154 bj_vec2 a,
1155 bj_vec2 b,
1156 bj_real s
1157) {
1158 bj_real z_a = BJ_FZERO;
1159 bj_real z_b = BJ_FZERO;
1160 const bj_real la = bj_vec2_len(a);
1161 const bj_real lb = bj_vec2_len(b);
1162 if (la < BJ_F(1.0)) {
1163 z_a = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(a,a));
1164 } else {
1165 a = bj_vec2_normalize(a);
1166 }
1167 if (lb < BJ_F(1.0)) {
1168 z_b = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(b,b));
1169 } else {
1170 b = bj_vec2_normalize(b);
1171 }
1172 const bj_vec3 A = { a.x, a.y, z_a };
1173 const bj_vec3 B = { b.x, b.y, z_b };
1174 const bj_vec3 C = bj_vec3_cross(A,B);
1175 const bj_real ang = bj_acos(bj_vec3_dot(A,B)) * s;
1176 bj_mat4_rotate_axis_andle(R, M, C, ang);
1177}
1178
1195 bj_mat4* BJ_RESTRICT out,
1196 const bj_mat4* BJ_RESTRICT M
1197) {
1198 const bj_real* m = M->m;
1199 bj_real s[6], c[6];
1200
1201 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1202 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1203 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1204 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1205 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1206 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1207
1208 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1209 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1210 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1211 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1212 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1213 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1214
1215 const bj_real det = s[0]*c[5] - s[1]*c[4] + s[2]*c[3]
1216 + s[3]*c[2] - s[4]*c[1] + s[5]*c[0];
1217
1218 if (bj_real_is_zero(det)) {
1219 return BJ_FALSE;
1220 }
1221 const bj_real id = BJ_F(1.0) / det;
1222 bj_real* o = out->m;
1223
1224 o[BJ_M4(0,0)] = ( m[BJ_M4(1,1)] * c[5] - m[BJ_M4(1,2)] * c[4] + m[BJ_M4(1,3)] * c[3]) * id;
1225 o[BJ_M4(0,1)] = (-m[BJ_M4(0,1)] * c[5] + m[BJ_M4(0,2)] * c[4] - m[BJ_M4(0,3)] * c[3]) * id;
1226 o[BJ_M4(0,2)] = ( m[BJ_M4(3,1)] * s[5] - m[BJ_M4(3,2)] * s[4] + m[BJ_M4(3,3)] * s[3]) * id;
1227 o[BJ_M4(0,3)] = (-m[BJ_M4(2,1)] * s[5] + m[BJ_M4(2,2)] * s[4] - m[BJ_M4(2,3)] * s[3]) * id;
1228
1229 o[BJ_M4(1,0)] = (-m[BJ_M4(1,0)] * c[5] + m[BJ_M4(1,2)] * c[2] - m[BJ_M4(1,3)] * c[1]) * id;
1230 o[BJ_M4(1,1)] = ( m[BJ_M4(0,0)] * c[5] - m[BJ_M4(0,2)] * c[2] + m[BJ_M4(0,3)] * c[1]) * id;
1231 o[BJ_M4(1,2)] = (-m[BJ_M4(3,0)] * s[5] + m[BJ_M4(3,2)] * s[2] - m[BJ_M4(3,3)] * s[1]) * id;
1232 o[BJ_M4(1,3)] = ( m[BJ_M4(2,0)] * s[5] - m[BJ_M4(2,2)] * s[2] + m[BJ_M4(2,3)] * s[1]) * id;
1233
1234 o[BJ_M4(2,0)] = ( m[BJ_M4(1,0)] * c[4] - m[BJ_M4(1,1)] * c[2] + m[BJ_M4(1,3)] * c[0]) * id;
1235 o[BJ_M4(2,1)] = (-m[BJ_M4(0,0)] * c[4] + m[BJ_M4(0,1)] * c[2] - m[BJ_M4(0,3)] * c[0]) * id;
1236 o[BJ_M4(2,2)] = ( m[BJ_M4(3,0)] * s[4] - m[BJ_M4(3,1)] * s[2] + m[BJ_M4(3,3)] * s[0]) * id;
1237 o[BJ_M4(2,3)] = (-m[BJ_M4(2,0)] * s[4] + m[BJ_M4(2,1)] * s[2] - m[BJ_M4(2,3)] * s[0]) * id;
1238
1239 o[BJ_M4(3,0)] = (-m[BJ_M4(1,0)] * c[3] + m[BJ_M4(1,1)] * c[1] - m[BJ_M4(1,2)] * c[0]) * id;
1240 o[BJ_M4(3,1)] = ( m[BJ_M4(0,0)] * c[3] - m[BJ_M4(0,1)] * c[1] + m[BJ_M4(0,2)] * c[0]) * id;
1241 o[BJ_M4(3,2)] = (-m[BJ_M4(3,0)] * s[3] + m[BJ_M4(3,1)] * s[1] - m[BJ_M4(3,2)] * s[0]) * id;
1242 o[BJ_M4(3,3)] = ( m[BJ_M4(2,0)] * s[3] - m[BJ_M4(2,1)] * s[1] + m[BJ_M4(2,2)] * s[0]) * id;
1243
1244 return BJ_TRUE;
1245}
1246
1259 bj_mat4* BJ_RESTRICT out,
1260 const bj_mat4* BJ_RESTRICT M
1261) {
1262 const bj_real* m = M->m;
1263 bj_real s[6], c[6];
1264
1265 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1266 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1267 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1268 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1269 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1270 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1271
1272 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1273 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1274 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1275 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1276 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1277 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1278
1279 const bj_real id = BJ_F(1.0) /
1280 (s[0]*c[5] - s[1]*c[4] + s[2]*c[3] + s[3]*c[2] - s[4]*c[1] + s[5]*c[0]);
1281
1282 bj_real* o = out->m;
1283
1284 o[BJ_M4(0,0)] = ( m[BJ_M4(1,1)] * c[5] - m[BJ_M4(1,2)] * c[4] + m[BJ_M4(1,3)] * c[3]) * id;
1285 o[BJ_M4(0,1)] = (-m[BJ_M4(0,1)] * c[5] + m[BJ_M4(0,2)] * c[4] - m[BJ_M4(0,3)] * c[3]) * id;
1286 o[BJ_M4(0,2)] = ( m[BJ_M4(3,1)] * s[5] - m[BJ_M4(3,2)] * s[4] + m[BJ_M4(3,3)] * s[3]) * id;
1287 o[BJ_M4(0,3)] = (-m[BJ_M4(2,1)] * s[5] + m[BJ_M4(2,2)] * s[4] - m[BJ_M4(2,3)] * s[3]) * id;
1288
1289 o[BJ_M4(1,0)] = (-m[BJ_M4(1,0)] * c[5] + m[BJ_M4(1,2)] * c[2] - m[BJ_M4(1,3)] * c[1]) * id;
1290 o[BJ_M4(1,1)] = ( m[BJ_M4(0,0)] * c[5] - m[BJ_M4(0,2)] * c[2] + m[BJ_M4(0,3)] * c[1]) * id;
1291 o[BJ_M4(1,2)] = (-m[BJ_M4(3,0)] * s[5] + m[BJ_M4(3,2)] * s[2] - m[BJ_M4(3,3)] * s[1]) * id;
1292 o[BJ_M4(1,3)] = ( m[BJ_M4(2,0)] * s[5] - m[BJ_M4(2,2)] * s[2] + m[BJ_M4(2,3)] * s[1]) * id;
1293
1294 o[BJ_M4(2,0)] = ( m[BJ_M4(1,0)] * c[4] - m[BJ_M4(1,1)] * c[2] + m[BJ_M4(1,3)] * c[0]) * id;
1295 o[BJ_M4(2,1)] = (-m[BJ_M4(0,0)] * c[4] + m[BJ_M4(0,1)] * c[2] - m[BJ_M4(0,3)] * c[0]) * id;
1296 o[BJ_M4(2,2)] = ( m[BJ_M4(3,0)] * s[4] - m[BJ_M4(3,1)] * s[2] + m[BJ_M4(3,3)] * s[0]) * id;
1297 o[BJ_M4(2,3)] = (-m[BJ_M4(2,0)] * s[4] + m[BJ_M4(2,1)] * s[2] - m[BJ_M4(2,3)] * s[0]) * id;
1298
1299 o[BJ_M4(3,0)] = (-m[BJ_M4(1,0)] * c[3] + m[BJ_M4(1,1)] * c[1] - m[BJ_M4(1,2)] * c[0]) * id;
1300 o[BJ_M4(3,1)] = ( m[BJ_M4(0,0)] * c[3] - m[BJ_M4(0,1)] * c[1] + m[BJ_M4(0,2)] * c[0]) * id;
1301 o[BJ_M4(3,2)] = (-m[BJ_M4(3,0)] * s[3] + m[BJ_M4(3,1)] * s[1] - m[BJ_M4(3,2)] * s[0]) * id;
1302 o[BJ_M4(3,3)] = ( m[BJ_M4(2,0)] * s[3] - m[BJ_M4(2,1)] * s[1] + m[BJ_M4(2,2)] * s[0]) * id;
1303}
1304
1312 bj_mat4* BJ_RESTRICT out,
1313 const bj_mat4* BJ_RESTRICT A
1314) {
1315 bj_mat4_copy(out, A);
1316 bj_vec3 z = {
1317 out->m[BJ_M4(2,0)],
1318 out->m[BJ_M4(2,1)],
1319 out->m[BJ_M4(2,2)]
1320 };
1321 z = bj_vec3_normalize(z);
1322
1323 bj_vec3 y = {
1324 out->m[BJ_M4(1,0)],
1325 out->m[BJ_M4(1,1)],
1326 out->m[BJ_M4(1,2)]
1327 };
1328 y = bj_vec3_sub(y, bj_vec3_scale(z, bj_vec3_dot(y,z)));
1329 y = bj_vec3_normalize(y);
1330
1331 bj_vec3 x = {
1332 out->m[BJ_M4(0,0)],
1333 out->m[BJ_M4(0,1)],
1334 out->m[BJ_M4(0,2)]
1335 };
1336 x = bj_vec3_sub(x, bj_vec3_scale(z, bj_vec3_dot(x,z)));
1337 x = bj_vec3_sub(x, bj_vec3_scale(y, bj_vec3_dot(x,y)));
1338 x = bj_vec3_normalize(x);
1339
1340 out->m[BJ_M4(0,0)] = x.x;
1341 out->m[BJ_M4(0,1)] = x.y;
1342 out->m[BJ_M4(0,2)] = x.z;
1343 out->m[BJ_M4(1,0)] = y.x;
1344 out->m[BJ_M4(1,1)] = y.y;
1345 out->m[BJ_M4(1,2)] = y.z;
1346 out->m[BJ_M4(2,0)] = z.x;
1347 out->m[BJ_M4(2,1)] = z.y;
1348 out->m[BJ_M4(2,2)] = z.z;
1349}
1350
1364 bj_real l,
1365 bj_real r,
1366 bj_real b,
1367 bj_real t,
1368 bj_real n,
1369 bj_real f
1370) {
1371
1372 bj_real* m = M->m;
1373 m[BJ_M4(0,0)] = BJ_F(2.0)*n/(r-l);
1374 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1375
1376 m[BJ_M4(1,0)] = BJ_FZERO;
1377 m[BJ_M4(1,1)] = BJ_F(-2.0)*n/(t-b);
1378 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1379
1380 m[BJ_M4(2,0)] = (r+l)/(r-l);
1381 m[BJ_M4(2,1)] = (t+b)/(t-b);
1382 m[BJ_M4(2,2)] = f/(f-n);
1383 m[BJ_M4(2,3)] = BJ_F(1.0);
1384
1385 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1386 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1387 m[BJ_M4(3,3)] = BJ_FZERO;
1388}
1389
1403 bj_real l,
1404 bj_real r,
1405 bj_real b,
1406 bj_real t,
1407 bj_real n,
1408 bj_real f
1409) {
1410
1411 bj_real* m = M->m;
1412 m[BJ_M4(0,0)] = BJ_F(2.0)/(r-l);
1413 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1414
1415 m[BJ_M4(1,0)] = m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1416 m[BJ_M4(1,1)] = BJ_F(-2.0)/(t-b);
1417
1418 m[BJ_M4(2,0)] = m[BJ_M4(2,1)] = m[BJ_M4(2,3)] = BJ_FZERO;
1419 m[BJ_M4(2,2)] = BJ_F(1.0)/(f-n);
1420
1421 m[BJ_M4(3,0)] = -(r+l)/(r-l);
1422 m[BJ_M4(3,1)] = (t+b)/(t-b);
1423 m[BJ_M4(3,2)] = -n/(f-n);
1424 m[BJ_M4(3,3)] = BJ_F(1.0);
1425}
1426
1427
1439 bj_real y_fov,
1440 bj_real aspect,
1441 bj_real n,
1442 bj_real f
1443) {
1444
1445 const bj_real a = BJ_F(1.0)/bj_tan(y_fov/BJ_F(2.0));
1446 bj_real* m = M->m;
1447 m[BJ_M4(0,0)] = a/aspect;
1448 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1449
1450 m[BJ_M4(1,0)] = BJ_FZERO;
1451 m[BJ_M4(1,1)] = -a;
1452 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1453
1454 m[BJ_M4(2,0)] = BJ_FZERO; m[BJ_M4(2,1)] = BJ_FZERO;
1455 m[BJ_M4(2,2)] = f/(f-n);
1456 m[BJ_M4(2,3)] = BJ_F(1.0);
1457
1458 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1459 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1460 m[BJ_M4(3,3)] = BJ_FZERO;
1461}
1462
1474 bj_real x,
1475 bj_real y,
1476 bj_real w,
1477 bj_real h
1478) {
1479 const bj_real zmin = BJ_FZERO;
1480 const bj_real zmax = BJ_F(1.0);
1481 bj_real* m = M->m;
1482 m[BJ_M4(0,0)] = BJ_F(0.5) * w;
1483 m[BJ_M4(0,1)] = BJ_FZERO;
1484 m[BJ_M4(0,2)] = BJ_FZERO;
1485 m[BJ_M4(0,3)] = BJ_FZERO;
1486 m[BJ_M4(1,0)] = BJ_FZERO;
1487 m[BJ_M4(1,1)] = BJ_F(0.5) * h;
1488 m[BJ_M4(1,2)] = BJ_FZERO;
1489 m[BJ_M4(1,3)] = BJ_FZERO;
1490 m[BJ_M4(2,0)] = BJ_FZERO;
1491 m[BJ_M4(2,1)] = BJ_FZERO;
1492 m[BJ_M4(2,2)] = (zmax - zmin);
1493 m[BJ_M4(2,3)] = BJ_FZERO;
1494 m[BJ_M4(3,0)] = x + BJ_F(0.5) * w;
1495 m[BJ_M4(3,1)] = y + BJ_F(0.5) * h;
1496 m[BJ_M4(3,2)] = zmin;
1497 m[BJ_M4(3,3)] = BJ_F(1.0);
1498}
1499
1510 bj_vec3 eye,
1511 bj_vec3 center,
1512 bj_vec3 up
1513) {
1514 bj_vec3 f = bj_vec3_normalize(bj_vec3_sub(center, eye));
1516 bj_vec3 t = bj_vec3_cross(f, s);
1517
1519 M->m[BJ_M4(0,0)] = s.x;
1520 M->m[BJ_M4(0,1)] = t.x;
1521 M->m[BJ_M4(0,2)] = f.x;
1522 M->m[BJ_M4(1,0)] = s.y;
1523 M->m[BJ_M4(1,1)] = t.y;
1524 M->m[BJ_M4(1,2)] = f.y;
1525 M->m[BJ_M4(2,0)] = s.z;
1526 M->m[BJ_M4(2,1)] = t.z;
1527 M->m[BJ_M4(2,2)] = f.z;
1528 bj_mat4_translate(M, -eye.x, -eye.y, -eye.z);
1529}
1530
1537) {
1538 bj_real* m = M->m;
1539 m[BJ_M43(0,0)] = BJ_F(1.0);
1540 m[BJ_M43(0,1)] = BJ_FZERO;
1541 m[BJ_M43(0,2)] = BJ_FZERO;
1542 m[BJ_M43(1,0)] = BJ_FZERO;
1543 m[BJ_M43(1,1)] = BJ_F(1.0);
1544 m[BJ_M43(1,2)] = BJ_FZERO;
1545 m[BJ_M43(2,0)] = BJ_FZERO;
1546 m[BJ_M43(2,1)] = BJ_FZERO;
1547 m[BJ_M43(2,2)] = BJ_F(1.0);
1548 m[BJ_M43(3,0)] = BJ_FZERO;
1549 m[BJ_M43(3,1)] = BJ_FZERO;
1550 m[BJ_M43(3,2)] = BJ_FZERO;
1551}
1552
1562 bj_real tx,
1563 bj_real ty,
1564 bj_real tz
1565) {
1567 M->m[BJ_M43(3,0)] = tx;
1568 M->m[BJ_M43(3,1)] = ty;
1569 M->m[BJ_M43(3,2)] = tz;
1570}
1571
1581 bj_real sx,
1582 bj_real sy,
1583 bj_real sz
1584) {
1586 M->m[BJ_M43(0,0)] = sx;
1587 M->m[BJ_M43(1,1)] = sy;
1588 M->m[BJ_M43(2,2)] = sz;
1589}
1590
1598 bj_real a
1599) {
1600 const bj_real c = bj_cos(a), s = bj_sin(a);
1602 M->m[BJ_M43(1,1)] = c; M->m[BJ_M43(1,2)] = s;
1603 M->m[BJ_M43(2,1)] = -s; M->m[BJ_M43(2,2)] = c;
1604}
1605
1613 bj_real a
1614) {
1615 const bj_real c = bj_cos(a), s = bj_sin(a);
1617 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,2)] = -s;
1618 M->m[BJ_M43(2,0)] = s; M->m[BJ_M43(2,2)] = c;
1619}
1620
1628 bj_real a
1629) {
1630 const bj_real c = bj_cos(a), s = bj_sin(a);
1632 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,1)] = s;
1633 M->m[BJ_M43(1,0)] = -s; M->m[BJ_M43(1,1)] = c;
1634}
1635
1645 const bj_mat4x3* BJ_RESTRICT A,
1646 const bj_mat4x3* BJ_RESTRICT B
1647){
1648
1649 const bj_real a00 = A->m[BJ_M43(0,0)];
1650 const bj_real a10 = A->m[BJ_M43(0,1)];
1651 const bj_real a20 = A->m[BJ_M43(0,2)];
1652 const bj_real a30 = A->m[BJ_M43(3,0)];
1653 const bj_real a01 = A->m[BJ_M43(1,0)];
1654 const bj_real a11 = A->m[BJ_M43(1,1)];
1655 const bj_real a21 = A->m[BJ_M43(1,2)];
1656 const bj_real a31 = A->m[BJ_M43(3,1)];
1657 const bj_real a02 = A->m[BJ_M43(2,0)];
1658 const bj_real a12 = A->m[BJ_M43(2,1)];
1659 const bj_real a22 = A->m[BJ_M43(2,2)];
1660 const bj_real a32 = A->m[BJ_M43(3,2)];
1661
1662 const bj_real b00 = B->m[BJ_M43(0,0)];
1663 const bj_real b01 = B->m[BJ_M43(1,0)];
1664 const bj_real bt0 = B->m[BJ_M43(3,0)];
1665 const bj_real b10 = B->m[BJ_M43(0,1)];
1666 const bj_real b11 = B->m[BJ_M43(1,1)];
1667 const bj_real bt1 = B->m[BJ_M43(3,1)];
1668 const bj_real b20 = B->m[BJ_M43(0,2)];
1669 const bj_real b21 = B->m[BJ_M43(1,2)];
1670 const bj_real b22 = B->m[BJ_M43(2,2)];
1671 const bj_real bt2 = B->m[BJ_M43(3,2)];
1672
1673 out->m[BJ_M43(0,0)] = a00 * b00 + a01 * b10 + a02 * b20;
1674 out->m[BJ_M43(0,1)] = a10 * b00 + a11 * b10 + a12 * b20;
1675 out->m[BJ_M43(0,2)] = a20 * b00 + a21 * b10 + a22 * b20;
1676
1677 out->m[BJ_M43(1,0)] = a00 * b01 + a01 * b11 + a02 * b21;
1678 out->m[BJ_M43(1,1)] = a10 * b01 + a11 * b11 + a12 * b21;
1679 out->m[BJ_M43(1,2)] = a20 * b01 + a21 * b11 + a22 * b21;
1680
1681 out->m[BJ_M43(2,0)] = a00 * b20 + a01 * b21 + a02 * b22;
1682 out->m[BJ_M43(2,1)] = a10 * b20 + a11 * b21 + a12 * b22;
1683 out->m[BJ_M43(2,2)] = a20 * b20 + a21 * b21 + a22 * b22;
1684
1685 out->m[BJ_M43(3,0)] = a00 * bt0 + a01 * bt1 + a02 * bt2 + a30;
1686 out->m[BJ_M43(3,1)] = a10 * bt0 + a11 * bt1 + a12 * bt2 + a31;
1687 out->m[BJ_M43(3,2)] = a20 * bt0 + a21 * bt1 + a22 * bt2 + a32;
1688}
1689
1698 const bj_mat4x3* BJ_RESTRICT M,
1699 bj_vec3 p
1700){
1701 const bj_real* m = M->m; const bj_real x = p.x,y = p.y,z = p.z;
1702 return (bj_vec3){
1703 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z + m[BJ_M43(3,0)],
1704 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z + m[BJ_M43(3,1)],
1705 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z + m[BJ_M43(3,2)]
1706 };
1707}
1708
1717 const bj_mat4x3* BJ_RESTRICT M,
1718 bj_vec3 v
1719){
1720 const bj_real* m = M->m; const bj_real x = v.x,y = v.y,z = v.z;
1721 return (bj_vec3){
1722 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z,
1723 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z,
1724 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z
1725 };
1726}
1727
1735 const bj_mat4x3* BJ_RESTRICT A
1736){
1737 const bj_real* a = A->m; bj_real* o = M->m;
1738 o[BJ_M4(0,0)] = a[BJ_M43(0,0)];
1739 o[BJ_M4(0,1)] = a[BJ_M43(0,1)];
1740 o[BJ_M4(0,2)] = a[BJ_M43(0,2)];
1741 o[BJ_M4(0,3)] = BJ_FZERO;
1742 o[BJ_M4(1,0)] = a[BJ_M43(1,0)];
1743 o[BJ_M4(1,1)] = a[BJ_M43(1,1)];
1744 o[BJ_M4(1,2)] = a[BJ_M43(1,2)];
1745 o[BJ_M4(1,3)] = BJ_FZERO;
1746 o[BJ_M4(2,0)] = a[BJ_M43(2,0)];
1747 o[BJ_M4(2,1)] = a[BJ_M43(2,1)];
1748 o[BJ_M4(2,2)] = a[BJ_M43(2,2)];
1749 o[BJ_M4(2,3)] = BJ_FZERO;
1750 o[BJ_M4(3,0)] = a[BJ_M43(3,0)];
1751 o[BJ_M4(3,1)] = a[BJ_M43(3,1)];
1752 o[BJ_M4(3,2)] = a[BJ_M43(3,2)];
1753 o[BJ_M4(3,3)] = BJ_F(1.0);
1754}
1755
1763 const bj_mat4* BJ_RESTRICT A
1764){
1765 const bj_real* a = A->m; bj_real* o = M->m;
1766 o[BJ_M43(0,0)] = a[BJ_M4(0,0)];
1767 o[BJ_M43(0,1)] = a[BJ_M4(0,1)];
1768 o[BJ_M43(0,2)] = a[BJ_M4(0,2)];
1769 o[BJ_M43(1,0)] = a[BJ_M4(1,0)];
1770 o[BJ_M43(1,1)] = a[BJ_M4(1,1)];
1771 o[BJ_M43(1,2)] = a[BJ_M4(1,2)];
1772 o[BJ_M43(2,0)] = a[BJ_M4(2,0)];
1773 o[BJ_M43(2,1)] = a[BJ_M4(2,1)];
1774 o[BJ_M43(2,2)] = a[BJ_M4(2,2)];
1775 o[BJ_M43(3,0)] = a[BJ_M4(3,0)];
1776 o[BJ_M43(3,1)] = a[BJ_M4(3,1)];
1777 o[BJ_M43(3,2)] = a[BJ_M4(3,2)];
1778}
1779
1780#endif
General-purpose definitions for Banjo API.
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:233
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:217
#define BJ_RESTRICT
BJ_RESTRICT expands to the appropriate restrict qualifier per toolchain.
Definition api.h:183
#define BJ_FALSE
Boolean false value (0).
Definition api.h:242
#define BJ_TRUE
Boolean true value (1).
Definition api.h:251
bj_real y
Definition vec.h:40
bj_real m[12]
Definition mat.h:53
bj_real z
Definition vec.h:54
bj_real m[16]
Definition mat.h:43
bj_real m[6]
Definition mat.h:35
bj_real x
Definition vec.h:52
bj_real y
Definition vec.h:28
bj_real y
Definition vec.h:53
bj_real x
Definition vec.h:39
bj_real z
Definition vec.h:41
bj_real x
Definition vec.h:27
bj_real m[9]
Definition mat.h:25
bj_real w
Definition vec.h:55
static void bj_mat3_from_mat3x2(bj_mat3 *restrict M, const bj_mat3x2 *restrict A)
Promote a 3×2 affine matrix to 3×3.
Definition mat.h:624
static void bj_mat3_invert_unsafe(bj_mat3 *restrict out, const bj_mat3 *restrict A)
Invert a 3×3 matrix (unsafe, adjugate).
Definition mat.h:400
static void bj_mat3_set_rotation_z(bj_mat3 *restrict M, bj_real angle)
Matrix operation.
Definition mat.h:316
struct bj_vec3_t bj_vec3
Definition vec.h:43
struct bj_mat3x3_t bj_mat3x3
Definition mat.h:26
#define BJ_M43(c, r)
Definition mat.h:55
static bj_vec3 bj_mat4x3_transform_dir(const bj_mat4x3 *restrict M, bj_vec3 v)
Transform a direction vector (ignoring translation).
Definition mat.h:1716
static bj_vec3 bj_vec3_sub(bj_vec3 lhs, bj_vec3 rhs)
Component-wise subtraction of two 3D vectors: res = lhs - rhs.
Definition vec.h:303
static bj_real bj_vec2_len(bj_vec2 v)
Euclidean length (L2 norm) of a 2D vector.
Definition vec.h:154
static bj_vec4 bj_mat4_col(const bj_mat4 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:725
static void bj_mat3_mul_scalar(bj_mat3 *restrict out, const bj_mat3 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:166
static void bj_mat4_add(bj_mat4 *restrict out, const bj_mat4 *restrict A, const bj_mat4 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:765
static void bj_mat3_set_shear_xy(bj_mat3 *restrict M, bj_real shx, bj_real shy)
Build an XY shear into a 3×3 matrix.
Definition mat.h:301
static void bj_mat3_set_translation(bj_mat3 *restrict M, bj_real tx, bj_real ty)
Build a 3×3 translation matrix.
Definition mat.h:250
static void bj_mat4x3_set_identity(bj_mat4x3 *restrict M)
Set a 4×3 affine matrix to identity.
Definition mat.h:1535
static bj_vec3 bj_mat3_row(const bj_mat3 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:88
static void bj_mat4_sub(bj_mat4 *restrict out, const bj_mat4 *restrict A, const bj_mat4 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:782
#define BJ_M32(c, r)
Definition mat.h:37
static void bj_mat4_set_lookat(bj_mat4 *restrict M, bj_vec3 eye, bj_vec3 center, bj_vec3 up)
Build a right-handed look-at view matrix.
Definition mat.h:1508
static void bj_mat3_add(bj_mat3 *restrict out, const bj_mat3 *restrict A, const bj_mat3 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:134
static void bj_mat4_set_outer_product(bj_mat4 *restrict out, bj_vec3 a, bj_vec3 b)
Add outer product r += s * v^T to a 4×4 matrix.
Definition mat.h:958
static void bj_mat4x3_set_translation(bj_mat4x3 *restrict M, bj_real tx, bj_real ty, bj_real tz)
Matrix operation.
Definition mat.h:1560
static bj_real bj_vec3_len(bj_vec3 v)
Euclidean length (L2 norm) of a 3D vector.
Definition vec.h:343
#define bj_tan
Tangent.
Definition math.h:223
struct bj_mat4x4_t bj_mat4
Definition mat.h:45
static bj_bool bj_mat3_invert(bj_mat3 *restrict out, const bj_mat3 *restrict A)
Invert a 3×3 matrix (safe, adjugate).
Definition mat.h:363
static void bj_mat3x2_set_scaling_xy(bj_mat3x2 *restrict M, bj_real sx, bj_real sy)
Scalar multiply: M = sx * k.
Definition mat.h:514
struct bj_mat4x3_t bj_mat4x3
Definition mat.h:54
static void bj_mat4_orthonormalize(bj_mat4 *restrict out, const bj_mat4 *restrict A)
Orthonormalize the 3×3 linear part of a 4×4 matrix.
Definition mat.h:1311
static void bj_mat4_rotate_y(bj_mat4 *restrict out, const bj_mat4 *restrict M, bj_real a)
Right-multiply by a Y-axis rotation.
Definition mat.h:1083
#define BJ_M3(c, r)
Definition mat.h:28
static void bj_mat4_scale_axes(bj_mat4 *restrict out, const bj_mat4 *restrict A, bj_real sx, bj_real sy, bj_real sz)
Scale basis vectors of a 4×4 matrix by per-axis factors.
Definition mat.h:818
static void bj_mat4_rotate_z(bj_mat4 *restrict out, const bj_mat4 *restrict M, bj_real a)
Right-multiply by a Z-axis rotation.
Definition mat.h:1116
static void bj_mat3_set_ortho(bj_mat3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
Build a 2D orthographic projection into a 3×3 matrix.
Definition mat.h:430
static void bj_mat4_rotate_x(bj_mat4 *restrict out, const bj_mat4 *restrict M, bj_real a)
Right-multiply by an X-axis rotation.
Definition mat.h:1049
struct bj_mat3x3_t bj_mat3
Definition mat.h:27
struct bj_vec2_t bj_vec2
Definition vec.h:30
static bj_vec4 bj_mat4_transform_vec4(const bj_mat4 *restrict M, bj_vec4 v)
Multiply a 4×4 matrix by a 4D vector: r = M * v.
Definition mat.h:895
struct bj_vec4_t bj_vec4
Definition vec.h:57
static bj_vec3 bj_mat4x3_transform_point(const bj_mat4x3 *restrict M, bj_vec3 p)
Transform a 3D point by a 4×3 affine matrix.
Definition mat.h:1697
#define bj_acos
Arc cosine.
Definition math.h:209
static void bj_mat3_transpose(bj_mat3 *restrict out, const bj_mat3 *restrict A)
Transpose a matrix.
Definition mat.h:111
static void bj_mat4x3_set_rotation_y(bj_mat4x3 *restrict M, bj_real a)
Right-multiply by a Y-axis rotation.
Definition mat.h:1611
static void bj_mat3x2_set_identity(bj_mat3x2 *restrict M)
Set a 3×2 affine matrix to identity.
Definition mat.h:480
static bj_vec2 bj_mat3_transform_point(const bj_mat3 *restrict M, bj_vec2 p)
Transform a 2D point by a 3×3 homogeneous transform.
Definition mat.h:228
static void bj_mat4_set_perspective(bj_mat4 *restrict M, bj_real y_fov, bj_real aspect, bj_real n, bj_real f)
Build a 4×4 perspective projection from vertical FOV.
Definition mat.h:1437
static bj_real bj_vec4_dot(bj_vec4 a, bj_vec4 b)
Dot product of two 4D vectors.
Definition vec.h:560
static void bj_mat4_set_frustum(bj_mat4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
Build a perspective frustum into a 4×4 matrix.
Definition mat.h:1362
static void bj_mat3x2_set_rotation_z(bj_mat3x2 *restrict M, bj_real angle)
Matrix operation.
Definition mat.h:529
#define bj_sin
Sine.
Definition math.h:221
static void bj_mat4_mul_scalar(bj_mat4 *restrict out, const bj_mat4 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:799
static void bj_mat3x2_set_translation(bj_mat3x2 *restrict M, bj_real tx, bj_real ty)
Matrix operation.
Definition mat.h:498
static void bj_mat3_set_identity(bj_mat3 *restrict M)
Set a 3×3 matrix to identity.
Definition mat.h:61
static void bj_mat4_set_translation(bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
Build a 4×4 translation matrix.
Definition mat.h:919
static void bj_mat3_mul(bj_mat3 *restrict out, const bj_mat3 *restrict A, const bj_mat3 *restrict B)
Matrix product: out = A * B.
Definition mat.h:183
static bj_vec2 bj_mat3x2_transform_point(const bj_mat3x2 *restrict M, bj_vec2 p)
Transform a 2D point by a 3×2 affine matrix.
Definition mat.h:586
static void bj_mat3x2_from_mat3(bj_mat3x2 *restrict M, const bj_mat3 *restrict A)
Demote a 3×3 matrix to 3×2 (drop projective terms).
Definition mat.h:646
static bj_vec3 bj_vec3_normalize(bj_vec3 v)
Normalize a 3D vector to unit length (safe).
Definition vec.h:397
static void bj_mat4_translate(bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
Right-multiply by a translation: M = M * T(tx,ty,tz).
Definition mat.h:939
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
static void bj_mat3_set_viewport(bj_mat3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 2D viewport transform into a 3×3 matrix.
Definition mat.h:457
static void bj_mat4_mul(bj_mat4 *restrict out, const bj_mat4 *restrict A, const bj_mat4 *restrict B)
Matrix product: out = A * B.
Definition mat.h:853
static void bj_mat4_set_ortho(bj_mat4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
Build a 3D orthographic projection into a 4×4 matrix.
Definition mat.h:1401
static bj_vec4 bj_mat4_row(const bj_mat4 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:706
static void bj_mat4_rotate_axis_andle(bj_mat4 *restrict out, const bj_mat4 *restrict M, bj_vec3 axis, bj_real angle)
Right-multiply a 4×4 by a rotation around an arbitrary axis.
Definition mat.h:989
static void bj_mat3_translate(bj_mat3 *restrict M, bj_real tx, bj_real ty)
Right-multiply by a translation: M = M * T(tx,ty).
Definition mat.h:267
static bj_vec2 bj_vec2_normalize(bj_vec2 v)
Normalize a 2D vector to unit length (safe).
Definition vec.h:208
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:441
static void bj_mat4_invert_unsafe(bj_mat4 *restrict out, const bj_mat4 *restrict M)
Invert a 4×4 matrix (unsafe).
Definition mat.h:1258
static bj_real bj_vec2_dot(bj_vec2 a, bj_vec2 b)
Dot product of two 2D vectors.
Definition vec.h:137
static bj_vec3 bj_vec3_cross(bj_vec3 l, bj_vec3 r)
3D cross product: res = l × r (right-hand rule).
Definition vec.h:453
struct bj_mat3x2_t bj_mat3x2
Definition mat.h:36
struct bj_mat4x4_t bj_mat4x4
Definition mat.h:44
static bj_vec2 bj_mat3x2_transform_dir(const bj_mat3x2 *restrict M, bj_vec2 v)
Transform a direction vector (ignoring translation).
Definition mat.h:606
static bj_vec3 bj_mat3_col(const bj_mat3 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:101
static bj_vec3 bj_mat3_transform_vec3(const bj_mat3 *restrict M, bj_vec3 v)
Multiply a 3×3 matrix by a 3D vector: r = M * v.
Definition mat.h:209
#define bj_cos
Cosine.
Definition math.h:212
static void bj_mat4_set_viewport(bj_mat4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 3D viewport transform into a 4×4 matrix.
Definition mat.h:1472
static bj_real bj_vec3_dot(bj_vec3 a, bj_vec3 b)
Dot product of two 3D vectors.
Definition vec.h:334
static void bj_mat4_copy(bj_mat4 *restrict dst, const bj_mat4 *restrict src)
Copy a matrix.
Definition mat.h:691
static void bj_mat4x3_mul(bj_mat4x3 *restrict out, const bj_mat4x3 *restrict A, const bj_mat4x3 *restrict B)
Matrix product: out = A * B.
Definition mat.h:1643
static void bj_mat4_from_mat4x3(bj_mat4 *restrict M, const bj_mat4x3 *restrict A)
Promote a 4×3 affine matrix to 4×4.
Definition mat.h:1733
static void bj_mat4_set_identity(bj_mat4 *restrict M)
Set a 4×4 matrix to identity.
Definition mat.h:664
static bj_real bj_mat3_determinant(const bj_mat3 *restrict A)
Determinant of a 3×3 matrix.
Definition mat.h:339
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
static bj_bool bj_mat4_invert(bj_mat4 *restrict out, const bj_mat4 *restrict M)
Invert a 4×4 matrix (safe).
Definition mat.h:1194
static void bj_mat4x3_set_scaling_xyz(bj_mat4x3 *restrict M, bj_real sx, bj_real sy, bj_real sz)
Scalar multiply: M = sx * k.
Definition mat.h:1579
static void bj_mat3x2_mul(bj_mat3x2 *restrict out, const bj_mat3x2 *restrict A, const bj_mat3x2 *restrict B)
Matrix product: out = A * B.
Definition mat.h:551
static void bj_mat3_set_scaling_xy(bj_mat3 *restrict M, bj_real sx, bj_real sy)
Apply non-uniform XY scale to a 3×3 matrix.
Definition mat.h:285
static void bj_mat4_rotate_arcball(bj_mat4 *restrict R, const bj_mat4 *restrict M, bj_vec2 a, bj_vec2 b, bj_real s)
Build a 4×4 rotation from two unit vectors (arcball).
Definition mat.h:1151
static void bj_mat3_sub(bj_mat3 *restrict out, const bj_mat3 *restrict A, const bj_mat3 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:150
#define bj_sqrt
Square root.
Definition math.h:222
float bj_real
Selected real type for float configuration.
Definition math.h:51
static void bj_mat4_transpose(bj_mat4 *restrict out, const bj_mat4 *restrict A)
Transpose a matrix.
Definition mat.h:743
#define BJ_M4(c, r)
Definition mat.h:46
static bj_vec3 bj_vec3_scale(bj_vec3 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:320
static void bj_mat4x3_set_rotation_z(bj_mat4x3 *restrict M, bj_real a)
Right-multiply by a Z-axis rotation.
Definition mat.h:1626
static void bj_mat3_copy(bj_mat3 *restrict dst, const bj_mat3 *restrict src)
Copy a matrix.
Definition mat.h:73
static void bj_mat4x3_from_mat4(bj_mat4x3 *restrict M, const bj_mat4 *restrict A)
Demote a 4×4 matrix to 4×3 (drop projective terms).
Definition mat.h:1761
static void bj_mat4x3_set_rotation_x(bj_mat4x3 *restrict M, bj_real a)
Right-multiply by an X-axis rotation.
Definition mat.h:1596
bj_mat3x2: 3×2 column-major matrix backed by bj_vec2.
Definition mat.h:35
Definition mat.h:25
bj_mat4x3: 4×3 column-major matrix backed by bj_vec3.
Definition mat.h:53
bj_mat4x4: 4×4 column-major matrix backed by bj_vec4.
Definition mat.h:43
C99 math shim with bj_real precision type and scalar utilities.
vector manipulation API