19 FEB 2026

rahulmnavneeth

math module

homedocs

Location

src/math/math.c          — Implementation
include/mop/types.h      — Type definitions and function declarations

Matrix Convention

All matrices are column-major, matching OpenGL convention.

MopMat4.d[col * 4 + row]

Column 0: d[0..3]
Column 1: d[4..7]
Column 2: d[8..11]
Column 3: d[12..15]

The indexing macro used internally:

#define M(mat, row, col) ((mat).d[(col) * 4 + (row)])

Vector Operations

FunctionSignatureDescription
mop_vec3_add(Vec3, Vec3) → Vec3Component-wise add
mop_vec3_sub(Vec3, Vec3) → Vec3Component-wise sub
mop_vec3_scale(Vec3, float) → Vec3Scalar multiply
mop_vec3_cross(Vec3, Vec3) → Vec3Cross product
mop_vec3_dot(Vec3, Vec3) → floatDot product
mop_vec3_length(Vec3) → floatEuclidean length
mop_vec3_normalize(Vec3) → Vec3Unit vector (0 if degenerate)

Matrix Operations

FunctionSignatureDescription
mop_mat4_identity() → Mat4Identity matrix
mop_mat4_perspective(fov_rad, aspect, near, far) → Mat4Symmetric perspective frustum
mop_mat4_look_at(eye, center, up) → Mat4View matrix
mop_mat4_rotate_y(angle_rad) → Mat4Rotation around Y axis
mop_mat4_rotate_x(angle_rad) → Mat4Rotation around X axis
mop_mat4_translate(Vec3) → Mat4Translation matrix
mop_mat4_scale(Vec3) → Mat4Non-uniform scale
mop_mat4_multiply(Mat4, Mat4) → Mat4Matrix product A * B
mop_mat4_mul_vec4(Mat4, Vec4) → Vec4Matrix-vector product M * v

Perspective Matrix

Uses the standard OpenGL infinite-far formulation:

| 1/(a·tan(fov/2))   0               0                    0              |
| 0                   1/tan(fov/2)    0                    0              |
| 0                   0               -(f+n)/(f-n)         -2fn/(f-n)     |
| 0                   0               -1                   0              |

Where a = aspect, f = far, n = near.

Look-At Matrix

Constructs an orthonormal basis (s, u, -f) from eye, center, and up, then applies the inverse translation.

| s.x   s.y   s.z   -dot(s,eye) |
| u.x   u.y   u.z   -dot(u,eye) |
|-f.x  -f.y  -f.z    dot(f,eye) |
| 0     0     0      1           |

Stored column-major: s occupies row 0, u occupies row 1, -f occupies row 2, translation occupies column 3.