Banjo API 1.1.0-alpha
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_mat3
struct  bj_mat32
struct  bj_mat4
struct  bj_mat43

Macros

#define BJ_M3(c, r)
#define BJ_M32(c, r)
#define BJ_M4(c, r)
#define BJ_M43(c, r)
#define bj_mat3x2_set_identity   bj_mat32_set_identity
#define bj_mat3x2_set_translation   bj_mat32_set_translation
#define bj_mat3x2_set_scaling_xy   bj_mat32_set_scaling_xy
#define bj_mat3x2_set_rotation_z   bj_mat32_set_rotation_z
#define bj_mat3x2_mul   bj_mat32_mul
#define bj_mat3x2_transform_point   bj_mat32_transform_point
#define bj_mat3x2_transform_dir   bj_mat32_transform_dir
#define bj_mat3_from_mat3x2   bj_mat3_from_mat32
#define bj_mat3x2_from_mat3   bj_mat32_from_mat3
#define bj_mat4x3_set_identity   bj_mat43_set_identity
#define bj_mat4x3_set_translation   bj_mat43_set_translation
#define bj_mat4x3_set_scaling_xyz   bj_mat43_set_scaling_xyz
#define bj_mat4x3_set_rotation_x   bj_mat43_set_rotation_x
#define bj_mat4x3_set_rotation_y   bj_mat43_set_rotation_y
#define bj_mat4x3_set_rotation_z   bj_mat43_set_rotation_z
#define bj_mat4x3_mul   bj_mat43_mul
#define bj_mat4x3_transform_point   bj_mat43_transform_point
#define bj_mat4x3_transform_dir   bj_mat43_transform_dir
#define bj_mat4_from_mat4x3   bj_mat4_from_mat43
#define bj_mat4x3_from_mat4   bj_mat43_from_mat4

Functions

static void bj_mat3_set_identity (struct bj_mat3 *restrict M)
static void bj_mat3_copy (struct bj_mat3 *restrict dst, const struct bj_mat3 *restrict src)
static struct bj_vec3 bj_mat3_row (const struct bj_mat3 *restrict M, int r)
static struct bj_vec3 bj_mat3_col (const struct bj_mat3 *restrict M, int c)
static void bj_mat3_transpose (struct bj_mat3 *out, const struct bj_mat3 *A)
static void bj_mat3_add (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
static void bj_mat3_sub (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
static void bj_mat3_mul_scalar (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, bj_real k)
static void bj_mat3_mul (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A, const struct bj_mat3 *restrict B)
static struct bj_vec3 bj_mat3_transform_vec3 (const struct bj_mat3 *restrict M, struct bj_vec3 v)
static struct bj_vec2 bj_mat3_transform_point (const struct bj_mat3 *restrict M, struct bj_vec2 p)
static void bj_mat3_set_translation (struct bj_mat3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_compose_translation (struct bj_mat3 *out, const struct bj_mat3 *M, bj_real tx, bj_real ty)
static void bj_mat3_translate (struct bj_mat3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_set_scaling_xy (struct bj_mat3 *restrict M, bj_real sx, bj_real sy)
static void bj_mat3_set_shear_xy (struct bj_mat3 *restrict M, bj_real shx, bj_real shy)
static void bj_mat3_set_rotation_z (struct bj_mat3 *restrict M, bj_real angle_rad)
static bj_real bj_mat3_determinant (const struct bj_mat3 *restrict A)
static bj_bool bj_mat3_invert (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A)
static void bj_mat3_invert_unsafe (struct bj_mat3 *restrict out, const struct bj_mat3 *restrict A)
static void bj_mat3_set_ortho (struct bj_mat3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
static void bj_mat3_set_viewport (struct bj_mat3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat32_set_identity (struct bj_mat32 *restrict M)
static void bj_mat32_set_translation (struct bj_mat32 *restrict M, bj_real tx, bj_real ty)
static void bj_mat32_set_scaling_xy (struct bj_mat32 *restrict M, bj_real sx, bj_real sy)
static void bj_mat32_set_rotation_z (struct bj_mat32 *restrict M, bj_real angle_rad)
static void bj_mat32_mul (struct bj_mat32 *restrict out, const struct bj_mat32 *restrict A, const struct bj_mat32 *restrict B)
static struct bj_vec2 bj_mat32_transform_point (const struct bj_mat32 *restrict M, struct bj_vec2 p)
static struct bj_vec2 bj_mat32_transform_dir (const struct bj_mat32 *restrict M, struct bj_vec2 v)
static void bj_mat3_from_mat32 (struct bj_mat3 *restrict M, const struct bj_mat32 *restrict A)
static void bj_mat32_from_mat3 (struct bj_mat32 *restrict M, const struct bj_mat3 *restrict A)
static void bj_mat4_set_identity (struct bj_mat4 *restrict M)
static void bj_mat4_copy (struct bj_mat4 *restrict dst, const struct bj_mat4 *restrict src)
static struct bj_vec4 bj_mat4_row (const struct bj_mat4 *restrict M, int r)
static struct bj_vec4 bj_mat4_col (const struct bj_mat4 *restrict M, int c)
static void bj_mat4_transpose (struct bj_mat4 *out, const struct bj_mat4 *A)
static void bj_mat4_add (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
static void bj_mat4_sub (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
static void bj_mat4_mul_scalar (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, bj_real k)
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)
static void bj_mat4_mul (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A, const struct bj_mat4 *restrict B)
static struct bj_vec4 bj_mat4_transform_vec4 (const struct bj_mat4 *restrict M, struct bj_vec4 v)
static void bj_mat4_set_translation (struct bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_compose_translation (struct bj_mat4 *out, const struct bj_mat4 *M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_translate (struct bj_mat4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_set_outer_product (struct bj_mat4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
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)
static void bj_mat4_rotate_x (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
static void bj_mat4_rotate_y (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
static void bj_mat4_rotate_z (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M, bj_real angle_rad)
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)
static bj_bool bj_mat4_invert (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M)
static void bj_mat4_invert_unsafe (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict M)
static void bj_mat4_orthonormalize (struct bj_mat4 *restrict out, const struct bj_mat4 *restrict A)
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)
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)
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)
static void bj_mat4_set_viewport (struct bj_mat4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat4_set_lookat (struct bj_mat4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
static void bj_mat43_set_identity (struct bj_mat43 *restrict M)
static void bj_mat43_set_translation (struct bj_mat43 *restrict M, bj_real tx, bj_real ty, bj_real tz)
static void bj_mat43_set_scaling_xyz (struct bj_mat43 *restrict M, bj_real sx, bj_real sy, bj_real sz)
static void bj_mat43_set_rotation_x (struct bj_mat43 *restrict M, bj_real angle_rad)
static void bj_mat43_set_rotation_y (struct bj_mat43 *restrict M, bj_real angle_rad)
static void bj_mat43_set_rotation_z (struct bj_mat43 *restrict M, bj_real angle_rad)
static void bj_mat43_mul (struct bj_mat43 *restrict out, const struct bj_mat43 *restrict A, const struct bj_mat43 *restrict B)
static struct bj_vec3 bj_mat43_transform_point (const struct bj_mat43 *restrict M, struct bj_vec3 p)
static struct bj_vec3 bj_mat43_transform_dir (const struct bj_mat43 *restrict M, struct bj_vec3 v)
static void bj_mat4_from_mat43 (struct bj_mat4 *restrict M, const struct bj_mat43 *restrict A)
static void bj_mat43_from_mat4 (struct bj_mat43 *restrict M, const struct bj_mat4 *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_mat3 3×3 2D homogeneous (projective) transforms
bj_mat32 3×2 2D affine transforms (no projective term)
bj_mat4 4×4 3D homogeneous transforms, projection matrices
bj_mat43 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.

Rotations follow the right-hand rule. A positive angle turns counter-clockwise when looking from the positive axis toward the origin, so a quarter turn about Z takes +X to +Y, as bj_quat_to_mat4 does.

bj_mat3 and bj_mat4 have identity, copy, row/column extraction, transpose, component-wise arithmetic, scalar multiplication, matrix product, matrix-vector transform, translation, scaling, rotation, inversion (safe and unsafe), and orthographic projection and viewport helpers. bj_mat3 adds shear and the determinant; bj_mat4 adds frustum, perspective, look-at, arcball, outer product and orthonormalisation. The affine bj_mat32 and bj_mat43 have identity, translation, scaling, rotation, product, point and direction transforms, and conversion to and from their full shape.

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.

Pointers are not checked. These header-only functions sit in the inner loops of a program, so they read and write through their matrix and vector pointers without testing them: every pointer must point to an object, and NULL is undefined behaviour. This is the one exception to the rule that a NULL argument does no harm.

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

Definition in file mat.h.