Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
mat.h
Go to the documentation of this file.
1
61#ifndef BJ_MAT_H
62#define BJ_MAT_H
63
64#include <banjo/api.h>
65#include <banjo/math.h>
66#include <banjo/vec.h>
67
68
75struct bj_mat3 { bj_real m[9]; };
77#define BJ_M3(c,r) ((c)*3 + (r))
78
86struct bj_mat32 { bj_real m[6]; };
88#define BJ_M32(c,r) ((c)*2 + (r))
89
95struct bj_mat4 { bj_real m[16]; };
97#define BJ_M4(c,r) ((c)*4 + (r))
98
105struct bj_mat43 { bj_real m[12]; };
107#define BJ_M43(c,r) ((c)*3 + (r))
108
114 bj_real* m = M->m;
115 m[BJ_M3(0, 0)] = BJ_F(1.0); m[BJ_M3(0, 1)] = BJ_FZERO; m[BJ_M3(0, 2)] = BJ_FZERO;
116 m[BJ_M3(1, 0)] = BJ_FZERO; m[BJ_M3(1, 1)] = BJ_F(1.0); m[BJ_M3(1, 2)] = BJ_FZERO;
117 m[BJ_M3(2, 0)] = BJ_FZERO; m[BJ_M3(2, 1)] = BJ_FZERO; m[BJ_M3(2, 2)] = BJ_F(1.0);
118}
119
126 struct bj_mat3* BJ_RESTRICT dst,
127 const struct bj_mat3* BJ_RESTRICT src
128) {
129 for (int i = 0 ; i < 9 ; ++i) {
130 dst->m[i] = src->m[i];
131 }
132}
133
140static BJ_INLINE struct bj_vec3 bj_mat3_row(const struct bj_mat3* BJ_RESTRICT M, int r) {
141 const bj_real* m = M->m;
142 return (struct bj_vec3) {
143 m[BJ_M3(0, r)], m[BJ_M3(1, r)], m[BJ_M3(2, r)]
144 };
145}
146
153static BJ_INLINE struct bj_vec3 bj_mat3_col(const struct bj_mat3* BJ_RESTRICT M, int c) {
154 const bj_real* m = M->m;
155 return (struct bj_vec3) { m[BJ_M3(c, 0)], m[BJ_M3(c, 1)], m[BJ_M3(c, 2)] };
156}
157
166static BJ_INLINE void bj_mat3_transpose(struct bj_mat3* out, const struct bj_mat3* A) {
167 const bj_real* a = A->m;
168 bj_real r[9];
169 r[BJ_M3(0,0)] = a[BJ_M3(0,0)];
170 r[BJ_M3(0,1)] = a[BJ_M3(1,0)];
171 r[BJ_M3(0,2)] = a[BJ_M3(2,0)];
172 r[BJ_M3(1,0)] = a[BJ_M3(0,1)];
173 r[BJ_M3(1,1)] = a[BJ_M3(1,1)];
174 r[BJ_M3(1,2)] = a[BJ_M3(2,1)];
175 r[BJ_M3(2,0)] = a[BJ_M3(0,2)];
176 r[BJ_M3(2,1)] = a[BJ_M3(1,2)];
177 r[BJ_M3(2,2)] = a[BJ_M3(2,2)];
178 for (int i = 0 ; i<9 ; ++i) {
179 out->m[i] = r[i];
180 }
181}
182
190 struct bj_mat3* BJ_RESTRICT out,
191 const struct bj_mat3* BJ_RESTRICT A,
192 const struct bj_mat3* BJ_RESTRICT B
193) {
194 for (int i = 0 ; i < 9 ; ++i) {
195 out->m[i] = A->m[i] + B->m[i];
196 }
197}
198
206 struct bj_mat3* BJ_RESTRICT out,
207 const struct bj_mat3* BJ_RESTRICT A,
208 const struct bj_mat3* BJ_RESTRICT B
209) {
210 for (int i = 0 ; i < 9 ; ++i) {
211 out->m[i] = A->m[i] - B->m[i];
212 }
213}
214
222 struct bj_mat3* BJ_RESTRICT out,
223 const struct bj_mat3* BJ_RESTRICT A,
224 bj_real k
225) {
226 for (int i = 0 ; i < 9 ; ++i) {
227 out->m[i] = A->m[i] * k;
228 }
229}
230
239 struct bj_mat3* BJ_RESTRICT out,
240 const struct bj_mat3* BJ_RESTRICT A,
241 const struct bj_mat3* BJ_RESTRICT B
242) {
243 const bj_real* a = A->m;
244 const bj_real* b = B->m;
245 bj_real* o = out->m;
246
247 for (int c = 0; c < 3; ++c) {
248 const bj_real b0 = b[BJ_M3(c, 0)];
249 const bj_real b1 = b[BJ_M3(c, 1)];
250 const bj_real b2 = b[BJ_M3(c, 2)];
251 o[BJ_M3(c,0)] = a[BJ_M3(0,0)] * b0 + a[BJ_M3(1,0)] * b1 + a[BJ_M3(2,0)] * b2;
252 o[BJ_M3(c,1)] = a[BJ_M3(0,1)] * b0 + a[BJ_M3(1,1)] * b1 + a[BJ_M3(2,1)] * b2;
253 o[BJ_M3(c,2)] = a[BJ_M3(0,2)] * b0 + a[BJ_M3(1,2)] * b1 + a[BJ_M3(2,2)] * b2;
254 }
255}
256
265 const struct bj_mat3* BJ_RESTRICT M,
266 struct bj_vec3 v
267) {
268 const bj_real* m = M->m;
269 return (struct bj_vec3) {
270 m[BJ_M3(0,0)] * v.x + m[BJ_M3(1,0)] * v.y + m[BJ_M3(2,0)] * v.z,
271 m[BJ_M3(0,1)] * v.x + m[BJ_M3(1,1)] * v.y + m[BJ_M3(2,1)] * v.z,
272 m[BJ_M3(0,2)] * v.x + m[BJ_M3(1,2)] * v.y + m[BJ_M3(2,2)] * v.z
273 };
274}
275
284 const struct bj_mat3* BJ_RESTRICT M,
285 struct bj_vec2 p
286) {
287 const bj_real* m = M->m;
288 const bj_real x = p.x;
289 const bj_real y = p.y;
290 const bj_real w = m[BJ_M3(0,2)] * x + m[BJ_M3(1,2)] * y + m[BJ_M3(2,2)];
291 const bj_real rx = m[BJ_M3(0,0)] * x + m[BJ_M3(1,0)] * y + m[BJ_M3(2,0)];
292 const bj_real ry = m[BJ_M3(0,1)] * x + m[BJ_M3(1,1)] * y + m[BJ_M3(2,1)];
293 if (w != BJ_FZERO) {
294 return (struct bj_vec2) { rx / w, ry / w };
295 }
296 return (struct bj_vec2) { rx, ry };
297}
298
306 struct bj_mat3* BJ_RESTRICT M,
307 bj_real tx,
308 bj_real ty
309) {
311 M->m[BJ_M3(2,0)] = tx;
312 M->m[BJ_M3(2,1)] = ty;
313}
314
327 struct bj_mat3* out,
328 const struct bj_mat3* M,
329 bj_real tx,
330 bj_real ty
331) {
332 for (int r = 0 ; r < 3 ; ++r) {
333 const struct bj_vec3 row = bj_mat3_row(M, r); /* snapshot before any write */
334 out->m[BJ_M3(0,r)] = row.x;
335 out->m[BJ_M3(1,r)] = row.y;
336 out->m[BJ_M3(2,r)] = row.z + row.x * tx + row.y * ty;
337 }
338}
339
352 struct bj_mat3* BJ_RESTRICT M,
353 bj_real tx,
354 bj_real ty
355) {
356 bj_mat3_compose_translation(M, M, tx, ty);
357}
358
371 struct bj_mat3* BJ_RESTRICT M,
372 bj_real sx,
373 bj_real sy
374) {
376 M->m[BJ_M3(0,0)] = sx;
377 M->m[BJ_M3(1,1)] = sy;
378}
379
390 struct bj_mat3* BJ_RESTRICT M,
391 bj_real shx,
392 bj_real shy
393) {
395 M->m[BJ_M3(1,0)] = shy;
396 M->m[BJ_M3(0,1)] = shx;
397}
398
405 struct bj_mat3* BJ_RESTRICT M,
406 bj_real angle_rad
407) {
408 const bj_real s = bj_sin(angle_rad);
409 const bj_real c = bj_cos(angle_rad);
410 bj_real* m = M->m;
411 m[BJ_M3(0,0)] = c;
412 m[BJ_M3(0,1)] = s;
413 m[BJ_M3(0,2)] = BJ_FZERO;
414 m[BJ_M3(1,0)] = -s;
415 m[BJ_M3(1,1)] = c;
416 m[BJ_M3(1,2)] = BJ_FZERO;
417 m[BJ_M3(2,0)] = BJ_FZERO;
418 m[BJ_M3(2,1)] = BJ_FZERO;
419 m[BJ_M3(2,2)] = BJ_F(1.0);
420}
421
428 const struct bj_mat3* BJ_RESTRICT A
429) {
430 const bj_real* m = A->m;
431 return
432 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)])
433 - 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)])
434 + 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)]);
435}
436
452 struct bj_mat3* BJ_RESTRICT out,
453 const struct bj_mat3* BJ_RESTRICT A
454) {
455 const bj_real* m = A->m;
456 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
457 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
458 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
459 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
460 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
461 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
462 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
463 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
464 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
465 const bj_real det = m[BJ_M3(0,0)] * c00 + m[BJ_M3(1,0)] * c01 + m[BJ_M3(2,0)] * c02;
466
467 if (bj_real_is_zero(det)) {
468 return BJ_FALSE;
469 }
470 const bj_real id = BJ_F(1.0) / det;
471 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(0,1)] = c01 * id; out->m[BJ_M3(0,2)] = c02 * id;
472 out->m[BJ_M3(1,0)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(1,2)] = c12 * id;
473 out->m[BJ_M3(2,0)] = c20 * id; out->m[BJ_M3(2,1)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
474 return BJ_TRUE;
475}
476
489 struct bj_mat3* BJ_RESTRICT out,
490 const struct bj_mat3* BJ_RESTRICT A
491) {
492 const bj_real* m = A->m;
493 const bj_real c00 = (m[BJ_M3(1,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(1,2)]);
494 const bj_real c01 = -(m[BJ_M3(0,1)] * m[BJ_M3(2,2)] - m[BJ_M3(2,1)] * m[BJ_M3(0,2)]);
495 const bj_real c02 = (m[BJ_M3(0,1)] * m[BJ_M3(1,2)] - m[BJ_M3(1,1)] * m[BJ_M3(0,2)]);
496 const bj_real c10 = -(m[BJ_M3(1,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(1,2)]);
497 const bj_real c11 = (m[BJ_M3(0,0)] * m[BJ_M3(2,2)] - m[BJ_M3(2,0)] * m[BJ_M3(0,2)]);
498 const bj_real c12 = -(m[BJ_M3(0,0)] * m[BJ_M3(1,2)] - m[BJ_M3(1,0)] * m[BJ_M3(0,2)]);
499 const bj_real c20 = (m[BJ_M3(1,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(1,1)]);
500 const bj_real c21 = -(m[BJ_M3(0,0)] * m[BJ_M3(2,1)] - m[BJ_M3(2,0)] * m[BJ_M3(0,1)]);
501 const bj_real c22 = (m[BJ_M3(0,0)] * m[BJ_M3(1,1)] - m[BJ_M3(1,0)] * m[BJ_M3(0,1)]);
502 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);
503
504 out->m[BJ_M3(0,0)] = c00 * id; out->m[BJ_M3(0,1)] = c01 * id; out->m[BJ_M3(0,2)] = c02 * id;
505 out->m[BJ_M3(1,0)] = c10 * id; out->m[BJ_M3(1,1)] = c11 * id; out->m[BJ_M3(1,2)] = c12 * id;
506 out->m[BJ_M3(2,0)] = c20 * id; out->m[BJ_M3(2,1)] = c21 * id; out->m[BJ_M3(2,2)] = c22 * id;
507}
508
509
524 struct bj_mat3* BJ_RESTRICT M,
525 bj_real l, bj_real r,
526 bj_real b, bj_real t
527) {
528 bj_real* m = M->m;
529 m[BJ_M3(0,0)] = BJ_F(2.0)/(r-l);
530 m[BJ_M3(0,1)] = BJ_FZERO;
531 m[BJ_M3(0,2)] = BJ_FZERO;
532
533 m[BJ_M3(1,0)] = BJ_FZERO;
534 m[BJ_M3(1,1)] = BJ_F(-2.0)/(t-b);
535 m[BJ_M3(1,2)] = BJ_FZERO;
536
537 m[BJ_M3(2,0)] = -(r+l)/(r-l);
538 m[BJ_M3(2,1)] = (t+b)/(t-b);
539 m[BJ_M3(2,2)] = BJ_F(1.0);
540}
541
551 struct bj_mat3* BJ_RESTRICT M,
552 bj_real x,
553 bj_real y,
554 bj_real w,
555 bj_real h
556) {
557 bj_real* m = M->m;
558 m[BJ_M3(0,0)] = BJ_F(0.5) * w;
559 m[BJ_M3(0,1)] = BJ_FZERO;
560 m[BJ_M3(0,2)] = BJ_FZERO;
561 m[BJ_M3(1,0)] = BJ_FZERO;
562 m[BJ_M3(1,1)] = BJ_F(0.5) * h;
563 m[BJ_M3(1,2)] = BJ_FZERO;
564 m[BJ_M3(2,0)] = x + BJ_F(0.5) * w;
565 m[BJ_M3(2,1)] = y + BJ_F(0.5) * h;
566 m[BJ_M3(2,2)] = BJ_F(1.0);
567}
568
574 struct bj_mat32* BJ_RESTRICT M
575) {
576 bj_real* m = M->m;
577 m[BJ_M32(0,0)] = BJ_F(1.0);
578 m[BJ_M32(0,1)] = BJ_FZERO;
579 m[BJ_M32(1,0)] = BJ_FZERO;
580 m[BJ_M32(1,1)] = BJ_F(1.0);
581 m[BJ_M32(2,0)] = BJ_FZERO;
582 m[BJ_M32(2,1)] = BJ_FZERO;
583}
584
595 struct bj_mat32* BJ_RESTRICT M,
596 bj_real tx,
597 bj_real ty
598) {
600 M->m[BJ_M32(2,0)] = tx;
601 M->m[BJ_M32(2,1)] = ty;
602}
603
615 struct bj_mat32* BJ_RESTRICT M,
616 bj_real sx,
617 bj_real sy
618) {
620 M->m[BJ_M32(0,0)] = sx;
621 M->m[BJ_M32(1,1)] = sy;
622}
623
630 struct bj_mat32* BJ_RESTRICT M,
631 bj_real angle_rad
632) {
633 const bj_real c = bj_cos(angle_rad);
634 const bj_real s = bj_sin(angle_rad);
635 bj_real* m = M->m;
636 m[BJ_M32(0,0)] = c;
637 m[BJ_M32(0,1)] = s;
638 m[BJ_M32(1,0)] = -s;
639 m[BJ_M32(1,1)] = c;
640 m[BJ_M32(2,0)] = BJ_FZERO;
641 m[BJ_M32(2,1)] = BJ_FZERO;
642}
643
652 struct bj_mat32* BJ_RESTRICT out,
653 const struct bj_mat32* BJ_RESTRICT A,
654 const struct bj_mat32* BJ_RESTRICT B
655) {
656 const bj_real a00 = A->m[BJ_M32(0,0)];
657 const bj_real a10 = A->m[BJ_M32(0,1)];
658 const bj_real a01 = A->m[BJ_M32(1,0)];
659 const bj_real a11 = A->m[BJ_M32(1,1)];
660 const bj_real a02 = A->m[BJ_M32(2,0)];
661 const bj_real a12 = A->m[BJ_M32(2,1)];
662
663 const bj_real b00 = B->m[BJ_M32(0,0)];
664 const bj_real b10 = B->m[BJ_M32(0,1)];
665 const bj_real b01 = B->m[BJ_M32(1,0)];
666 const bj_real b11 = B->m[BJ_M32(1,1)];
667 const bj_real b02 = B->m[BJ_M32(2,0)];
668 const bj_real b12 = B->m[BJ_M32(2,1)];
669
670 out->m[BJ_M32(0,0)] = a00*b00 + a01*b10;
671 out->m[BJ_M32(0,1)] = a10*b00 + a11*b10;
672 out->m[BJ_M32(1,0)] = a00*b01 + a01*b11;
673 out->m[BJ_M32(1,1)] = a10*b01 + a11*b11;
674 out->m[BJ_M32(2,0)] = a00*b02 + a01*b12 + a02;
675 out->m[BJ_M32(2,1)] = a10*b02 + a11*b12 + a12;
676}
677
678
691 const struct bj_mat32* BJ_RESTRICT M,
692 struct bj_vec2 p
693) {
694 const bj_real* m = M->m;
695 const bj_real x = p.x;
696 const bj_real y = p.y;
697 return (struct bj_vec2) {
698 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y + m[BJ_M32(2,0)],
699 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y + m[BJ_M32(2,1)]
700 };
701}
702
716 const struct bj_mat32* BJ_RESTRICT M,
717 struct bj_vec2 v
718) {
719 const bj_real* m = M->m;
720 const bj_real x = v.x;
721 const bj_real y = v.y;
722 return (struct bj_vec2) {
723 m[BJ_M32(0,0)] * x + m[BJ_M32(1,0)] * y,
724 m[BJ_M32(0,1)] * x + m[BJ_M32(1,1)] * y
725 };
726}
727
734 struct bj_mat3* BJ_RESTRICT M,
735 const struct bj_mat32* BJ_RESTRICT A
736) {
737 bj_real* o = M->m;
738 const bj_real* a = A->m;
739 o[BJ_M3(0,0)] = a[BJ_M32(0,0)];
740 o[BJ_M3(0,1)] = a[BJ_M32(0,1)];
741 o[BJ_M3(0,2)] = BJ_FZERO;
742 o[BJ_M3(1,0)] = a[BJ_M32(1,0)];
743 o[BJ_M3(1,1)] = a[BJ_M32(1,1)];
744 o[BJ_M3(1,2)] = BJ_FZERO;
745 o[BJ_M3(2,0)] = a[BJ_M32(2,0)];
746 o[BJ_M3(2,1)] = a[BJ_M32(2,1)];
747 o[BJ_M3(2,2)] = BJ_F(1.0);
748}
749
756 struct bj_mat32* BJ_RESTRICT M,
757 const struct bj_mat3* BJ_RESTRICT A
758) {
759 const bj_real* a = A->m;
760 bj_real* o = M->m;
761 o[BJ_M32(0,0)] = a[BJ_M3(0,0)];
762 o[BJ_M32(0,1)] = a[BJ_M3(0,1)];
763 o[BJ_M32(1,0)] = a[BJ_M3(1,0)];
764 o[BJ_M32(1,1)] = a[BJ_M3(1,1)];
765 o[BJ_M32(2,0)] = a[BJ_M3(2,0)];
766 o[BJ_M32(2,1)] = a[BJ_M3(2,1)];
767}
768
774 struct bj_mat4* BJ_RESTRICT M
775) {
776 bj_real* m = M->m;
777 m[BJ_M4(0,0)] = BJ_F(1.0);
778 m[BJ_M4(0,1)] = BJ_FZERO;
779 m[BJ_M4(0,2)] = BJ_FZERO;
780 m[BJ_M4(0,3)] = BJ_FZERO;
781 m[BJ_M4(1,0)] = BJ_FZERO;
782 m[BJ_M4(1,1)] = BJ_F(1.0);
783 m[BJ_M4(1,2)] = BJ_FZERO;
784 m[BJ_M4(1,3)] = BJ_FZERO;
785 m[BJ_M4(2,0)] = BJ_FZERO;
786 m[BJ_M4(2,1)] = BJ_FZERO;
787 m[BJ_M4(2,2)] = BJ_F(1.0);
788 m[BJ_M4(2,3)] = BJ_FZERO;
789 m[BJ_M4(3,0)] = BJ_FZERO;
790 m[BJ_M4(3,1)] = BJ_FZERO;
791 m[BJ_M4(3,2)] = BJ_FZERO;
792 m[BJ_M4(3,3)] = BJ_F(1.0);
793}
794
801 struct bj_mat4* BJ_RESTRICT dst,
802 const struct bj_mat4* BJ_RESTRICT src
803) {
804 for(int i = 0 ; i < 16 ; ++i) {
805 dst->m[i] = src->m[i];
806 }
807}
808
816 const struct bj_mat4* BJ_RESTRICT M,
817 int r
818) {
819 const bj_real* m = M->m;
820 return (struct bj_vec4) {
821 m[BJ_M4(0,r)],
822 m[BJ_M4(1,r)],
823 m[BJ_M4(2,r)],
824 m[BJ_M4(3,r)]
825 };
826}
827
835 const struct bj_mat4* BJ_RESTRICT M,
836 int c
837) {
838 const bj_real* m = M->m;
839 return (struct bj_vec4) {
840 m[BJ_M4(c,0)],
841 m[BJ_M4(c,1)],
842 m[BJ_M4(c,2)],
843 m[BJ_M4(c,3)]
844 };
845}
846
856 struct bj_mat4* out,
857 const struct bj_mat4* A
858) {
859 const bj_real* a = A->m;
860 bj_real r[16];
861 for(int c = 0 ; c < 4; ++c) {
862 for(int r0 = 0 ; r0 < 4; ++r0) {
863 r[BJ_M4(r0,c)] = a[BJ_M4(c,r0)];
864 }
865 }
866 for(int i = 0 ; i < 16 ; ++i) {
867 out->m[i] = r[i];
868 }
869}
870
878 struct bj_mat4* BJ_RESTRICT out,
879 const struct bj_mat4* BJ_RESTRICT A,
880 const struct bj_mat4* BJ_RESTRICT B
881) {
882
883 for(int i = 0 ; i < 16 ; ++i) {
884 out->m[i] = A->m[i] + B->m[i];
885 }
886}
887
895 struct bj_mat4* BJ_RESTRICT out,
896 const struct bj_mat4* BJ_RESTRICT A,
897 const struct bj_mat4* BJ_RESTRICT B
898) {
899
900 for(int i = 0 ; i < 16 ; ++i) {
901 out->m[i] = A->m[i] - B->m[i];
902 }
903}
904
912 struct bj_mat4* BJ_RESTRICT out,
913 const struct bj_mat4* BJ_RESTRICT A,
914 bj_real k
915) {
916
917 for(int i = 0;i<16;++i) {
918 out->m[i] = A->m[i] * k;
919 }
920}
921
931 struct bj_mat4* BJ_RESTRICT out,
932 const struct bj_mat4* BJ_RESTRICT A,
933 bj_real sx,
934 bj_real sy,
935 bj_real sz
936) {
937
938 const bj_real* a = A->m;
939 bj_real* m = out->m;
940 m[BJ_M4(0,0)] = a[BJ_M4(0,0)] * sx;
941 m[BJ_M4(0,1)] = a[BJ_M4(0,1)] * sx;
942 m[BJ_M4(0,2)] = a[BJ_M4(0,2)] * sx;
943 m[BJ_M4(0,3)] = a[BJ_M4(0,3)] * sx;
944 m[BJ_M4(1,0)] = a[BJ_M4(1,0)] * sy;
945 m[BJ_M4(1,1)] = a[BJ_M4(1,1)] * sy;
946 m[BJ_M4(1,2)] = a[BJ_M4(1,2)] * sy;
947 m[BJ_M4(1,3)] = a[BJ_M4(1,3)] * sy;
948 m[BJ_M4(2,0)] = a[BJ_M4(2,0)] * sz;
949 m[BJ_M4(2,1)] = a[BJ_M4(2,1)] * sz;
950 m[BJ_M4(2,2)] = a[BJ_M4(2,2)] * sz;
951 m[BJ_M4(2,3)] = a[BJ_M4(2,3)] * sz;
952 m[BJ_M4(3,0)] = a[BJ_M4(3,0)];
953 m[BJ_M4(3,1)] = a[BJ_M4(3,1)];
954 m[BJ_M4(3,2)] = a[BJ_M4(3,2)];
955 m[BJ_M4(3,3)] = a[BJ_M4(3,3)];
956}
957
966 struct bj_mat4* BJ_RESTRICT out,
967 const struct bj_mat4* BJ_RESTRICT A,
968 const struct bj_mat4* BJ_RESTRICT B
969)
970{
971 const bj_real* a = A->m;
972 const bj_real* b = B->m;
973 bj_real* o = out->m;
974
975 for (int c = 0; c < 4; ++c) {
976 const bj_real b0 = b[BJ_M4(c,0)];
977 const bj_real b1 = b[BJ_M4(c,1)];
978 const bj_real b2 = b[BJ_M4(c,2)];
979 const bj_real b3 = b[BJ_M4(c,3)];
980
981 o[BJ_M4(c,0)] = a[BJ_M4(0,0)] * b0
982 + a[BJ_M4(1,0)] * b1
983 + a[BJ_M4(2,0)] * b2
984 + a[BJ_M4(3,0)] * b3;
985 o[BJ_M4(c,1)] = a[BJ_M4(0,1)] * b0
986 + a[BJ_M4(1,1)] * b1
987 + a[BJ_M4(2,1)] * b2
988 + a[BJ_M4(3,1)] * b3;
989 o[BJ_M4(c,2)] = a[BJ_M4(0,2)] * b0
990 + a[BJ_M4(1,2)] * b1
991 + a[BJ_M4(2,2)] * b2
992 + a[BJ_M4(3,2)] * b3;
993 o[BJ_M4(c,3)] = a[BJ_M4(0,3)] * b0
994 + a[BJ_M4(1,3)] * b1
995 + a[BJ_M4(2,3)] * b2
996 + a[BJ_M4(3,3)] * b3;
997 }
998}
999
1008 const struct bj_mat4* BJ_RESTRICT M,
1009 struct bj_vec4 v
1010) {
1011 const bj_real* m = M->m;
1012 return (struct bj_vec4) {
1013 m[BJ_M4(0,0)] * v.x + m[BJ_M4(1,0)] * v.y +
1014 m[BJ_M4(2,0)] * v.z + m[BJ_M4(3,0)] * v.w,
1015 m[BJ_M4(0,1)] * v.x + m[BJ_M4(1,1)] * v.y +
1016 m[BJ_M4(2,1)] * v.z + m[BJ_M4(3,1)] * v.w,
1017 m[BJ_M4(0,2)] * v.x + m[BJ_M4(1,2)] * v.y +
1018 m[BJ_M4(2,2)] * v.z + m[BJ_M4(3,2)] * v.w,
1019 m[BJ_M4(0,3)] * v.x + m[BJ_M4(1,3)] * v.y +
1020 m[BJ_M4(2,3)] * v.z + m[BJ_M4(3,3)] * v.w
1021 };
1022}
1023
1035 struct bj_mat4* BJ_RESTRICT M,
1036 bj_real x,
1037 bj_real y,
1038 bj_real z
1039) {
1041 M->m[BJ_M4(3,0)] = x;
1042 M->m[BJ_M4(3,1)] = y;
1043 M->m[BJ_M4(3,2)] = z;
1044}
1045
1059 struct bj_mat4* out,
1060 const struct bj_mat4* M,
1061 bj_real x,
1062 bj_real y,
1063 bj_real z
1064) {
1065 for (int r = 0 ; r < 4 ; ++r) {
1066 const struct bj_vec4 row = bj_mat4_row(M, r); /* snapshot before any write */
1067 out->m[BJ_M4(0,r)] = row.x;
1068 out->m[BJ_M4(1,r)] = row.y;
1069 out->m[BJ_M4(2,r)] = row.z;
1070 out->m[BJ_M4(3,r)] = row.w + row.x * x + row.y * y + row.z * z;
1071 }
1072}
1073
1087 struct bj_mat4* BJ_RESTRICT M,
1088 bj_real x,
1089 bj_real y,
1090 bj_real z
1091) {
1093}
1094
1108 struct bj_mat4* BJ_RESTRICT out,
1109 struct bj_vec3 a,
1110 struct bj_vec3 b
1111) {
1112 bj_real* m = out->m;
1113 m[BJ_M4(0,0)] = a.x*b.x;
1114 m[BJ_M4(1,0)] = a.y*b.x;
1115 m[BJ_M4(2,0)] = a.z*b.x;
1116 m[BJ_M4(3,0)] = BJ_FZERO;
1117 m[BJ_M4(0,1)] = a.x*b.y;
1118 m[BJ_M4(1,1)] = a.y*b.y;
1119 m[BJ_M4(2,1)] = a.z*b.y;
1120 m[BJ_M4(3,1)] = BJ_FZERO;
1121 m[BJ_M4(0,2)] = a.x*b.z;
1122 m[BJ_M4(1,2)] = a.y*b.z;
1123 m[BJ_M4(2,2)] = a.z*b.z;
1124 m[BJ_M4(3,2)] = BJ_FZERO;
1125 m[BJ_M4(0,3)] = BJ_FZERO;
1126 m[BJ_M4(1,3)] = BJ_FZERO;
1127 m[BJ_M4(2,3)] = BJ_FZERO;
1128 m[BJ_M4(3,3)] = BJ_FZERO;
1129}
1130
1145 struct bj_mat4* BJ_RESTRICT out,
1146 const struct bj_mat4* BJ_RESTRICT M,
1147 struct bj_vec3 axis,
1148 bj_real angle_rad
1149) {
1150 const bj_real s = bj_sin(angle_rad);
1151 const bj_real c = bj_cos(angle_rad);
1152 const bj_real len = bj_vec3_len(axis);
1153 if (len <= BJ_F(1e-4)) {
1154 bj_mat4_copy(out, M);
1155 return;
1156 }
1157
1158 const bj_real inv = BJ_F(1.0) / len;
1159 const bj_real ux = axis.x * inv, uy = axis.y * inv, uz = axis.z * inv;
1160 const bj_real t = BJ_F(1.0) - c;
1161
1162 /* 3x3 rotation block */
1163 const bj_real r00 = c + t*ux*ux;
1164 const bj_real r01 = t*ux*uy - s*uz;
1165 const bj_real r02 = t*ux*uz + s*uy;
1166
1167 const bj_real r10 = t*uy*ux + s*uz;
1168 const bj_real r11 = c + t*uy*uy;
1169 const bj_real r12 = t*uy*uz - s*ux;
1170
1171 const bj_real r20 = t*uz*ux - s*uy;
1172 const bj_real r21 = t*uz*uy + s*ux;
1173 const bj_real r22 = c + t*uz*uz;
1174
1175 const bj_real* m = M->m;
1176 bj_real* o = out->m;
1177
1178 /* Columns of M (column-major) */
1179 const bj_real* m0 = &m[BJ_M4(0,0)];
1180 const bj_real* m1 = &m[BJ_M4(1,0)];
1181 const bj_real* m2 = &m[BJ_M4(2,0)];
1182 const bj_real* m3 = &m[BJ_M4(3,0)];
1183
1184 bj_real* o0 = &o[BJ_M4(0,0)];
1185 bj_real* o1 = &o[BJ_M4(1,0)];
1186 bj_real* o2 = &o[BJ_M4(2,0)];
1187 bj_real* o3 = &o[BJ_M4(3,0)];
1188
1189 for (int r = 0; r < 4; ++r) {
1190 const bj_real M0 = m0[r], M1 = m1[r], M2 = m2[r];
1191 o0[r] = M0*r00 + M1*r10 + M2*r20;
1192 o1[r] = M0*r01 + M1*r11 + M2*r21;
1193 o2[r] = M0*r02 + M1*r12 + M2*r22;
1194 o3[r] = m3[r];
1195 }
1196}
1197
1206 struct bj_mat4* BJ_RESTRICT out,
1207 const struct bj_mat4* BJ_RESTRICT M,
1208 bj_real angle_rad
1209) {
1210
1211 const bj_real s = bj_sin(angle_rad), c = bj_cos(angle_rad);
1212 const bj_real* m = M->m; bj_real* o = out->m;
1213
1214 const bj_real* m0 = &m[BJ_M4(0,0)];
1215 const bj_real* m1 = &m[BJ_M4(1,0)];
1216 const bj_real* m2 = &m[BJ_M4(2,0)];
1217 const bj_real* m3 = &m[BJ_M4(3,0)];
1218
1219 bj_real* o0 = &o[BJ_M4(0,0)];
1220 bj_real* o1 = &o[BJ_M4(1,0)];
1221 bj_real* o2 = &o[BJ_M4(2,0)];
1222 bj_real* o3 = &o[BJ_M4(3,0)];
1223
1224 for (int r = 0; r < 4; ++r) {
1225 const bj_real M1 = m1[r], M2 = m2[r];
1226 o0[r] = m0[r];
1227 o1[r] = c*M1 + s*M2;
1228 o2[r] = -s*M1 + c*M2;
1229 o3[r] = m3[r];
1230 }
1231}
1232
1241 struct bj_mat4* BJ_RESTRICT out,
1242 const struct bj_mat4* BJ_RESTRICT M,
1243 bj_real angle_rad
1244) {
1245 const bj_real s = bj_sin(angle_rad), c = bj_cos(angle_rad);
1246 const bj_real* m = M->m; bj_real* o = out->m;
1247
1248 const bj_real* m0 = &m[BJ_M4(0,0)];
1249 const bj_real* m1 = &m[BJ_M4(1,0)];
1250 const bj_real* m2 = &m[BJ_M4(2,0)];
1251 const bj_real* m3 = &m[BJ_M4(3,0)];
1252
1253 bj_real* o0 = &o[BJ_M4(0,0)];
1254 bj_real* o1 = &o[BJ_M4(1,0)];
1255 bj_real* o2 = &o[BJ_M4(2,0)];
1256 bj_real* o3 = &o[BJ_M4(3,0)];
1257
1258 for (int r = 0; r < 4; ++r) {
1259 const bj_real M0 = m0[r], M2 = m2[r];
1260 o0[r] = c*M0 - s*M2;
1261 o1[r] = m1[r];
1262 o2[r] = s*M0 + c*M2;
1263 o3[r] = m3[r];
1264 }
1265}
1266
1275 struct bj_mat4* BJ_RESTRICT out,
1276 const struct bj_mat4* BJ_RESTRICT M,
1277 bj_real angle_rad
1278) {
1279 const bj_real s = bj_sin(angle_rad), c = bj_cos(angle_rad);
1280 const bj_real* m = M->m; bj_real* o = out->m;
1281
1282 const bj_real* m0 = &m[BJ_M4(0,0)];
1283 const bj_real* m1 = &m[BJ_M4(1,0)];
1284 const bj_real* m2 = &m[BJ_M4(2,0)];
1285 const bj_real* m3 = &m[BJ_M4(3,0)];
1286
1287 bj_real* o0 = &o[BJ_M4(0,0)];
1288 bj_real* o1 = &o[BJ_M4(1,0)];
1289 bj_real* o2 = &o[BJ_M4(2,0)];
1290 bj_real* o3 = &o[BJ_M4(3,0)];
1291
1292 for (int r = 0; r < 4; ++r) {
1293 const bj_real M0 = m0[r], M1 = m1[r];
1294 o0[r] = c*M0 + s*M1;
1295 o1[r] = -s*M0 + c*M1;
1296 o2[r] = m2[r];
1297 o3[r] = m3[r];
1298 }
1299}
1300
1317 struct bj_mat4* BJ_RESTRICT R,
1318 const struct bj_mat4* BJ_RESTRICT M,
1319 struct bj_vec2 a,
1320 struct bj_vec2 b,
1321 bj_real s
1322) {
1323 bj_real z_a = BJ_FZERO;
1324 bj_real z_b = BJ_FZERO;
1325 const bj_real la = bj_vec2_len(a);
1326 const bj_real lb = bj_vec2_len(b);
1327 if (la < BJ_F(1.0)) {
1328 z_a = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(a,a));
1329 } else {
1330 a = bj_vec2_normalize(a);
1331 }
1332 if (lb < BJ_F(1.0)) {
1333 z_b = bj_sqrt(BJ_F(1.0) - bj_vec2_dot(b,b));
1334 } else {
1335 b = bj_vec2_normalize(b);
1336 }
1337 const struct bj_vec3 A = { a.x, a.y, z_a };
1338 const struct bj_vec3 B = { b.x, b.y, z_b };
1339 const struct bj_vec3 C = bj_vec3_cross(A,B);
1340 // Rounding can take the dot product of the two unit vectors past 1,
1341 // where bj_acos has no value
1342 const bj_real ang = bj_acos(bj_clamp(bj_vec3_dot(A,B), BJ_F(-1.0), BJ_F(1.0))) * s;
1343 bj_mat4_rotate_axis_angle(R, M, C, ang);
1344}
1345
1362 struct bj_mat4* BJ_RESTRICT out,
1363 const struct bj_mat4* BJ_RESTRICT M
1364) {
1365 const bj_real* m = M->m;
1366 bj_real s[6], c[6];
1367
1368 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1369 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1370 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1371 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1372 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1373 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1374
1375 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1376 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1377 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1378 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1379 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1380 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1381
1382 const bj_real det = s[0]*c[5] - s[1]*c[4] + s[2]*c[3]
1383 + s[3]*c[2] - s[4]*c[1] + s[5]*c[0];
1384
1385 if (bj_real_is_zero(det)) {
1386 return BJ_FALSE;
1387 }
1388 const bj_real id = BJ_F(1.0) / det;
1389 bj_real* o = out->m;
1390
1391 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;
1392 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;
1393 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;
1394 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;
1395
1396 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;
1397 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;
1398 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;
1399 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;
1400
1401 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;
1402 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;
1403 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;
1404 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;
1405
1406 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;
1407 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;
1408 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;
1409 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;
1410
1411 return BJ_TRUE;
1412}
1413
1426 struct bj_mat4* BJ_RESTRICT out,
1427 const struct bj_mat4* BJ_RESTRICT M
1428) {
1429 const bj_real* m = M->m;
1430 bj_real s[6], c[6];
1431
1432 s[0] = m[BJ_M4(0,0)] * m[BJ_M4(1,1)] - m[BJ_M4(1,0)] * m[BJ_M4(0,1)];
1433 s[1] = m[BJ_M4(0,0)] * m[BJ_M4(1,2)] - m[BJ_M4(1,0)] * m[BJ_M4(0,2)];
1434 s[2] = m[BJ_M4(0,0)] * m[BJ_M4(1,3)] - m[BJ_M4(1,0)] * m[BJ_M4(0,3)];
1435 s[3] = m[BJ_M4(0,1)] * m[BJ_M4(1,2)] - m[BJ_M4(1,1)] * m[BJ_M4(0,2)];
1436 s[4] = m[BJ_M4(0,1)] * m[BJ_M4(1,3)] - m[BJ_M4(1,1)] * m[BJ_M4(0,3)];
1437 s[5] = m[BJ_M4(0,2)] * m[BJ_M4(1,3)] - m[BJ_M4(1,2)] * m[BJ_M4(0,3)];
1438
1439 c[0] = m[BJ_M4(2,0)] * m[BJ_M4(3,1)] - m[BJ_M4(3,0)] * m[BJ_M4(2,1)];
1440 c[1] = m[BJ_M4(2,0)] * m[BJ_M4(3,2)] - m[BJ_M4(3,0)] * m[BJ_M4(2,2)];
1441 c[2] = m[BJ_M4(2,0)] * m[BJ_M4(3,3)] - m[BJ_M4(3,0)] * m[BJ_M4(2,3)];
1442 c[3] = m[BJ_M4(2,1)] * m[BJ_M4(3,2)] - m[BJ_M4(3,1)] * m[BJ_M4(2,2)];
1443 c[4] = m[BJ_M4(2,1)] * m[BJ_M4(3,3)] - m[BJ_M4(3,1)] * m[BJ_M4(2,3)];
1444 c[5] = m[BJ_M4(2,2)] * m[BJ_M4(3,3)] - m[BJ_M4(3,2)] * m[BJ_M4(2,3)];
1445
1446 const bj_real id = BJ_F(1.0) /
1447 (s[0]*c[5] - s[1]*c[4] + s[2]*c[3] + s[3]*c[2] - s[4]*c[1] + s[5]*c[0]);
1448
1449 bj_real* o = out->m;
1450
1451 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;
1452 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;
1453 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;
1454 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;
1455
1456 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;
1457 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;
1458 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;
1459 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;
1460
1461 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;
1462 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;
1463 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;
1464 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;
1465
1466 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;
1467 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;
1468 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;
1469 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;
1470}
1471
1481 struct bj_mat4* BJ_RESTRICT out,
1482 const struct bj_mat4* BJ_RESTRICT A
1483) {
1484 bj_mat4_copy(out, A);
1485 struct bj_vec3 z = {
1486 out->m[BJ_M4(2,0)],
1487 out->m[BJ_M4(2,1)],
1488 out->m[BJ_M4(2,2)]
1489 };
1491
1492 struct bj_vec3 y = {
1493 out->m[BJ_M4(1,0)],
1494 out->m[BJ_M4(1,1)],
1495 out->m[BJ_M4(1,2)]
1496 };
1499
1500 struct bj_vec3 x = {
1501 out->m[BJ_M4(0,0)],
1502 out->m[BJ_M4(0,1)],
1503 out->m[BJ_M4(0,2)]
1504 };
1508
1509 out->m[BJ_M4(0,0)] = x.x;
1510 out->m[BJ_M4(0,1)] = x.y;
1511 out->m[BJ_M4(0,2)] = x.z;
1512 out->m[BJ_M4(1,0)] = y.x;
1513 out->m[BJ_M4(1,1)] = y.y;
1514 out->m[BJ_M4(1,2)] = y.z;
1515 out->m[BJ_M4(2,0)] = z.x;
1516 out->m[BJ_M4(2,1)] = z.y;
1517 out->m[BJ_M4(2,2)] = z.z;
1518}
1519
1537 struct bj_mat4* BJ_RESTRICT M,
1538 bj_real l,
1539 bj_real r,
1540 bj_real b,
1541 bj_real t,
1542 bj_real n,
1543 bj_real f
1544) {
1545
1546 bj_real* m = M->m;
1547 m[BJ_M4(0,0)] = BJ_F(2.0)*n/(r-l);
1548 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1549
1550 m[BJ_M4(1,0)] = BJ_FZERO;
1551 m[BJ_M4(1,1)] = BJ_F(-2.0)*n/(t-b);
1552 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1553
1554 m[BJ_M4(2,0)] = (r+l)/(r-l);
1555 m[BJ_M4(2,1)] = (t+b)/(t-b);
1556 m[BJ_M4(2,2)] = f/(f-n);
1557 m[BJ_M4(2,3)] = BJ_F(1.0);
1558
1559 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1560 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1561 m[BJ_M4(3,3)] = BJ_FZERO;
1562}
1563
1580 struct bj_mat4* BJ_RESTRICT M,
1581 bj_real l,
1582 bj_real r,
1583 bj_real b,
1584 bj_real t,
1585 bj_real n,
1586 bj_real f
1587) {
1588
1589 bj_real* m = M->m;
1590 m[BJ_M4(0,0)] = BJ_F(2.0)/(r-l);
1591 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1592
1593 m[BJ_M4(1,0)] = m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1594 m[BJ_M4(1,1)] = BJ_F(-2.0)/(t-b);
1595
1596 m[BJ_M4(2,0)] = m[BJ_M4(2,1)] = m[BJ_M4(2,3)] = BJ_FZERO;
1597 m[BJ_M4(2,2)] = BJ_F(1.0)/(f-n);
1598
1599 m[BJ_M4(3,0)] = -(r+l)/(r-l);
1600 m[BJ_M4(3,1)] = (t+b)/(t-b);
1601 m[BJ_M4(3,2)] = -n/(f-n);
1602 m[BJ_M4(3,3)] = BJ_F(1.0);
1603}
1604
1605
1616 struct bj_mat4* BJ_RESTRICT M,
1617 bj_real y_fov_rad,
1618 bj_real aspect,
1619 bj_real n,
1620 bj_real f
1621) {
1622
1623 const bj_real a = BJ_F(1.0)/bj_tan(y_fov_rad/BJ_F(2.0));
1624 bj_real* m = M->m;
1625 m[BJ_M4(0,0)] = a/aspect;
1626 m[BJ_M4(0,1)] = m[BJ_M4(0,2)] = m[BJ_M4(0,3)] = BJ_FZERO;
1627
1628 m[BJ_M4(1,0)] = BJ_FZERO;
1629 m[BJ_M4(1,1)] = -a;
1630 m[BJ_M4(1,2)] = m[BJ_M4(1,3)] = BJ_FZERO;
1631
1632 m[BJ_M4(2,0)] = BJ_FZERO; m[BJ_M4(2,1)] = BJ_FZERO;
1633 m[BJ_M4(2,2)] = f/(f-n);
1634 m[BJ_M4(2,3)] = BJ_F(1.0);
1635
1636 m[BJ_M4(3,0)] = BJ_FZERO; m[BJ_M4(3,1)] = BJ_FZERO;
1637 m[BJ_M4(3,2)] = -(f*n)/(f-n);
1638 m[BJ_M4(3,3)] = BJ_FZERO;
1639}
1640
1651 struct bj_mat4* BJ_RESTRICT M,
1652 bj_real x,
1653 bj_real y,
1654 bj_real w,
1655 bj_real h
1656) {
1657 const bj_real zmin = BJ_FZERO;
1658 const bj_real zmax = BJ_F(1.0);
1659 bj_real* m = M->m;
1660 m[BJ_M4(0,0)] = BJ_F(0.5) * w;
1661 m[BJ_M4(0,1)] = BJ_FZERO;
1662 m[BJ_M4(0,2)] = BJ_FZERO;
1663 m[BJ_M4(0,3)] = BJ_FZERO;
1664 m[BJ_M4(1,0)] = BJ_FZERO;
1665 m[BJ_M4(1,1)] = BJ_F(0.5) * h;
1666 m[BJ_M4(1,2)] = BJ_FZERO;
1667 m[BJ_M4(1,3)] = BJ_FZERO;
1668 m[BJ_M4(2,0)] = BJ_FZERO;
1669 m[BJ_M4(2,1)] = BJ_FZERO;
1670 m[BJ_M4(2,2)] = (zmax - zmin);
1671 m[BJ_M4(2,3)] = BJ_FZERO;
1672 m[BJ_M4(3,0)] = x + BJ_F(0.5) * w;
1673 m[BJ_M4(3,1)] = y + BJ_F(0.5) * h;
1674 m[BJ_M4(3,2)] = zmin;
1675 m[BJ_M4(3,3)] = BJ_F(1.0);
1676}
1677
1689 struct bj_mat4* BJ_RESTRICT M,
1690 struct bj_vec3 eye,
1691 struct bj_vec3 center,
1692 struct bj_vec3 up
1693) {
1694 struct bj_vec3 f = bj_vec3_normalize(bj_vec3_sub(center, eye));
1695 struct bj_vec3 s = bj_vec3_normalize(bj_vec3_cross(up, f));
1696 struct bj_vec3 t = bj_vec3_cross(f, s);
1697
1699 M->m[BJ_M4(0,0)] = s.x;
1700 M->m[BJ_M4(0,1)] = t.x;
1701 M->m[BJ_M4(0,2)] = f.x;
1702 M->m[BJ_M4(1,0)] = s.y;
1703 M->m[BJ_M4(1,1)] = t.y;
1704 M->m[BJ_M4(1,2)] = f.y;
1705 M->m[BJ_M4(2,0)] = s.z;
1706 M->m[BJ_M4(2,1)] = t.z;
1707 M->m[BJ_M4(2,2)] = f.z;
1708 bj_mat4_compose_translation(M, M, -eye.x, -eye.y, -eye.z);
1709}
1710
1716 struct bj_mat43* BJ_RESTRICT M
1717) {
1718 bj_real* m = M->m;
1719 m[BJ_M43(0,0)] = BJ_F(1.0);
1720 m[BJ_M43(0,1)] = BJ_FZERO;
1721 m[BJ_M43(0,2)] = BJ_FZERO;
1722 m[BJ_M43(1,0)] = BJ_FZERO;
1723 m[BJ_M43(1,1)] = BJ_F(1.0);
1724 m[BJ_M43(1,2)] = BJ_FZERO;
1725 m[BJ_M43(2,0)] = BJ_FZERO;
1726 m[BJ_M43(2,1)] = BJ_FZERO;
1727 m[BJ_M43(2,2)] = BJ_F(1.0);
1728 m[BJ_M43(3,0)] = BJ_FZERO;
1729 m[BJ_M43(3,1)] = BJ_FZERO;
1730 m[BJ_M43(3,2)] = BJ_FZERO;
1731}
1732
1744 struct bj_mat43* BJ_RESTRICT M,
1745 bj_real tx,
1746 bj_real ty,
1747 bj_real tz
1748) {
1750 M->m[BJ_M43(3,0)] = tx;
1751 M->m[BJ_M43(3,1)] = ty;
1752 M->m[BJ_M43(3,2)] = tz;
1753}
1754
1767 struct bj_mat43* BJ_RESTRICT M,
1768 bj_real sx,
1769 bj_real sy,
1770 bj_real sz
1771) {
1773 M->m[BJ_M43(0,0)] = sx;
1774 M->m[BJ_M43(1,1)] = sy;
1775 M->m[BJ_M43(2,2)] = sz;
1776}
1777
1788 struct bj_mat43* BJ_RESTRICT M,
1789 bj_real angle_rad
1790) {
1791 const bj_real c = bj_cos(angle_rad), s = bj_sin(angle_rad);
1793 M->m[BJ_M43(1,1)] = c; M->m[BJ_M43(1,2)] = s;
1794 M->m[BJ_M43(2,1)] = -s; M->m[BJ_M43(2,2)] = c;
1795}
1796
1807 struct bj_mat43* BJ_RESTRICT M,
1808 bj_real angle_rad
1809) {
1810 const bj_real c = bj_cos(angle_rad), s = bj_sin(angle_rad);
1812 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,2)] = -s;
1813 M->m[BJ_M43(2,0)] = s; M->m[BJ_M43(2,2)] = c;
1814}
1815
1826 struct bj_mat43* BJ_RESTRICT M,
1827 bj_real angle_rad
1828) {
1829 const bj_real c = bj_cos(angle_rad), s = bj_sin(angle_rad);
1831 M->m[BJ_M43(0,0)] = c; M->m[BJ_M43(0,1)] = s;
1832 M->m[BJ_M43(1,0)] = -s; M->m[BJ_M43(1,1)] = c;
1833}
1834
1843 struct bj_mat43* BJ_RESTRICT out,
1844 const struct bj_mat43* BJ_RESTRICT A,
1845 const struct bj_mat43* BJ_RESTRICT B
1846){
1847
1848 const bj_real a00 = A->m[BJ_M43(0,0)];
1849 const bj_real a10 = A->m[BJ_M43(0,1)];
1850 const bj_real a20 = A->m[BJ_M43(0,2)];
1851 const bj_real a30 = A->m[BJ_M43(3,0)];
1852 const bj_real a01 = A->m[BJ_M43(1,0)];
1853 const bj_real a11 = A->m[BJ_M43(1,1)];
1854 const bj_real a21 = A->m[BJ_M43(1,2)];
1855 const bj_real a31 = A->m[BJ_M43(3,1)];
1856 const bj_real a02 = A->m[BJ_M43(2,0)];
1857 const bj_real a12 = A->m[BJ_M43(2,1)];
1858 const bj_real a22 = A->m[BJ_M43(2,2)];
1859 const bj_real a32 = A->m[BJ_M43(3,2)];
1860
1861 const bj_real b00 = B->m[BJ_M43(0,0)];
1862 const bj_real b01 = B->m[BJ_M43(1,0)];
1863 const bj_real bt0 = B->m[BJ_M43(3,0)];
1864 const bj_real b10 = B->m[BJ_M43(0,1)];
1865 const bj_real b11 = B->m[BJ_M43(1,1)];
1866 const bj_real bt1 = B->m[BJ_M43(3,1)];
1867 const bj_real b20 = B->m[BJ_M43(0,2)];
1868 const bj_real b21 = B->m[BJ_M43(1,2)];
1869 const bj_real b22 = B->m[BJ_M43(2,2)];
1870 const bj_real b02 = B->m[BJ_M43(2,0)];
1871 const bj_real b12 = B->m[BJ_M43(2,1)];
1872 const bj_real bt2 = B->m[BJ_M43(3,2)];
1873
1874 out->m[BJ_M43(0,0)] = a00 * b00 + a01 * b10 + a02 * b20;
1875 out->m[BJ_M43(0,1)] = a10 * b00 + a11 * b10 + a12 * b20;
1876 out->m[BJ_M43(0,2)] = a20 * b00 + a21 * b10 + a22 * b20;
1877
1878 out->m[BJ_M43(1,0)] = a00 * b01 + a01 * b11 + a02 * b21;
1879 out->m[BJ_M43(1,1)] = a10 * b01 + a11 * b11 + a12 * b21;
1880 out->m[BJ_M43(1,2)] = a20 * b01 + a21 * b11 + a22 * b21;
1881
1882 out->m[BJ_M43(2,0)] = a00 * b02 + a01 * b12 + a02 * b22;
1883 out->m[BJ_M43(2,1)] = a10 * b02 + a11 * b12 + a12 * b22;
1884 out->m[BJ_M43(2,2)] = a20 * b02 + a21 * b12 + a22 * b22;
1885
1886 out->m[BJ_M43(3,0)] = a00 * bt0 + a01 * bt1 + a02 * bt2 + a30;
1887 out->m[BJ_M43(3,1)] = a10 * bt0 + a11 * bt1 + a12 * bt2 + a31;
1888 out->m[BJ_M43(3,2)] = a20 * bt0 + a21 * bt1 + a22 * bt2 + a32;
1889}
1890
1899 const struct bj_mat43* BJ_RESTRICT M,
1900 struct bj_vec3 p
1901){
1902 const bj_real* m = M->m; const bj_real x = p.x,y = p.y,z = p.z;
1903 return (struct bj_vec3){
1904 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z + m[BJ_M43(3,0)],
1905 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z + m[BJ_M43(3,1)],
1906 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z + m[BJ_M43(3,2)]
1907 };
1908}
1909
1918 const struct bj_mat43* BJ_RESTRICT M,
1919 struct bj_vec3 v
1920){
1921 const bj_real* m = M->m; const bj_real x = v.x,y = v.y,z = v.z;
1922 return (struct bj_vec3){
1923 m[BJ_M43(0,0)]*x + m[BJ_M43(1,0)]*y + m[BJ_M43(2,0)]*z,
1924 m[BJ_M43(0,1)]*x + m[BJ_M43(1,1)]*y + m[BJ_M43(2,1)]*z,
1925 m[BJ_M43(0,2)]*x + m[BJ_M43(1,2)]*y + m[BJ_M43(2,2)]*z
1926 };
1927}
1928
1935 struct bj_mat4* BJ_RESTRICT M,
1936 const struct bj_mat43* BJ_RESTRICT A
1937){
1938 const bj_real* a = A->m; bj_real* o = M->m;
1939 o[BJ_M4(0,0)] = a[BJ_M43(0,0)];
1940 o[BJ_M4(0,1)] = a[BJ_M43(0,1)];
1941 o[BJ_M4(0,2)] = a[BJ_M43(0,2)];
1942 o[BJ_M4(0,3)] = BJ_FZERO;
1943 o[BJ_M4(1,0)] = a[BJ_M43(1,0)];
1944 o[BJ_M4(1,1)] = a[BJ_M43(1,1)];
1945 o[BJ_M4(1,2)] = a[BJ_M43(1,2)];
1946 o[BJ_M4(1,3)] = BJ_FZERO;
1947 o[BJ_M4(2,0)] = a[BJ_M43(2,0)];
1948 o[BJ_M4(2,1)] = a[BJ_M43(2,1)];
1949 o[BJ_M4(2,2)] = a[BJ_M43(2,2)];
1950 o[BJ_M4(2,3)] = BJ_FZERO;
1951 o[BJ_M4(3,0)] = a[BJ_M43(3,0)];
1952 o[BJ_M4(3,1)] = a[BJ_M43(3,1)];
1953 o[BJ_M4(3,2)] = a[BJ_M43(3,2)];
1954 o[BJ_M4(3,3)] = BJ_F(1.0);
1955}
1956
1963 struct bj_mat43* BJ_RESTRICT M,
1964 const struct bj_mat4* BJ_RESTRICT A
1965){
1966 const bj_real* a = A->m; bj_real* o = M->m;
1967 o[BJ_M43(0,0)] = a[BJ_M4(0,0)];
1968 o[BJ_M43(0,1)] = a[BJ_M4(0,1)];
1969 o[BJ_M43(0,2)] = a[BJ_M4(0,2)];
1970 o[BJ_M43(1,0)] = a[BJ_M4(1,0)];
1971 o[BJ_M43(1,1)] = a[BJ_M4(1,1)];
1972 o[BJ_M43(1,2)] = a[BJ_M4(1,2)];
1973 o[BJ_M43(2,0)] = a[BJ_M4(2,0)];
1974 o[BJ_M43(2,1)] = a[BJ_M4(2,1)];
1975 o[BJ_M43(2,2)] = a[BJ_M4(2,2)];
1976 o[BJ_M43(3,0)] = a[BJ_M4(3,0)];
1977 o[BJ_M43(3,1)] = a[BJ_M4(3,1)];
1978 o[BJ_M43(3,2)] = a[BJ_M4(3,2)];
1979}
1980
1981// --- Deprecated function spellings (the "x" in the prefix). Removed in 2.0. ---
1982
1984#define bj_mat3x2_set_identity bj_mat32_set_identity
1986#define bj_mat3x2_set_translation bj_mat32_set_translation
1988#define bj_mat3x2_set_scaling_xy bj_mat32_set_scaling_xy
1990#define bj_mat3x2_set_rotation_z bj_mat32_set_rotation_z
1992#define bj_mat3x2_mul bj_mat32_mul
1994#define bj_mat3x2_transform_point bj_mat32_transform_point
1996#define bj_mat3x2_transform_dir bj_mat32_transform_dir
1998#define bj_mat3_from_mat3x2 bj_mat3_from_mat32
2000#define bj_mat3x2_from_mat3 bj_mat32_from_mat3
2001
2003#define bj_mat4x3_set_identity bj_mat43_set_identity
2005#define bj_mat4x3_set_translation bj_mat43_set_translation
2007#define bj_mat4x3_set_scaling_xyz bj_mat43_set_scaling_xyz
2009#define bj_mat4x3_set_rotation_x bj_mat43_set_rotation_x
2011#define bj_mat4x3_set_rotation_y bj_mat43_set_rotation_y
2013#define bj_mat4x3_set_rotation_z bj_mat43_set_rotation_z
2015#define bj_mat4x3_mul bj_mat43_mul
2017#define bj_mat4x3_transform_point bj_mat43_transform_point
2019#define bj_mat4x3_transform_dir bj_mat43_transform_dir
2021#define bj_mat4_from_mat4x3 bj_mat4_from_mat43
2023#define bj_mat4x3_from_mat4 bj_mat43_from_mat4
2024
2025#endif
General-purpose definitions for Banjo API.
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:264
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:248
#define BJ_RESTRICT
BJ_RESTRICT expands to the appropriate restrict qualifier per toolchain.
Definition api.h:214
#define BJ_FALSE
Boolean false value (0).
Definition api.h:273
#define BJ_TRUE
Boolean true value (1).
Definition api.h:282
bj_real z
Z component.
Definition vec.h:88
bj_real m[16]
Definition mat.h:95
bj_real m[9]
Definition mat.h:75
bj_real m[6]
Definition mat.h:86
bj_real m[12]
Definition mat.h:105
bj_real x
X component.
Definition vec.h:58
bj_real x
X component.
Definition vec.h:71
bj_real y
Y component.
Definition vec.h:59
bj_real w
W component (scalar part for quaternions).
Definition vec.h:89
bj_real z
Z component.
Definition vec.h:73
bj_real x
X component.
Definition vec.h:86
bj_real y
Y component.
Definition vec.h:72
bj_real y
Y component.
Definition vec.h:87
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
Definition vec.h:227
#define BJ_M43(c, r)
Index a 4×3 matrix element at column c, row r. 0 <= c < 4, 0 <= r < 3.
Definition mat.h:107
static void bj_mat4_orthonormalize(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A)
Orthonormalize the 3×3 linear part of a 4×4 matrix.
Definition mat.h:1480
static void bj_mat3_transpose(struct bj_mat3 *out, const struct bj_mat3 *A)
Transpose a matrix.
Definition mat.h:166
static void bj_mat43_from_mat4(struct bj_mat43 *restrict M, const struct bj_mat4 *restrict A)
Demote a 4×4 matrix to 4×3 (drop projective terms).
Definition mat.h:1962
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:421
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:403
static void bj_mat3_add(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:189
static bj_bool bj_mat4_invert(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M)
Invert a 4×4 matrix (safe).
Definition mat.h:1361
static void bj_mat43_set_scaling_xyz(struct bj_mat43 *restrict M, bj_real sx, bj_real sy, bj_real sz)
Build a 4×3 non-uniform XYZ scale.
Definition mat.h:1766
static void bj_mat32_set_translation(struct bj_mat32 *restrict M, bj_real tx, bj_real ty)
Build a 3×2 translation matrix.
Definition mat.h:594
static void bj_mat3_mul_scalar(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:221
static void bj_mat4_set_identity(struct bj_mat4 *restrict M)
Set a 4×4 matrix to identity.
Definition mat.h:773
static struct bj_vec3 bj_mat3_transform_vec3(const struct bj_mat3 *restrict M, struct bj_vec3 v)
Multiply a 3×3 matrix by a 3D vector: r = M * v.
Definition mat.h:264
static void bj_mat4_add(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
Component-wise addition: out = A + B.
Definition mat.h:877
static void bj_mat43_set_rotation_x(struct bj_mat43 *restrict M, bj_real angle_rad)
Build a 4×3 X-axis rotation.
Definition mat.h:1787
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product: a.x*b.x + a.y*b.y.
Definition vec.h:195
static struct bj_vec2 bj_mat32_transform_dir(const struct bj_mat32 *restrict M, struct bj_vec2 v)
Transform a direction vector by a 3×2 affine matrix (ignoring translation).
Definition mat.h:715
static void bj_mat4_set_ortho(struct 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:1579
static void bj_mat3_set_rotation_z(struct bj_mat3 *restrict M, bj_real angle_rad)
Build a Z-axis (i.e.
Definition mat.h:404
#define BJ_M32(c, r)
Index a 3×2 matrix element at column c, row r. 0 <= c < 3, 0 <= r < 2.
Definition mat.h:88
static void bj_mat4_from_mat43(struct bj_mat4 *restrict M, const struct bj_mat43 *restrict A)
Promote a 4×3 affine matrix to 4×4.
Definition mat.h:1934
static struct bj_vec3 bj_mat3_col(const struct bj_mat3 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:153
static void bj_mat4_set_perspective(struct bj_mat4 *restrict M, bj_real y_fov_rad, bj_real aspect, bj_real n, bj_real f)
Build a 4×4 perspective projection from vertical FOV.
Definition mat.h:1615
static void bj_mat4_mul(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
Matrix product: out = A * B.
Definition mat.h:965
static void bj_mat4_mul_scalar(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, bj_real k)
Scalar multiply: out = A * k.
Definition mat.h:911
static void bj_mat4_sub(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:894
static void bj_mat4_scale_axes(struct bj_mat4 *restrict out, const struct 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:930
static void bj_mat3_compose_translation(struct bj_mat3 *out, const struct bj_mat3 *M, bj_real tx, bj_real ty)
Right-multiply a 3×3 by a translation: out = M * T(tx, ty).
Definition mat.h:326
#define bj_tan
Tangent.
Definition math.h:252
static void bj_mat4_invert_unsafe(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M)
Invert a 4×4 matrix (unsafe).
Definition mat.h:1425
static void bj_mat3_set_viewport(struct 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:550
static struct bj_vec3 bj_mat43_transform_dir(const struct bj_mat43 *restrict M, struct bj_vec3 v)
Transform a direction vector (ignoring translation).
Definition mat.h:1917
#define BJ_M3(c, r)
Index a 3×3 matrix element at column c, row r. 0 <= c,r < 3.
Definition mat.h:77
static void bj_mat4_rotate_arcball(struct bj_mat4 *restrict R, const struct bj_mat4 *restrict M, struct bj_vec2 a, struct bj_vec2 b, bj_real s)
Right-multiply by an arcball rotation between two screen points.
Definition mat.h:1316
static void bj_mat3_set_ortho(struct 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:523
static void bj_mat4_copy(struct bj_mat4 *restrict dst, const struct bj_mat4 *restrict src)
Copy a matrix.
Definition mat.h:800
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
Definition vec.h:464
static void bj_mat3_translate(struct bj_mat3 *restrict M, bj_real tx, bj_real ty)
Right-multiply by a translation, in place: M = M * T(tx, ty).
Definition mat.h:351
static void bj_mat4_rotate_z(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
Right-multiply a 4×4 by a Z-axis rotation: out = M * Rz(angle_rad).
Definition mat.h:1274
static void bj_mat43_set_translation(struct bj_mat43 *restrict M, bj_real tx, bj_real ty, bj_real tz)
Build a 4×3 translation matrix.
Definition mat.h:1743
static void bj_mat3_sub(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
Component-wise subtraction: out = A - B.
Definition mat.h:205
static void bj_mat4_rotate_x(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
Right-multiply a 4×4 by an X-axis rotation: out = M * Rx(angle_rad).
Definition mat.h:1205
static void bj_mat43_set_rotation_z(struct bj_mat43 *restrict M, bj_real angle_rad)
Build a 4×3 Z-axis rotation.
Definition mat.h:1825
#define bj_acos
Arc cosine.
Definition math.h:238
static void bj_mat32_mul(struct bj_mat32 *restrict out, const struct bj_mat32 *restrict A, const struct bj_mat32 *restrict B)
Matrix product: out = A * B.
Definition mat.h:651
static void bj_mat32_set_identity(struct bj_mat32 *restrict M)
Set a 3×2 affine matrix to identity.
Definition mat.h:573
static struct bj_vec4 bj_mat4_row(const struct bj_mat4 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:815
#define bj_sin
Sine.
Definition math.h:250
static void bj_mat4_transpose(struct bj_mat4 *out, const struct bj_mat4 *A)
Transpose a matrix.
Definition mat.h:855
static struct bj_vec3 bj_mat43_transform_point(const struct bj_mat43 *restrict M, struct bj_vec3 p)
Transform a 3D point by a 4×3 affine matrix.
Definition mat.h:1898
static struct bj_vec2 bj_mat32_transform_point(const struct bj_mat32 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×2 affine matrix.
Definition mat.h:690
static bj_real bj_clamp(bj_real x, bj_real lo, bj_real hi)
Clamp x to the closed interval [lo, hi].
Definition math.h:270
static void bj_mat3_copy(struct bj_mat3 *restrict dst, const struct bj_mat3 *restrict src)
Copy a matrix.
Definition mat.h:125
static void bj_mat4_set_translation(struct bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
Build a 4×4 translation matrix.
Definition mat.h:1034
static struct bj_vec4 bj_mat4_col(const struct bj_mat4 *restrict M, int c)
Extract a matrix column as a vector.
Definition mat.h:834
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:93
static bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b)
Dot product: a.x*b.x + a.y*b.y + a.z*b.z.
Definition vec.h:454
static void bj_mat3_invert_unsafe(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A)
Invert a 3×3 matrix (unsafe, adjugate).
Definition mat.h:488
static void bj_mat43_set_identity(struct bj_mat43 *restrict M)
Set a 4×3 affine matrix to identity.
Definition mat.h:1715
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
Definition vec.h:295
static void bj_mat3_set_scaling_xy(struct bj_mat3 *restrict M, bj_real sx, bj_real sy)
Build a non-uniform XY scale into a 3×3 matrix.
Definition mat.h:370
static void bj_mat4_rotate_axis_angle(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, struct bj_vec3 axis, bj_real angle_rad)
Right-multiply a 4×4 by a rotation around an arbitrary axis.
Definition mat.h:1144
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:483
static void bj_mat43_mul(struct bj_mat43 *restrict out, const struct bj_mat43 *restrict A, const struct bj_mat43 *restrict B)
Matrix product: out = A * B.
Definition mat.h:1842
static void bj_mat32_from_mat3(struct bj_mat32 *restrict M, const struct bj_mat3 *restrict A)
Demote a 3×3 matrix to 3×2 (drop projective terms).
Definition mat.h:755
static void bj_mat3_set_identity(struct bj_mat3 *restrict M)
Set a 3×3 matrix to identity.
Definition mat.h:113
static void bj_mat43_set_rotation_y(struct bj_mat43 *restrict M, bj_real angle_rad)
Build a 4×3 Y-axis rotation.
Definition mat.h:1806
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: l × r (right-hand rule).
Definition vec.h:597
static struct bj_vec4 bj_mat4_transform_vec4(const struct bj_mat4 *restrict M, struct bj_vec4 v)
Multiply a 4×4 matrix by a 4D vector: r = M * v.
Definition mat.h:1007
static void bj_mat4_compose_translation(struct bj_mat4 *out, const struct bj_mat4 *M, bj_real x, bj_real y, bj_real z)
Right-multiply a 4×4 by a translation: out = M * T(x, y, z).
Definition mat.h:1058
static void bj_mat32_set_scaling_xy(struct bj_mat32 *restrict M, bj_real sx, bj_real sy)
Build a 3×2 non-uniform XY scale.
Definition mat.h:614
#define bj_cos
Cosine.
Definition math.h:241
static void bj_mat4_set_lookat(struct bj_mat4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
Build a left-handed look-at view matrix.
Definition mat.h:1688
static bj_bool bj_mat3_invert(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A)
Invert a 3×3 matrix (safe, adjugate).
Definition mat.h:451
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:82
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
Definition vec.h:531
static struct bj_vec3 bj_mat3_row(const struct bj_mat3 *restrict M, int r)
Extract a matrix row as a vector.
Definition mat.h:140
#define bj_sqrt
Square root.
Definition math.h:251
float bj_real
Selected real type for float configuration.
Definition math.h:80
static void bj_mat3_mul(struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
Matrix product: out = A * B.
Definition mat.h:238
static void bj_mat4_set_outer_product(struct bj_mat4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
Build the outer product b * a^T as a 4×4 matrix.
Definition mat.h:1107
static void bj_mat3_set_translation(struct bj_mat3 *restrict M, bj_real tx, bj_real ty)
Build a 3×3 translation matrix.
Definition mat.h:305
static void bj_mat3_set_shear_xy(struct bj_mat3 *restrict M, bj_real shx, bj_real shy)
Build an XY shear into a 3×3 matrix.
Definition mat.h:389
#define BJ_M4(c, r)
Index a 4×4 matrix element at column c, row r. 0 <= c,r < 4.
Definition mat.h:97
static void bj_mat32_set_rotation_z(struct bj_mat32 *restrict M, bj_real angle_rad)
Build a 3×2 Z-axis (2D) rotation.
Definition mat.h:629
static void bj_mat3_from_mat32(struct bj_mat3 *restrict M, const struct bj_mat32 *restrict A)
Promote a 3×2 affine matrix to 3×3.
Definition mat.h:733
static void bj_mat4_rotate_y(struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
Right-multiply a 4×4 by a Y-axis rotation: out = M * Ry(angle_rad).
Definition mat.h:1240
static void bj_mat4_translate(struct bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
Right-multiply by a translation, in place: M = M * T(x, y, z).
Definition mat.h:1086
static void bj_mat4_set_viewport(struct 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:1650
static void bj_mat4_set_frustum(struct 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 projection into a 4×4 matrix.
Definition mat.h:1536
static bj_real bj_mat3_determinant(const struct bj_mat3 *restrict A)
Determinant of a 3×3 matrix.
Definition mat.h:427
static struct bj_vec2 bj_mat3_transform_point(const struct bj_mat3 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×3 homogeneous transform.
Definition mat.h:283
3×3 column-major matrix.
Definition mat.h:75
3×2 column-major matrix (2D affine).
Definition mat.h:86
4×4 column-major matrix.
Definition mat.h:95
4×3 column-major matrix (3D affine).
Definition mat.h:105
2D vector of bj_real components.
Definition vec.h:57
3D vector of bj_real components.
Definition vec.h:70
4D vector of bj_real components.
Definition vec.h:85
C99 math shim with bj_real precision type and scalar utilities.
Fixed-size vector types (2D, 3D, 4D) and inline operations.