Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
mat.h File Reference
#include <banjo/api.h>
#include <banjo/math.h>
#include <banjo/vec.h>
Include dependency graph for mat.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  bj_mat3x3
struct  bj_mat3x2
struct  bj_mat4x4
struct  bj_mat4x3

Macros

#define BJ_M3(c, r)
#define BJ_M32(c, r)
#define BJ_M4(c, r)
#define BJ_M43(c, r)

Functions

static void bj_mat3_set_identity (struct bj_mat3x3 *restrict M)
static void bj_mat3_copy (struct bj_mat3x3 *restrict dst, const struct bj_mat3x3 *restrict src)
static struct bj_vec3 bj_mat3_row (const struct bj_mat3x3 *restrict M, int r)
static struct bj_vec3 bj_mat3_col (const struct bj_mat3x3 *restrict M, int c)
static void bj_mat3_transpose (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_add (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static void bj_mat3_sub (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static void bj_mat3_mul_scalar (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, bj_real k)
static void bj_mat3_mul (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static struct bj_vec3 bj_mat3_transform_vec3 (const struct bj_mat3x3 *restrict M, struct bj_vec3 v)
static struct bj_vec2 bj_mat3_transform_point (const struct bj_mat3x3 *restrict M, struct bj_vec2 p)
static void bj_mat3_set_translation (struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_translate (struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_set_scaling_xy (struct bj_mat3x3 *restrict M, bj_real sx, bj_real sy)
static void bj_mat3_set_shear_xy (struct bj_mat3x3 *restrict M, bj_real shx, bj_real shy)
static void bj_mat3_set_rotation_z (struct bj_mat3x3 *restrict M, bj_real angle)
static bj_real bj_mat3_determinant (const struct bj_mat3x3 *restrict A)
static bj_bool bj_mat3_invert (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_invert_unsafe (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_set_ortho (struct bj_mat3x3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
static void bj_mat3_set_viewport (struct bj_mat3x3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat3x2_set_identity (struct bj_mat3x2 *restrict M)
static void bj_mat3x2_set_translation (struct bj_mat3x2 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3x2_set_scaling_xy (struct bj_mat3x2 *restrict M, bj_real sx, bj_real sy)
static void bj_mat3x2_set_rotation_z (struct bj_mat3x2 *restrict M, bj_real angle)
static void bj_mat3x2_mul (struct bj_mat3x2 *restrict out, const struct bj_mat3x2 *restrict A, const struct bj_mat3x2 *restrict B)
static struct bj_vec2 bj_mat3x2_transform_point (const struct bj_mat3x2 *restrict M, struct bj_vec2 p)
static struct bj_vec2 bj_mat3x2_transform_dir (const struct bj_mat3x2 *restrict M, struct bj_vec2 v)
static void bj_mat3_from_mat3x2 (struct bj_mat3x3 *restrict M, const struct bj_mat3x2 *restrict A)
static void bj_mat3x2_from_mat3 (struct bj_mat3x2 *restrict M, const struct bj_mat3x3 *restrict A)
static void bj_mat4_set_identity (struct bj_mat4x4 *restrict M)
static void bj_mat4_copy (struct bj_mat4x4 *restrict dst, const struct bj_mat4x4 *restrict src)
static struct bj_vec4 bj_mat4_row (const struct bj_mat4x4 *restrict M, int r)
static struct bj_vec4 bj_mat4_col (const struct bj_mat4x4 *restrict M, int c)
static void bj_mat4_transpose (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
static void bj_mat4_add (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static void bj_mat4_sub (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static void bj_mat4_mul_scalar (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real k)
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)
static void bj_mat4_mul (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static struct bj_vec4 bj_mat4_transform_vec4 (const struct bj_mat4x4 *restrict M, struct bj_vec4 v)
static void bj_mat4_set_translation (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_translate (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_set_outer_product (struct bj_mat4x4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
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)
static void bj_mat4_rotate_x (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
static void bj_mat4_rotate_y (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
static void bj_mat4_rotate_z (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
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)
static bj_bool bj_mat4_invert (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
static void bj_mat4_invert_unsafe (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
static void bj_mat4_orthonormalize (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
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)
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)
static void bj_mat4_set_perspective (struct bj_mat4x4 *restrict M, bj_real y_fov, bj_real aspect, bj_real n, bj_real f)
static void bj_mat4_set_viewport (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat4_set_lookat (struct bj_mat4x4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
static void bj_mat4x3_set_identity (struct bj_mat4x3 *restrict M)
static void bj_mat4x3_set_translation (struct bj_mat4x3 *restrict M, bj_real tx, bj_real ty, bj_real tz)
static void bj_mat4x3_set_scaling_xyz (struct bj_mat4x3 *restrict M, bj_real sx, bj_real sy, bj_real sz)
static void bj_mat4x3_set_rotation_x (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_set_rotation_y (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_set_rotation_z (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_mul (struct bj_mat4x3 *restrict out, const struct bj_mat4x3 *restrict A, const struct bj_mat4x3 *restrict B)
static struct bj_vec3 bj_mat4x3_transform_point (const struct bj_mat4x3 *restrict M, struct bj_vec3 p)
static struct bj_vec3 bj_mat4x3_transform_dir (const struct bj_mat4x3 *restrict M, struct bj_vec3 v)
static void bj_mat4_from_mat4x3 (struct bj_mat4x4 *restrict M, const struct bj_mat4x3 *restrict A)
static void bj_mat4x3_from_mat4 (struct bj_mat4x3 *restrict M, const struct bj_mat4x4 *restrict A)

Detailed Description

Matrix types and operations for 2D and 3D transforms.

Four matrix shapes, all backed by a plain bj_real m[N] array:

Type Shape Typical use
bj_mat3x3 3×3 2D homogeneous (projective) transforms
bj_mat3x2 3×2 2D affine transforms (no projective term)
bj_mat4x4 4×4 3D homogeneous transforms, projection matrices
bj_mat4x3 4×3 3D affine transforms (no projective term)

Storage is column-major. Element (row r, column c) lives at m[col * height + row], accessed via the BJ_M3 / BJ_M32 / BJ_M4 / BJ_M43 index macros. This matches the convention used by OpenGL and most graphics math libraries, so matrices can be uploaded to a GPU verbatim if you ever wire one in.

Vectors are columns; multiply on the right. res = M * v means M.transform(v). Composition reads right-to-left: M = T * R * S applies S first, then R, then T.

What's here per matrix size: identity, copy, row/column extraction, transpose, component-wise arithmetic, scalar multiplication, matrix product, matrix-vector transform, translation and scaling constructors, axis rotations, inversion (safe and unsafe), and the common projection / viewport / look-at helpers.

Safe vs unsafe inversion. The *_invert functions check the determinant via bj_real_is_zero and return BJ_FALSE (leaving out unspecified) on singular input. The *_invert_unsafe variants skip the check; passing a singular matrix produces undefined results. Cofactor-based inversion is also numerically fragile for ill-conditioned matrices: prefer affine or orthonormal specialisations when the structure is known.

See also
vec.h for the vector types these matrices act on, quat.h for orientation as quaternions (with conversions to and from bj_mat4x4).

Definition in file mat.h.