61#define BJ_M3(c,r) ((c)*3 + (r))
72#define BJ_M32(c,r) ((c)*2 + (r))
81#define BJ_M4(c,r) ((c)*4 + (r))
91#define BJ_M43(c,r) ((c)*3 + (r))
113 for (
int i = 0 ; i < 9 ; ++i) {
114 dst->m[i] = src->m[i];
159 for (
int i = 0 ; i<9 ; ++i) {
175 for (
int i = 0 ; i < 9 ; ++i) {
176 out->m[i] = A->m[i] + B->m[i];
191 for (
int i = 0 ; i < 9 ; ++i) {
192 out->m[i] = A->m[i] - B->m[i];
207 for (
int i = 0 ; i < 9 ; ++i) {
208 out->m[i] = A->m[i] * k;
228 for (
int c = 0; c < 3; ++c) {
275 return (
struct bj_vec2) { rx / w, ry / w };
277 return (
struct bj_vec2) { rx, ry };
292 M->m[
BJ_M3(2,0)] = tx;
293 M->m[
BJ_M3(2,1)] = ty;
309 for (
int r = 0 ; r<3 ; ++r) {
311 M->m[
BJ_M3(2,r)] += row.
x * t.
x + row.
y * t.
y + row.
z * t.
z;
332 M->m[
BJ_M3(0,0)] = sx;
333 M->m[
BJ_M3(1,1)] = sy;
351 M->m[
BJ_M3(1,0)] = shy;
352 M->m[
BJ_M3(0,1)] = shx;
427 out->m[
BJ_M3(0,0)] = c00 * id; out->m[
BJ_M3(1,0)] = c01 * id; out->m[
BJ_M3(2,0)] = c02 * id;
428 out->m[
BJ_M3(0,1)] = c10 * id; out->m[
BJ_M3(1,1)] = c11 * id; out->m[
BJ_M3(2,1)] = c12 * id;
429 out->m[
BJ_M3(0,2)] = c20 * id; out->m[
BJ_M3(1,2)] = c21 * id; out->m[
BJ_M3(2,2)] = c22 * id;
460 out->m[
BJ_M3(0,0)] = c00 * id; out->m[
BJ_M3(1,0)] = c01 * id; out->m[
BJ_M3(2,0)] = c02 * id;
461 out->m[
BJ_M3(0,1)] = c10 * id; out->m[
BJ_M3(1,1)] = c11 * id; out->m[
BJ_M3(2,1)] = c12 * id;
462 out->m[
BJ_M3(0,2)] = c20 * id; out->m[
BJ_M3(1,2)] = c21 * id; out->m[
BJ_M3(2,2)] = c22 * id;
493 m[
BJ_M3(2,0)] = -(r+l)/(r-l);
494 m[
BJ_M3(2,1)] = (t+b)/(t-b);
626 out->m[
BJ_M32(0,0)] = a00*b00 + a01*b10;
627 out->m[
BJ_M32(0,1)] = a10*b00 + a11*b10;
628 out->m[
BJ_M32(1,0)] = a00*b01 + a01*b11;
629 out->m[
BJ_M32(1,1)] = a10*b01 + a11*b11;
630 out->m[
BJ_M32(2,0)] = a00*b02 + a01*b12 + a02;
631 out->m[
BJ_M32(2,1)] = a10*b02 + a11*b12 + a12;
760 for(
int i = 0 ; i < 16 ; ++i) {
761 dst->m[i] = src->m[i];
814 for(
int c = 0 ; c < 4; ++c) {
815 for(
int r0 = 0 ; r0 < 4; ++r0) {
819 for(
int i = 0 ; i < 16 ; ++i) {
836 for(
int i = 0 ; i < 16 ; ++i) {
837 out->m[i] = A->m[i] + B->m[i];
853 for(
int i = 0 ; i < 16 ; ++i) {
854 out->m[i] = A->m[i] - B->m[i];
870 for(
int i = 0;i<16;++i) {
871 out->m[i] = A->m[i] * k;
928 for (
int c = 0; c < 4; ++c) {
937 + a[
BJ_M4(3,0)] * b3;
941 + a[
BJ_M4(3,1)] * b3;
945 + a[
BJ_M4(3,2)] * b3;
949 + a[
BJ_M4(3,3)] * b3;
994 M->m[
BJ_M4(3,0)] = x;
995 M->m[
BJ_M4(3,1)] = y;
996 M->m[
BJ_M4(3,2)] = z;
1016 for (
int r = 0 ; r<4 ; ++r) {
1079 if (len <=
BJ_F(1e-4)) {
1085 const bj_real ux = axis.
x * inv, uy = axis.
y * inv, uz = axis.
z * inv;
1089 const bj_real r00 = c + t*ux*ux;
1090 const bj_real r01 = t*ux*uy + s*uz;
1091 const bj_real r02 = t*ux*uz - s*uy;
1093 const bj_real r10 = t*uy*ux - s*uz;
1094 const bj_real r11 = c + t*uy*uy;
1095 const bj_real r12 = t*uy*uz + s*ux;
1097 const bj_real r20 = t*uz*ux + s*uy;
1098 const bj_real r21 = t*uz*uy - s*ux;
1099 const bj_real r22 = c + t*uz*uz;
1115 for (
int r = 0; r < 4; ++r) {
1116 const bj_real M0 = m0[r], M1 = m1[r], M2 = m2[r];
1117 o0[r] = M0*r00 + M1*r10 + M2*r20;
1118 o1[r] = M0*r01 + M1*r11 + M2*r21;
1119 o2[r] = M0*r02 + M1*r12 + M2*r22;
1150 for (
int r = 0; r < 4; ++r) {
1151 const bj_real M1 = m1[r], M2 = m2[r];
1153 o1[r] = c*M1 - s*M2;
1154 o2[r] = s*M1 + c*M2;
1184 for (
int r = 0; r < 4; ++r) {
1185 const bj_real M0 = m0[r], M2 = m2[r];
1186 o0[r] = c*M0 + s*M2;
1188 o2[r] = -s*M0 + c*M2;
1218 for (
int r = 0; r < 4; ++r) {
1219 const bj_real M0 = m0[r], M1 = m1[r];
1220 o0[r] = c*M0 - s*M1;
1221 o1[r] = s*M0 + c*M1;
1253 if (la <
BJ_F(1.0)) {
1258 if (lb <
BJ_F(1.0)) {
1263 const struct bj_vec3 A = { a.
x, a.
y, z_a };
1264 const struct bj_vec3 B = { b.
x, b.
y, z_b };
1306 const bj_real det = s[0]*c[5] - s[1]*c[4] + s[2]*c[3]
1307 + s[3]*c[2] - s[4]*c[1] + s[5]*c[0];
1371 (s[0]*c[5] - s[1]*c[4] + s[2]*c[3] + s[3]*c[2] - s[4]*c[1] + s[5]*c[0]);
1431 out->m[
BJ_M4(0,0)] =
x.x;
1432 out->m[
BJ_M4(0,1)] =
x.y;
1433 out->m[
BJ_M4(0,2)] =
x.z;
1434 out->m[
BJ_M4(1,0)] =
y.x;
1435 out->m[
BJ_M4(1,1)] =
y.y;
1436 out->m[
BJ_M4(1,2)] =
y.z;
1437 out->m[
BJ_M4(2,0)] =
z.x;
1438 out->m[
BJ_M4(2,1)] =
z.y;
1439 out->m[
BJ_M4(2,2)] =
z.z;
1476 m[
BJ_M4(2,0)] = (r+l)/(r-l);
1477 m[
BJ_M4(2,1)] = (t+b)/(t-b);
1478 m[
BJ_M4(2,2)] = f/(f-n);
1482 m[
BJ_M4(3,2)] = -(f*n)/(f-n);
1521 m[
BJ_M4(3,0)] = -(r+l)/(r-l);
1522 m[
BJ_M4(3,1)] = (t+b)/(t-b);
1523 m[
BJ_M4(3,2)] = -n/(f-n);
1547 m[
BJ_M4(0,0)] = a/aspect;
1555 m[
BJ_M4(2,2)] = f/(f-n);
1559 m[
BJ_M4(3,2)] = -(f*n)/(f-n);
1592 m[
BJ_M4(2,2)] = (zmax - zmin);
1596 m[
BJ_M4(3,2)] = zmin;
1794 out->m[
BJ_M43(0,0)] = a00 * b00 + a01 * b10 + a02 * b20;
1795 out->m[
BJ_M43(0,1)] = a10 * b00 + a11 * b10 + a12 * b20;
1796 out->m[
BJ_M43(0,2)] = a20 * b00 + a21 * b10 + a22 * b20;
1798 out->m[
BJ_M43(1,0)] = a00 * b01 + a01 * b11 + a02 * b21;
1799 out->m[
BJ_M43(1,1)] = a10 * b01 + a11 * b11 + a12 * b21;
1800 out->m[
BJ_M43(1,2)] = a20 * b01 + a21 * b11 + a22 * b21;
1802 out->m[
BJ_M43(2,0)] = a00 * b02 + a01 * b12 + a02 * b22;
1803 out->m[
BJ_M43(2,1)] = a10 * b02 + a11 * b12 + a12 * b22;
1804 out->m[
BJ_M43(2,2)] = a20 * b02 + a21 * b12 + a22 * b22;
1806 out->m[
BJ_M43(3,0)] = a00 * bt0 + a01 * bt1 + a02 * bt2 + a30;
1807 out->m[
BJ_M43(3,1)] = a10 * bt0 + a11 * bt1 + a12 * bt2 + a31;
1808 out->m[
BJ_M43(3,2)] = a20 * bt0 + a21 * bt1 + a22 * bt2 + a32;
General-purpose definitions for Banjo API.
uint32_t bj_bool
Boolean type used throughout the Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
#define BJ_RESTRICT
BJ_RESTRICT expands to the appropriate restrict qualifier per toolchain.
#define BJ_FALSE
Boolean false value (0).
#define BJ_TRUE
Boolean true value (1).
static void bj_mat3x2_set_scaling_xy(struct bj_mat3x2 *restrict M, bj_real sx, bj_real sy)
Build a 3×2 non-uniform XY scale.
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
static void bj_mat4_scale_axes(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real sx, bj_real sy, bj_real sz)
Scale basis vectors of a 4×4 matrix by per-axis factors.
#define BJ_M43(c, r)
Index a 4×3 matrix element at column c, row r. 0 <= c < 4, 0 <= r < 3.
static bj_bool bj_mat3_invert(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Invert a 3×3 matrix (safe, adjugate).
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
static bj_real bj_mat3_determinant(const struct bj_mat3x3 *restrict A)
Determinant of a 3×3 matrix.
static struct bj_vec2 bj_mat3x2_transform_point(const struct bj_mat3x2 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×2 affine matrix.
static void bj_mat3_set_identity(struct bj_mat3x3 *restrict M)
Set a 3×3 matrix to identity.
static void bj_mat4x3_set_rotation_y(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 Y-axis rotation.
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product: a.x*b.x + a.y*b.y.
static void bj_mat4x3_from_mat4(struct bj_mat4x3 *restrict M, const struct bj_mat4x4 *restrict A)
Demote a 4×4 matrix to 4×3 (drop projective terms).
#define BJ_M32(c, r)
Index a 3×2 matrix element at column c, row r. 0 <= c < 3, 0 <= r < 2.
static void bj_mat4_orthonormalize(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
Orthonormalize the 3×3 linear part of a 4×4 matrix.
static struct bj_vec4 bj_mat4_row(const struct bj_mat4x4 *restrict M, int r)
Extract a matrix row as a vector.
static void bj_mat3x2_from_mat3(struct bj_mat3x2 *restrict M, const struct bj_mat3x3 *restrict A)
Demote a 3×3 matrix to 3×2 (drop projective terms).
static void bj_mat3_set_shear_xy(struct bj_mat3x3 *restrict M, bj_real shx, bj_real shy)
Build an XY shear into a 3×3 matrix.
static struct bj_vec3 bj_mat3_row(const struct bj_mat3x3 *restrict M, int r)
Extract a matrix row as a vector.
static void bj_mat4_rotate_z(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by a Z-axis rotation: out = M * Rz(a).
static void bj_mat4_set_identity(struct bj_mat4x4 *restrict M)
Set a 4×4 matrix to identity.
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product over all four components.
static void bj_mat4_copy(struct bj_mat4x4 *restrict dst, const struct bj_mat4x4 *restrict src)
Copy a matrix.
static struct bj_vec3 bj_mat4x3_transform_dir(const struct bj_mat4x3 *restrict M, struct bj_vec3 v)
Transform a direction vector (ignoring translation).
static struct bj_vec3 bj_mat3_transform_vec3(const struct bj_mat3x3 *restrict M, struct bj_vec3 v)
Multiply a 3×3 matrix by a 3D vector: r = M * v.
static void bj_mat4_set_outer_product(struct bj_mat4x4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
Build the outer product a * b^T as a 4×4 matrix.
#define BJ_M3(c, r)
Index a 3×3 matrix element at column c, row r. 0 <= c,r < 3.
static void bj_mat4_rotate_x(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by an X-axis rotation: out = M * Rx(a).
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
static void bj_mat4_set_translation(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
Build a 4×4 translation matrix.
static void bj_mat4_mul_scalar(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real k)
Scalar multiply: out = A * k.
static void bj_mat4_set_viewport(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 3D viewport transform into a 4×4 matrix.
static void bj_mat4x3_set_rotation_x(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 X-axis rotation.
#define bj_acos
Arc cosine.
static void bj_mat3x2_mul(struct bj_mat3x2 *restrict out, const struct bj_mat3x2 *restrict A, const struct bj_mat3x2 *restrict B)
Matrix product: out = A * B.
static void bj_mat4_rotate_y(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
Right-multiply a 4×4 by a Y-axis rotation: out = M * Ry(a).
static void bj_mat4_translate(struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
Right-multiply by a translation: M = M * T(x, y, z).
static void bj_mat4_set_ortho(struct bj_mat4x4 *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.
static void bj_mat4x3_set_translation(struct bj_mat4x3 *restrict M, bj_real tx, bj_real ty, bj_real tz)
Build a 4×3 translation matrix.
static void bj_mat4x3_mul(struct bj_mat4x3 *restrict out, const struct bj_mat4x3 *restrict A, const struct bj_mat4x3 *restrict B)
Matrix product: out = A * B.
static void bj_mat3_set_ortho(struct bj_mat3x3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
Build a 2D orthographic projection into a 3×3 matrix.
static void bj_mat3_from_mat3x2(struct bj_mat3x3 *restrict M, const struct bj_mat3x2 *restrict A)
Promote a 3×2 affine matrix to 3×3.
static void bj_mat4_invert_unsafe(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
Invert a 4×4 matrix (unsafe).
static struct bj_vec3 bj_mat4x3_transform_point(const struct bj_mat4x3 *restrict M, struct bj_vec3 p)
Transform a 3D point by a 4×3 affine matrix.
static void bj_mat4x3_set_scaling_xyz(struct bj_mat4x3 *restrict M, bj_real sx, bj_real sy, bj_real sz)
Build a 4×3 non-uniform XYZ scale.
static void bj_mat4x3_set_rotation_z(struct bj_mat4x3 *restrict M, bj_real a)
Build a 4×3 Z-axis rotation.
static void bj_mat3x2_set_identity(struct bj_mat3x2 *restrict M)
Set a 3×2 affine matrix to identity.
#define BJ_FZERO
Zero constant in bj_real.
static void bj_mat3x2_set_rotation_z(struct bj_mat3x2 *restrict M, bj_real angle)
Build a 3×2 Z-axis (2D) rotation.
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.
static void bj_mat3x2_set_translation(struct bj_mat3x2 *restrict M, bj_real tx, bj_real ty)
Build a 3×2 translation matrix.
static void bj_mat3_set_scaling_xy(struct bj_mat3x3 *restrict M, bj_real sx, bj_real sy)
Build a non-uniform XY scale into a 3×3 matrix.
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
static void bj_mat4_from_mat4x3(struct bj_mat4x4 *restrict M, const struct bj_mat4x3 *restrict A)
Promote a 4×3 affine matrix to 4×4.
static struct bj_vec2 bj_mat3x2_transform_dir(const struct bj_mat3x2 *restrict M, struct bj_vec2 v)
Transform a direction vector by a 3×2 affine matrix (ignoring translation).
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
static void bj_mat4_add(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Component-wise addition: out = A + B.
static struct bj_vec4 bj_mat4_transform_vec4(const struct bj_mat4x4 *restrict M, struct bj_vec4 v)
Multiply a 4×4 matrix by a 4D vector: r = M * v.
static void bj_mat4_transpose(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
Transpose a matrix.
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: l × r (right-hand rule).
static struct bj_vec2 bj_mat3_transform_point(const struct bj_mat3x3 *restrict M, struct bj_vec2 p)
Transform a 2D point by a 3×3 homogeneous transform.
static void bj_mat3_invert_unsafe(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Invert a 3×3 matrix (unsafe, adjugate).
static void bj_mat4_rotate_arcball(struct bj_mat4x4 *restrict R, const struct bj_mat4x4 *restrict M, struct bj_vec2 a, struct bj_vec2 b, bj_real s)
Right-multiply by an arcball rotation between two screen points.
static void bj_mat3_mul_scalar(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, bj_real k)
Scalar multiply: out = A * k.
static struct bj_vec4 bj_mat4_col(const struct bj_mat4x4 *restrict M, int c)
Extract a matrix column as a vector.
static void bj_mat4_set_perspective(struct bj_mat4x4 *restrict M, bj_real y_fov, bj_real aspect, bj_real n, bj_real f)
Build a 4×4 perspective projection from vertical FOV.
static void bj_mat3_set_translation(struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
Build a 3×3 translation matrix.
static void bj_mat4_set_lookat(struct bj_mat4x4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
Build a right-handed look-at view matrix.
static void bj_mat3_translate(struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
Right-multiply by a translation: M = M * T(tx,ty).
static void bj_mat3_mul(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Matrix product: out = A * B.
static struct bj_vec3 bj_mat3_col(const struct bj_mat3x3 *restrict M, int c)
Extract a matrix column as a vector.
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
static void bj_mat3_set_rotation_z(struct bj_mat3x3 *restrict M, bj_real angle)
Build a Z-axis (i.e.
static void bj_mat4_sub(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Component-wise subtraction: out = A - B.
static void bj_mat3_copy(struct bj_mat3x3 *restrict dst, const struct bj_mat3x3 *restrict src)
Copy a matrix.
static void bj_mat4_set_frustum(struct bj_mat4x4 *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.
static void bj_mat3_sub(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Component-wise subtraction: out = A - B.
static void bj_mat3_transpose(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
Transpose a matrix.
#define bj_sqrt
Square root.
float bj_real
Selected real type for float configuration.
static bj_bool bj_mat4_invert(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
Invert a 4×4 matrix (safe).
#define BJ_M4(c, r)
Index a 4×4 matrix element at column c, row r. 0 <= c,r < 4.
static void bj_mat3_set_viewport(struct bj_mat3x3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
Build a 2D viewport transform into a 3×3 matrix.
static void bj_mat4x3_set_identity(struct bj_mat4x3 *restrict M)
Set a 4×3 affine matrix to identity.
static void bj_mat4_rotate_axis_angle(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, struct bj_vec3 axis, bj_real angle)
Right-multiply a 4×4 by a rotation around an arbitrary axis.
static void bj_mat4_mul(struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
Matrix product: out = A * B.
static void bj_mat3_add(struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
Component-wise addition: out = A + B.
3×2 column-major matrix (2D affine).
4×3 column-major matrix (3D affine).
2D vector of bj_real components.
3D vector of bj_real components.
4D vector of bj_real components.
C99 math shim with bj_real precision type and scalar utilities.
Fixed-size vector types (2D, 3D, 4D) and inline operations.