Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
matrix4.c
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Igal Alkon
2// SPDX-FileCopyrightText: 2026 ALKONTEK <git@alkontek.com>
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <vecmat.h>
6
15{
16 matrix4 res = {0};
18 return res;
19}
20
31{
32 matrix4 res = {0};
33 mat4_mul_ptr(&res, &a, &b);
34 return res;
35}
36
46{
47 matrix4 res = {0};
48 mat4_transpose_ptr(&res, &m);
49 return res;
50}
51
61{
62 matrix4 res = {0};
63 mat4_inverse_ptr(&res, &m);
64 return res;
65}
66
76{
77 matrix4 res = {0};
78 mat4_translate_ptr(&res, &v);
79 return res;
80}
81
91{
92 matrix4 res = {0};
93 mat4_scale_ptr(&res, &v);
94 return res;
95}
96
106matrix4 mat4_rotation(const vector3 axis, const vm_float_t angle)
107{
108 matrix4 res = {0};
109 mat4_rotation_ptr(&res, &axis, angle);
110 return res;
111}
112
124matrix4 mat4_perspective(const vm_float_t fov, const vm_float_t aspect, const vm_float_t near, const vm_float_t far)
125{
126 matrix4 res = {0};
127 mat4_perspective_ptr(&res, fov, aspect, near, far);
128 return res;
129}
130
144matrix4 mat4_ortho(const vm_float_t left, const vm_float_t right, const vm_float_t bottom,
145 const vm_float_t top, const vm_float_t near, const vm_float_t far)
146{
147 matrix4 res = {0};
148 mat4_ortho_ptr(&res, left, right, bottom, top, near, far);
149 return res;
150}
151
162matrix4 mat4_look_at(const vector3 position, const vector3 target, const vector3 up)
163{
164 matrix4 res = {0};
165 mat4_look_at_ptr(&res, &position, &target, &up);
166 return res;
167}
168
182 const vm_float_t n, const vm_float_t f)
183{
184 matrix4 res = {0};
185 mat4_perspective_fov_ptr(&res, fov, w, h, n, f);
186 return res;
187}
188
200{
201 matrix4 res = {0};
202 mat4_perspective_infinite_ptr(&res, fov_y, aspect, n);
203 return res;
204}
205
215{
216 const vm_float_t cofactor_1 = m.v[5] * m.v[10] * m.v[15] - m.v[5] * m.v[14] * m.v[11] - m.v[9] * m.v[6] * m.v[15] +
217 m.v[9] * m.v[14] * m.v[7] + m.v[13] * m.v[6] * m.v[11] - m.v[13] * m.v[10] * m.v[7];
218
219 const vm_float_t cofactor_2 = -m.v[4] * m.v[10] * m.v[15] + m.v[4] * m.v[14] * m.v[11] + m.v[8] * m.v[6] * m.v[15] -
220 m.v[8] * m.v[14] * m.v[7] - m.v[12] * m.v[6] * m.v[11] + m.v[12] * m.v[10] * m.v[7];
221
222 const vm_float_t cofactor_3 = m.v[4] * m.v[9] * m.v[15] - m.v[4] * m.v[13] * m.v[11] - m.v[8] * m.v[5] * m.v[15] +
223 m.v[8] * m.v[13] * m.v[7] + m.v[12] * m.v[5] * m.v[11] - m.v[12] * m.v[9] * m.v[7];
224
225 const vm_float_t cofactor_4 = -m.v[4] * m.v[9] * m.v[14] + m.v[4] * m.v[13] * m.v[10] + m.v[8] * m.v[5] * m.v[14] -
226 m.v[8] * m.v[13] * m.v[6] - m.v[12] * m.v[5] * m.v[10] + m.v[12] * m.v[9] * m.v[6];
227
228 return m.v[0] * cofactor_1 + m.v[1] * cofactor_2 + m.v[2] * cofactor_3 + m.v[3] * cofactor_4;
229}
230
241{
242 vector4 res;
243 mat4_mul_vec4_ptr(&res, &m, &v);
244 return res;
245}
246
258{
259 vector3 res;
260 mat4_mul_vec3_ptr(&res, &m, &v, w);
261 return res;
262}
263
273{
274 matrix4 res;
275 mat4_rotation_x_ptr(&res, degrees);
276 return res;
277}
278
288{
289 matrix4 res;
290 mat4_rotation_y_ptr(&res, degrees);
291 return res;
292}
293
303{
304 matrix4 res;
305 mat4_rotation_z_ptr(&res, degrees);
306 return res;
307}
308
319matrix4 mat4_trs(const vector3 translation, const quaternion rotation, const vector3 scale)
320{
321 matrix4 res;
322 mat4_trs_ptr(&res, &translation, &rotation, &scale);
323 return res;
324}
325
335{
336 matrix4 res;
337 mat4_from_mat3_ptr(&res, &m);
338 return res;
339}
340
350{
351 vector3 res;
353 return res;
354}
355
365{
366 vector3 res;
367 mat4_extract_scale_ptr(&res, &m);
368 return res;
369}
370
380{
381 quaternion res;
383 return res;
384}
matrix4 mat4_from_mat3(const matrix3 m)
Embeds a matrix3 into the upper-left of a matrix4.
Definition matrix4.c:334
matrix4 mat4_transpose(const matrix4 m)
Computes the transpose of a 4x4 matrix.
Definition matrix4.c:45
matrix4 mat4_perspective_infinite(const vm_float_t fov_y, const vm_float_t aspect, const vm_float_t n)
Constructs an infinite far-plane perspective projection matrix.
Definition matrix4.c:199
matrix4 mat4_scale(const vector3 v)
Constructs a scaling matrix from the given scale vector.
Definition matrix4.c:90
matrix4 mat4_rotation_x(const vm_float_t degrees)
Builds a 4x4 rotation matrix around the X axis (degrees).
Definition matrix4.c:272
vector3 mat4_extract_translation(const matrix4 m)
Extracts the translation vector from a matrix4.
Definition matrix4.c:349
matrix4 mat4_trs(const vector3 translation, const quaternion rotation, const vector3 scale)
Builds a 4x4 TRS matrix from translation, rotation, and scale.
Definition matrix4.c:319
vector4 mat4_mul_vec4(const matrix4 m, const vector4 v)
Multiplies a 4x4 matrix by a vector4.
Definition matrix4.c:240
vector3 mat4_extract_scale(const matrix4 m)
Extracts the scale vector from a matrix4.
Definition matrix4.c:364
matrix4 mat4_rotation_y(const vm_float_t degrees)
Builds a 4x4 rotation matrix around the Y axis (degrees).
Definition matrix4.c:287
matrix4 mat4_rotation(const vector3 axis, const vm_float_t angle)
Constructs a rotation matrix around the given axis by the specified angle (in radians).
Definition matrix4.c:106
matrix4 mat4_identity(void)
Constructs the 4x4 identity matrix.
Definition matrix4.c:14
quaternion mat4_extract_rotation(const matrix4 m)
Extracts the rotation quaternion from a matrix4.
Definition matrix4.c:379
matrix4 mat4_rotation_z(const vm_float_t degrees)
Builds a 4x4 rotation matrix around the Z axis (degrees).
Definition matrix4.c:302
vm_float_t mat4_determinant(const matrix4 m)
Calculates the determinant of the given 4x4 matrix.
Definition matrix4.c:214
matrix4 mat4_inverse(const matrix4 m)
Computes the inverse of a 4x4 matrix.
Definition matrix4.c:60
matrix4 mat4_mul(const matrix4 a, const matrix4 b)
Multiplies two 4x4 matrices (a * b).
Definition matrix4.c:30
matrix4 mat4_perspective(const vm_float_t fov, const vm_float_t aspect, const vm_float_t near, const vm_float_t far)
Constructs a perspective projection matrix.
Definition matrix4.c:124
vector3 mat4_mul_vec3(const matrix4 m, const vector3 v, const vm_float_t w)
Transforms a vector3 by a 4x4 matrix using homogeneous w.
Definition matrix4.c:257
matrix4 mat4_perspective_fov(const vm_float_t fov, const vm_float_t w, const vm_float_t h, const vm_float_t n, const vm_float_t f)
Constructs a perspective projection matrix using FOV, width, and height.
Definition matrix4.c:181
matrix4 mat4_translate(const vector3 v)
Constructs a translation matrix from the given vector.
Definition matrix4.c:75
matrix4 mat4_ortho(const vm_float_t left, const vm_float_t right, const vm_float_t bottom, const vm_float_t top, const vm_float_t near, const vm_float_t far)
Constructs an orthographic projection matrix.
Definition matrix4.c:144
matrix4 mat4_look_at(const vector3 position, const vector3 target, const vector3 up)
Constructs a view matrix from eye position, target, and up vector.
Definition matrix4.c:162
vm_float_t v[VECMAT_MAT4_SIZE]
Definition vecmat.h:271
void mat4_mul_vec3_ptr(vector3 *res, const matrix4 *m, const vector3 *v, vm_float_t w)
void mat4_ortho_ptr(matrix4 *res, vm_float_t left, vm_float_t right, vm_float_t bottom, vm_float_t top, vm_float_t near, vm_float_t far)
Sets the matrix to an orthographic projection matrix.
void mat4_rotation_x_ptr(matrix4 *res, vm_float_t degrees)
Builds a 4x4 rotation matrix around the X axis (degrees).
void mat4_scale_ptr(matrix4 *res, const vector3 *v)
Sets the matrix to a scaling matrix using the provided scale vector.
void mat4_mul_vec4_ptr(vector4 *res, const matrix4 *m, const vector4 *v)
void mat4_extract_scale_ptr(vector3 *res, const matrix4 *m)
Extracts the scale vector from a matrix4.
void mat4_look_at_ptr(matrix4 *res, const vector3 *position, const vector3 *target, const vector3 *up)
Constructs a view matrix for a camera positioned at the given location, looking towards a target,...
void mat4_rotation_ptr(matrix4 *res, const vector3 *axis, vm_float_t angle)
Sets the matrix to a rotation matrix around the specified axis by the given angle.
void mat4_extract_rotation_ptr(quaternion *res, const matrix4 *m)
Extracts the rotation quaternion from a matrix4.
void mat4_inverse_ptr(matrix4 *res, const matrix4 *m)
Computes the inverse of a 4x4 matrix.
Definition matrix4_ptr.c:98
void mat4_perspective_fov_ptr(matrix4 *res, vm_float_t fov, vm_float_t w, vm_float_t h, vm_float_t n, vm_float_t f)
Sets the matrix to a perspective projection matrix.
void mat4_perspective_infinite_ptr(matrix4 *res, vm_float_t fov_y, vm_float_t aspect, vm_float_t n)
Sets the matrix to an infinite perspective projection matrix.
void mat4_rotation_z_ptr(matrix4 *res, vm_float_t degrees)
Builds a 4x4 rotation matrix around the Z axis (degrees).
double vm_float_t
Definition vecmat.h:79
void mat4_translate_ptr(matrix4 *res, const vector3 *v)
Sets the matrix to a translation matrix.
void mat4_extract_translation_ptr(vector3 *res, const matrix4 *m)
Extracts the translation vector from a matrix4.
void mat4_transpose_ptr(matrix4 *res, const matrix4 *m)
Definition matrix4_ptr.c:79
void mat4_from_mat3_ptr(matrix4 *res, const matrix3 *m)
Embeds a matrix3 into the upper-left of a matrix4.
void mat4_mul_ptr(matrix4 *res, const matrix4 *a, const matrix4 *b)
Definition matrix4_ptr.c:69
void mat4_perspective_ptr(matrix4 *res, vm_float_t fov, vm_float_t aspect, vm_float_t near, vm_float_t far)
Creates a perspective projection matrix.
void mat4_identity_ptr(matrix4 *res)
Sets the matrix to the identity matrix.
Definition matrix4_ptr.c:16
void mat4_trs_ptr(matrix4 *res, const vector3 *translation, const quaternion *rotation, const vector3 *scale)
Builds a 4x4 TRS matrix from translation, rotation, and scale.
void mat4_rotation_y_ptr(matrix4 *res, vm_float_t degrees)
Builds a 4x4 rotation matrix around the Y axis (degrees).