SH4ZAM! 0.1.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_matrix.h
Go to the documentation of this file.
1/*! \file
2 \brief API for operating on MxN matrices within memory.
3 \ingroup matrix
4
5 This file provides a collection of routines for manipulating
6 MxN matrices which are stored within memory, rather than in
7 the XMTRX FP register back-bank.
8
9 Some of these routines are simply loading into the back-bank
10 and are performing work there, temporarily. Some of them have
11 implementations which have specific optimizations for when the
12 matrices are NOT within such registers, which are faster than
13 having to go through XMTRX and clobbering the back-bank.
14
15 \todo
16 - shz_mat4x4_add_symmetric_skew()
17 - shz_mat4x4_add_diagonal()
18
19 \author 2025, 2026 Falco Girgis
20 \author 2025 Daniel Fairchild
21
22 \copyright MIT License
23*/
24
25#ifndef SHZ_MATRIX_H
26#define SHZ_MATRIX_H
27
28#include "shz_vector.h"
29#include "shz_quat.h"
30#include "shz_xmtrx.h"
31
32/*! \defgroup matrix Matrices
33 \brief In-Memory Matrix Manipulation
34
35 These types and their corresponding functions are for working
36 with matrices stored within memory, as opposed to being preloaded
37 within the XMTRX back-bank of FP registers. Typically this is
38 desirable for one-off operations where there is no batching of
39 matrix operations, especially when clobbering XMTRX is undesirable.
40
41 For most transform types, this API offers 4 different "versions" of
42 the operation. Using translation as an example:
43 - shz_mat4x4_init_translation(): **Initializes** the the matrix
44 to a given transform, setting the other components to identity.
45 - shz_mat4x4_set_translation(): **Sets** only the values
46 corresponding to the given transform, leaving the others alone.
47 - shz_mat4x4_apply_translation(): **Apply** transform operation,
48 updates only the values corresponding to the given transform based
49 on their current values (additively in this case, multiplicatively
50 for scaling and rotation).
51 - shz_mat4x4_translate(): **GL-based** transform operation,
52 multiplying and accumulating the given matrix by a matrix which
53 has been initialized to the given transform.
54
55 \warning
56 Beware that some of these routines clobber the matrix currently loaded
57 as the active 4x4 matrix, XMTRX.
58
59 \sa xmtrx
60*/
61
62SHZ_DECLS_BEGIN
63
64/*! Structure representing a 4x4 column-major matrix.
65
66 \warning
67 This structure MUST be aligned on 8-byte boundaries!
68*/
69typedef SHZ_ALIGNAS(8) struct shz_mat4x4 {
70 union { //!< Inner convenience union.
71 float elem[16]; //!< Access the matrix as a 1D array of 16 single-precision floats.
72 float elem2D[4][4]; //!< Access the matrix as a 2D array of 4x4 single-precision floats.
73 shz_vec4_t col[4]; //!< Access the matrix as an array of 4 1x4 column vectors.
74 struct { //!< Named column vectors.
75 shz_vec4_t left; //!< Access the first column of the matrix as a 1x4 vector.
76 shz_vec4_t up; //!< Access the second column of the matrix as a 1x4 vector.
77 shz_vec4_t forward; //!< Access the third column of the matrix as a 1x4 vector.
78 shz_vec4_t pos; //!< Access the last column of the matrix as a 1x4 vector.
79 };
80 };
81} shz_mat4x4_t;
82
83//! Alternate shz_mat4x4_t C typedef for those who hate POSIX style.
84typedef shz_mat4x4_t shz_mat4x4;
85
86/*! \name Initialization
87 \brief Routines for fully initializing a matrix.
88 @{
89*/
90
91/*! Initializes the given matrix to the identity matrix as fast as possible.
92
93 \warning This routine will NOT zero out NaNs properly!
94 \warning This routine clobbers XMTRX.
95
96 \sa shz_mat4x4_init_identity_safe()
97*/
98SHZ_INLINE void shz_mat4x4_init_identity(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
99
100/*! Initializes the given matrix to the identity matrix, safely zeroing out NaN values.
101
102 \warning This routine clobbers XMTRX.
103
104 \sa shz_mat4x4_init_identity()
105*/
106SHZ_INLINE void shz_mat4x4_init_identity_safe(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
107
108/*! Initializes the given matrix with all 0s for its element values.
109
110 \warning This routine clobbers XMTRX.
111*/
112SHZ_INLINE void shz_mat4x4_init_zero(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
113
114/*! Initializes the given matrix with all 1s for its element values.
115
116 \warning This routine clobbers XMTRX.
117*/
118SHZ_INLINE void shz_mat4x4_init_one(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
119
120/*! Initializes the given matrix with all elements assigned to the given value.
121
122 \warning This routine clobbers XMTRX.
123*/
124SHZ_INLINE void shz_mat4x4_init_fill(shz_mat4x4_t* mat, float value) SHZ_NOEXCEPT;
125
126/*! Initializes the given matrix to a 3D translation matrix with the given coordinates.
127
128 \warning This routine clobbers XMTRX.
129*/
130SHZ_INLINE void shz_mat4x4_init_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
131
132/*! Initializes the given matrix to a 3D scaling matrix with the given dimensions.
133
134 \warning This routine clobbers XMTRX.
135*/
136SHZ_INLINE void shz_mat4x4_init_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
137
138/*! Initializes the given matrix to a 3D rotation matrix by \p xAngle radians over the X-axis.
139
140 \warning This routine clobbers XMTRX.
141*/
142SHZ_INLINE void shz_mat4x4_init_rotation_x(shz_mat4x4_t* mat, float xAngle) SHZ_NOEXCEPT;
143
144/*! Initializes the given matrix to a 3D rotation matrix by \p yAngle radians over the Y-axis.
145
146 \warning This routine clobbers XMTRX.
147*/
148SHZ_INLINE void shz_mat4x4_init_rotation_y(shz_mat4x4_t* mat, float yAngle) SHZ_NOEXCEPT;
149
150/*! Initializes the given matrix to a 3D rotation matrix by \p zAngle radians over the Z-axis.
151
152 \warning This routine clobber XMTRX.
153*/
154SHZ_INLINE void shz_mat4x4_init_rotation_z(shz_mat4x4_t* mat, float zAngle) SHZ_NOEXCEPT;
155
156/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan X-Y-Z angles.
157
158 \warning This routine clobbers XMTRX.
159*/
160SHZ_INLINE void shz_mat4x4_init_rotation_xyz(shz_mat4x4_t* mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT;
161
162/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
163
164 \warning This routine clobbers XMTRX.
165*/
166SHZ_INLINE void shz_mat4x4_init_rotation_zyx(shz_mat4x4_t* mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT;
167
168/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-X-Y angles.
169
170 \warning This routine clobbers XMTRX.
171*/
172SHZ_INLINE void shz_mat4x4_init_rotation_zxy(shz_mat4x4_t* mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT;
173
174/*! Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Y-X-Z angles.
175
176 \warning This routine clobbers XMTRX.
177*/
178SHZ_INLINE void shz_mat4x4_init_rotation_yxz(shz_mat4x4_t* mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT;
179
180/*! Initializes the given matrix to a 3D rotation matrix about the given \p axis rotated by \p angle radians.
181
182 \warning This routine clobbers XMTRX.
183*/
184SHZ_INLINE void shz_mat4x4_init_rotation(shz_mat4x4_t* mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
185
186/*! Initializes the given matrix to a 3D rotation matrix with its orientation given by a quaternion.
187
188 \warning This routine clobbers XMTRX.
189*/
190SHZ_INLINE void shz_mat4x4_init_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
191
192/*! Initializes the given matrix to a diagonal matrix with the given 4 values.
193
194 \warning This routine clobbers XMTRX.
195*/
196SHZ_INLINE void shz_mat4x4_init_diagonal(shz_mat4x4_t* mat, float x, float y, float z, float w) SHZ_NOEXCEPT;
197
198/*! Initializes the given matrix to an upper triangular matrix whose nonzero entries have the given value.
199
200 \warning This routine clobbers XMTRX.
201*/
202SHZ_INLINE void shz_mat4x4_init_upper_triangular(shz_mat4x4_t* mat, float col1, shz_vec2_t col2, shz_vec3_t col3, shz_vec4_t col4) SHZ_NOEXCEPT;
203
204/*! Initializes the given matrix to a lower triangular matrix whose nonzero entries have the given value.
205
206 \warning This routine clobbers XMTRX.
207*/
208SHZ_INLINE void shz_mat4x4_init_lower_triangular(shz_mat4x4_t* mat, shz_vec4_t col1, shz_vec3_t col2, shz_vec2_t col3, float col4) SHZ_NOEXCEPT;
209
210/*! Initializes the given matrix to be the symmetric skew of the given 3D vector components.
211
212 \note
213 This can be useful for batching the cross-product operation against a constant vector.
214 For one-offs, prefer shz_vec3_cross().
215
216 \warning This routine clobbers XMTRX.
217*/
218SHZ_INLINE void shz_mat4x4_init_symmetric_skew(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
219
220/*! Initializes the given matrix to be the result from taking the outer product of the two given 4D vectors.
221
222 \warning This routine clobbers XMTRX.
223*/
224SHZ_INLINE void shz_mat4x4_init_outer_product(shz_mat4x4_t* mat, shz_vec4_t v1, shz_vec4_t v2) SHZ_NOEXCEPT;
225
226/*! Initializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
227
228 \warning This routine clobbers XMTRX.
229*/
230SHZ_INLINE void shz_mat4x4_init_permutation_wxyz(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
231
232/*! Initializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
233
234 \warning This routine clobbers XMTRX.
235*/
236SHZ_INLINE void shz_mat4x4_init_permutation_yzwx(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
237
238/*! Initializes the given matrix to the viewport matrix with the given dimenions.
239
240 \sa This routine clobbers XMTRX.
241*/
242SHZ_INLINE void shz_mat4x4_init_screen(shz_mat4x4_t* mat, float width, float height) SHZ_NOEXCEPT;
243
244/*! Initializes the given matrix to a "lookAt" view matrix.
245
246 \warning This routine clobbers XMTRX.
247*/
248SHZ_INLINE void shz_mat4x4_init_lookat(shz_mat4x4_t* mat, shz_vec3_t eye, shz_vec3_t center, shz_vec3_t up) SHZ_NOEXCEPT;
249
250/*! Initializes the given matrix to an orthographic projection matrix.
251
252 \warning This routine clobbers XMTRX.
253*/
254SHZ_INLINE void shz_mat4x4_init_ortho(shz_mat4x4_t* mat, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT;
255
256/*! Initializes the given matrix to a frustum projection matrix.
257
258 \warning This routine clobbers XMTRX.
259*/
260SHZ_INLINE void shz_mat4x4_init_frustum(shz_mat4x4_t* mat, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT;
261
262/*! Initializes the given matrix to a perspective projection matrix.
263
264 \warning This routine clobbers XMTRX.
265*/
266SHZ_INLINE void shz_mat4x4_init_perspective(shz_mat4x4_t* mat, float fov, float aspect, float znear) SHZ_NOEXCEPT;
267
268//! @}
269
270
271/*! \name Decomposition
272 \brief Routines for decomposing a matrix into its constituent transforms.
273 @{
274*/
275
276/*! Decomposes a 4x4 transform matrix into translation, rotation, and scale.
277
278 \note Any output pointer may be NULL to skip that component.
279 \warning This routine clobbers XMTRX.
280*/
281void shz_mat4x4_decompose(const shz_mat4x4_t* mat,
282 shz_vec3_t* translation,
283 shz_quat_t* rotation,
284 shz_vec3_t* scale) SHZ_NOEXCEPT;
285
286//! @}
287
288/*! \name Getting
289 \brief Routines for getting specific values within a matrix.
290 @{
291*/
292
293//! Extracts the \p row index as a 4D row vector from the given matrix.
294SHZ_INLINE shz_vec4_t shz_mat4x4_row(const shz_mat4x4_t* mat, size_t row) SHZ_NOEXCEPT;
295
296//! Extracts the \p col index as a 4D column vector from the given matrix.
297SHZ_INLINE shz_vec4_t shz_mat4x4_col(const shz_mat4x4_t* mat, size_t col) SHZ_NOEXCEPT;
298
299//! Returns the translational components from the 4th column as a 3D vector.
300SHZ_INLINE shz_vec3_t shz_mat4x4_get_translation(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
301
302//! Returns the determinant of the given 4x4 matrix.
303SHZ_INLINE float shz_mat4x4_determinant(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
304
305//! Returns the trace of the given 4x4 matrix.
306SHZ_INLINE float shz_mat4x4_trace(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
307
308//! Extracts the top-left 3x3 of the given 4D matrix.
309SHZ_INLINE void shz_mat4x4_3x3(const shz_mat4x4_t* mat4, shz_mat3x3_t* mat3) SHZ_NOEXCEPT;
310
311//! Extracts and inverts the top-level, unscaled 3x3 of the given 4x4 matrix.
312SHZ_INLINE void shz_mat4x4_3x3_inverse_unscaled(const shz_mat4x4_t* mat4, shz_mat3x3_t* invmat3) SHZ_NOEXCEPT;
313
314//! Extracts and inverts the top-level 3x3 (which may be scaled) of the given 4x4 matrix.
315SHZ_INLINE void shz_mat4x4_3x3_inverse(const shz_mat4x4_t* mat4, shz_mat3x3_t* invmat3) SHZ_NOEXCEPT;
316
317//! Returns the determinant of the given 4x4 matrix's internal top-level 3x3 matrix.
318SHZ_INLINE float shz_mat4x4_3x3_determinant(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
319
320//! @}
321
322/*! \name Setting
323 \brief Routines for setting specific values within a matrix
324 @{
325*/
326
327//! Sets the values of \p mat at the given \p row to those of the 4D vector, \p vec.
328SHZ_INLINE void shz_mat4x4_set_row(shz_mat4x4_t* mat, size_t row, shz_vec4_t vec) SHZ_NOEXCEPT;
329
330//! Sets the values of \p mat at the given \p col to those of the 4D vector, \p vec.
331SHZ_INLINE void shz_mat4x4_set_col(shz_mat4x4_t* mat, size_t col, shz_vec4_t vec) SHZ_NOEXCEPT;
332
333//! Swaps the 4D row vectors located at \p row1 and \p row2 within \p mat.
334SHZ_INLINE void shz_mat4x4_swap_rows(shz_mat4x4_t* mat, size_t row1, size_t row2) SHZ_NOEXCEPT;
335
336//! Swaps the 4D column vectors located at \p col1 and \p col2 within \p mat.
337SHZ_INLINE void shz_mat4x4_swap_cols(shz_mat4x4_t* mat, size_t col1, size_t col2) SHZ_NOEXCEPT;
338
339//! Assigns only the 3D translation-related elements of the given matrix to the given values.
340SHZ_INLINE void shz_mat4x4_set_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
341
342//! Assigns only the 3D scale-related elements of the given matrix to the given values.
343SHZ_INLINE void shz_mat4x4_set_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
344
345//! Sets just the rotational component of the matrix to the orientation given by a quaternion, keeping the other elements in-tact.
346SHZ_INLINE void shz_mat4x4_set_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
347
348//! Assigns only the 4 elements along the diagonal of the given matrix to the given values.
349SHZ_INLINE void shz_mat4x4_set_diagonal(shz_mat4x4_t* mat, float x, float y, float z, float w) SHZ_NOEXCEPT;
350
351//! @}
352
353/*! \name Applying
354 \brief Routines for multiplying and accumulating onto the given matrix.
355 @{
356*/
357
358/*! Multiplies and accumulates the \p src 4x4 matrix onto the \p dst 4x4 matrix.
359
360 \warning This routine clobbers XMTRX.
361*/
362SHZ_INLINE void shz_mat4x4_apply(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
363
364/*! Multiplies and accumulates the unaligned \p src 4x4 matrix onto the \p dst 4x4 matrix.
365
366 \warning This routine clobbers XMTRX.
367*/
368SHZ_INLINE void shz_mat4x4_apply_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
369
370/*! Multiplies and accumulates the transposed \p src 4x4 matrix onto the \p dst 4x4 matrix.
371
372 \warning This routine clobbers XMTRX.
373*/
374SHZ_INLINE void shz_mat4x4_apply_transpose(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
375
376/*! Multiplies and accumulates the transposed unaligned \p src 4x4 matrix onto the \p dst 4x4 matrix.
377
378 \warning This routine clobbers XMTRX.
379*/
380SHZ_INLINE void shz_mat4x4_apply_transpose_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
381
382/*! Adds the given 3D vector components to the translational values of the given matrix.
383
384 \note This routine does not use XMTRX.
385*/
386SHZ_INLINE void shz_mat4x4_apply_translation(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
387
388/*! Multiplies and accumulates the scale-related elements of the given matrix by the given 3D components.
389
390 \warning This routine clobbers XMTRX.
391*/
392SHZ_INLINE void shz_mat4x4_apply_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
393
394/*! Multiplies and accumulates a rotation matrix by \p xAngle radians about the X-axis onto the given matrix.
395
396 \warning This routine clobbers XMTRX.
397*/
398SHZ_INLINE void shz_mat4x4_apply_rotation_x(shz_mat4x4_t* mat, float xAngle) SHZ_NOEXCEPT;
399
400/*! Multiplies and accumulates a rotation matrix by \p yAngle radians about the Y-axis onto the given matrix.
401
402 \warning This routine clobbers XMTRX.
403*/
404SHZ_INLINE void shz_mat4x4_apply_rotation_y(shz_mat4x4_t* mat, float yAngle) SHZ_NOEXCEPT;
405
406/*! Multiplies and accumulates a rotation matrix by \p zAngle radians about the Z-axis onto the given matrix.
407
408 \warning This routine clobbers XMTRX.
409*/
410SHZ_INLINE void shz_mat4x4_apply_rotation_z(shz_mat4x4_t* mat, float zAngle) SHZ_NOEXCEPT;
411
412/*! Rotates the given transform matrix about the X axis, then the Y axis, then the Z axis by the given angles in radians.
413
414 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan X-Y-Z angles.
415
416 \warning This routine clobbers XMTRX.
417*/
418SHZ_INLINE void shz_mat4x4_apply_rotation_xyz(shz_mat4x4_t* mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT;
419
420/*! Rotates the given transform matrix about the Z axis, then the Y axis, then the X axis by the given angles in radians.
421
422 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
423
424 \warning This routine clobbers XMTRX.
425*/
426SHZ_INLINE void shz_mat4x4_apply_rotation_zyx(shz_mat4x4_t* mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT;
427
428/*! Rotates the given transform matrix about the Z axis, then the X axis, then the Y axis by the given angles in radians.
429
430 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
431
432 \warning This routine clobbers XMTRX.
433*/
434SHZ_INLINE void shz_mat4x4_apply_rotation_zxy(shz_mat4x4_t* mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT;
435
436/*! Rotates the given transform matrix about the Y axis, then the X axis, then the Z axis by the given angles in radians.
437
438 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Y-X-Z angles.
439
440 \warning This routine clobbers XMTRX.
441*/
442SHZ_INLINE void shz_mat4x4_apply_rotation_yxz(shz_mat4x4_t* mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT;
443
444/*! Rotates the given transform matrix about the arbitrary axis given by a 3D direction vector and angle of rotation in radians.
445
446 Multiplies and accumulates the given matrix with the 3D rotation matrix formed from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
447
448 \warning This routine clobbers XMTRX.
449*/
450SHZ_INLINE void shz_mat4x4_apply_rotation(shz_mat4x4_t* mat, float angle, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
451
452/*! Multiplies and accumulates the given matrix with a rotation matrix whose orientation is given by a quaternion.
453
454 \note This routine clobbers XMTRX.
455*/
456SHZ_INLINE void shz_mat4x4_apply_rotation_quat(shz_mat4x4_t* m, shz_quat_t q) SHZ_NOEXCEPT;
457
458/*! Applies the 3D "lookAt" matrix constructed with the given vector components onto the given matrix.
459
460 \warning This routine clobbers XMTRX.
461*/
462SHZ_INLINE void shz_mat4x4_apply_lookat(shz_mat4x4_t* m, shz_vec3_t pos, shz_vec3_t target, shz_vec3_t up) SHZ_NOEXCEPT;
463
464/*! Multiplies and accumulates the ortho matrix constructed from the given values onto the given matrix.
465
466 \warning This routine clobbers XMTRX.
467*/
468SHZ_INLINE void shz_mat4x4_apply_ortho(shz_mat4x4_t* m, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT;
469
470/*! Multiplies and accumulates the frustum matrix constructed from the given values onto the given matrix.
471
472 \warning This routine clobbers XMTRX.
473*/
474SHZ_INLINE void shz_mat4x4_apply_frustum(shz_mat4x4_t* m, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT;
475
476/*! Multiplies and accumulates the perspective matrix constructed from the given values onto the given matrix.
477
478 \warning This routine clobbers XMTRX.
479*/
480SHZ_INLINE void shz_mat4x4_apply_perspective(shz_mat4x4_t* m, float fov, float aspect, float znear) SHZ_NOEXCEPT;
481
482/*! Multiplies and accumulates the viewport matrix created with the given components ont othe given matrix.
483
484 \warning This routine clobbers XMTRX.
485*/
486SHZ_INLINE void shz_mat4x4_apply_screen(shz_mat4x4_t* m, float width, float height) SHZ_NOEXCEPT;
487
488/*! Multiplies and accumulates the given matrix with a symmetric skew matrix formed from the given 3D vector components.
489
490 \warning This routine clobbers XMTRX.
491*/
492SHZ_INLINE void shz_mat4x4_apply_symmetric_skew(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
493
494/*! Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
495
496 \warning This routine clobbers XMTRX.
497*/
498SHZ_INLINE void shz_mat4x4_apply_permutation_wxyz(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
499
500/*! Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
501
502 \warning This routine clobbers XMTRX.
503*/
504SHZ_INLINE void shz_mat4x4_apply_permutation_yzwx(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
505
506/*! Multiplies and accumulates the given matrix onto itself.
507
508 \warning This routine clobbers XMTRX.
509*/
510SHZ_INLINE void shz_mat4x4_apply_self(shz_mat4x4_t* mat) SHZ_NOEXCEPT;
511
512//! @}
513
514/*! \name GL Transformations
515 \brief OpenGL-style 4x4 matrix transforms.
516 @{
517*/
518
519/*! Multiplies and accumulates \p mat by a 3D translation matrix with the given components.
520
521 \note glTranslatef() equivalent.
522 \warning This routine clobbers XMTRX.
523*/
524SHZ_INLINE void shz_mat4x4_translate(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
525
526/*! Multiplies and accumulates \p mat by a 3D scaling matrix with the given components.
527
528 \note glScalef() equivalent.
529 \warning This routine clobbers XMTRX.
530*/
531SHZ_INLINE void shz_mat4x4_scale(shz_mat4x4_t* mat, float x, float y, float z) SHZ_NOEXCEPT;
532
533/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the X axis.
534
535 \warning This routine clobbers XMTRX.
536*/
537SHZ_INLINE void shz_mat4x4_rotate_x(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
538
539/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the Y axis.
540
541 \warning This routine clobbers XMTRX.
542*/
543SHZ_INLINE void shz_mat4x4_rotate_y(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
544
545/*! Multiplies and accumulates \p mat by a 3D rotation matrix about the Z axis.
546
547 \warning This routine clobbers XMTRX.
548*/
549SHZ_INLINE void shz_mat4x4_rotate_z(shz_mat4x4_t* mat, float radians) SHZ_NOEXCEPT;
550
551/*! Multiplies and accumulates \p mat by 3D rotation matrices about the X then Y then Z axes.
552
553 \warning This routine clobbers XMTRX.
554*/
555SHZ_INLINE void shz_mat4x4_rotate_xyz(shz_mat4x4_t* mat, float xRadians, float yRadians, float zRadians) SHZ_NOEXCEPT;
556
557/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Z then Y then X axes.
558
559 \warning This routine clobbers XMTRX.
560*/
561SHZ_INLINE void shz_mat4x4_rotate_zyx(shz_mat4x4_t* mat, float zRadians, float yRadians, float xRadians) SHZ_NOEXCEPT;
562
563/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Z then X then Y axes.
564
565 \warning This routine clobbers XMTRX.
566*/
567SHZ_INLINE void shz_mat4x4_rotate_zxy(shz_mat4x4_t* mat, float zRadians, float xRadians, float yRadians) SHZ_NOEXCEPT;
568
569/*! Multiplies and accumulates \p mat by 3D rotation matrices about the Y then X then Z axes.
570
571 \warning This routine clobbers XMTRX.
572*/
573SHZ_INLINE void shz_mat4x4_rotate_yxz(shz_mat4x4_t* mat, float yRadians, float xRadians, float zRadians) SHZ_NOEXCEPT;
574
575/*! Multiplies and accumulates \p mat by the 3D rotation matrix formed by the given axis and angle.
576
577 \note Equivalent to glRotatef().
578 \warning This routine clobbers XMTRX.
579*/
580SHZ_INLINE void shz_mat4x4_rotate(shz_mat4x4_t* mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT;
581
582//!@}
583
584/*! \name Transforming
585 \brief Routines for transforming vectors and points by a matrix.
586 @{
587*/
588
589/*! Multiplies two 4x4 matrices together, storing the result into a third.
590
591 \warning This routine clobbers XMTRX.
592*/
593SHZ_INLINE void shz_mat4x4_mult(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const shz_mat4x4_t* rhs) SHZ_NOEXCEPT;
594
595/*! Multiplies two 4x4 matrices together, with the right handed matrix being unaligned, storing the result into a third.
596
597 \warning This routine clobbers XMTRX.
598*/
599SHZ_INLINE void shz_mat4x4_mult_unaligned(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const float rhs[16]) SHZ_NOEXCEPT;
600
601/*! Multiplies the regular matrix, \p lhs, by the transpose of the matrix, \p rhs, storing the result into \p mat.
602
603 \warning This routine clobbers XMTRX.
604*/
605SHZ_INLINE void shz_mat4x4_mult_transpose(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const shz_mat4x4_t* rhs) SHZ_NOEXCEPT;
606
607/*! Multiplies the regular matrix, \p lhs, by the transpose of the unaligned matrix, \p rhs, storing the result into \p mat.
608
609 \warning This routine clobbers XMTRX.
610*/
611SHZ_INLINE void shz_mat4x4_mult_transpose_unaligned(shz_mat4x4_t* mat, const shz_mat4x4_t* lhs, const float rhs[16]) SHZ_NOEXCEPT;
612
613/*! Transforms a 2D vector by a 4x4 matrix.
614
615 This is a routine specializing in one-off transforms of a **single**
616 2D vector by a **single** 4x4 matrix. It should be faster than going
617 through XMTRX.
618
619 \note
620 For batch transforming multiple 2D vectors against the same 4x4 matrix,
621 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec2().
622
623 \sa shz_mat4x4_transform_vec2(), shz_xmtrx_transform_vec3()
624*/
625SHZ_INLINE shz_vec2_t shz_mat4x4_transform_vec2(const shz_mat4x4_t* m, shz_vec2_t v) SHZ_NOEXCEPT;
626
627/*! Transforms a 3D vector by a 4x4 matrix.
628
629 This is a routine specializing in one-off transforms of a **single**
630 3D vector (such as a normal, without a W component) by a **single**
631 4x4 matrix. It should be faster than going through XMTRX.
632
633 \note
634 For batch transforming multiple 3D vectors against the same 4x4 matrix,
635 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec3().
636
637 \sa shz_mat4x4_transform_vec4(), shz_xmtrx_transform_vec3()
638*/
639SHZ_INLINE shz_vec3_t shz_mat4x4_transform_vec3(const shz_mat4x4_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
640
641/*! Transforms a 4D vector by a 4x4 matrix.
642
643 This is a routine specializing in one-off transforms of a *single*
644 4D vector by a *single* 4x4 matrix. It should be faster than going
645 through XMTRX.
646
647 \note
648 For batch transforming multiple 4D vectors against the same 4x4 matrix,
649 preload the matrix into XMTRX, then use shz_xmtrx_trans_vec4().
650
651 \sa shz_mat4x4_transform_vec3(), shz_xmtrx_transform_vec4()
652*/
653SHZ_INLINE shz_vec4_t shz_mat4x4_transform_vec4(const shz_mat4x4_t* mat, shz_vec4_t in) SHZ_NOEXCEPT;
654
655/*! Transforms a 2D vector the the transpose of a 4x4 matrix.
656
657 This is a routine specializing in one-off transforms of a **single**
658 2D vector by the transpose of a **single** 4x4 matrix. It should be
659 faster than going through XMTRX.
660
661 \note
662 For batch transforming multiple 2D vectors against the transpose of
663 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
664 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec2().
665
666 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec2().
667*/
668SHZ_INLINE shz_vec2_t shz_mat4x4_transform_vec2_transpose(const shz_mat4x4_t* m, shz_vec2_t v) SHZ_NOEXCEPT;
669
670/*! Transforms a 3D vector the the transpose of a 4x4 matrix.
671
672 This is a routine specializing in one-off transforms of a **single**
673 3D vector by the transpose of a **single** 4x4 matrix. It should be
674 faster than going through XMTRX.
675
676 \note
677 For batch transforming multiple 3D vectors against the transpose of
678 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
679 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec3().
680
681 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec3().
682*/
683SHZ_INLINE shz_vec3_t shz_mat4x4_transform_vec3_transpose(const shz_mat4x4_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
684
685/*! Transforms a 4D vector the the transpose of a 4x4 matrix.
686
687 This is a routine specializing in one-off transforms of a **single**
688 4D vector by the transpose of a **single** 4x4 matrix. It should be
689 faster than going through XMTRX.
690
691 \note
692 For batch transforming multiple 4D vectors against the transpose of
693 the same 4x4 matrix, preload the matrix into XMTRX, take its transpose
694 with shz_xmtrx_transpose(), then use shz_xmtrx_transform_vec4().
695
696 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_vec4().
697*/
698SHZ_INLINE shz_vec4_t shz_mat4x4_transform_vec4_transpose(const shz_mat4x4_t* m, shz_vec4_t v) SHZ_NOEXCEPT;
699
700/*! Transforms a 2D point by a 4x4 matrix.
701
702 This is a routine specializing in one-off transforms of a *single*
703 2D point by a *single* 4x4 matrix. It should be faster than going
704 through XMTRX.
705
706 \note
707 For batch transforming multiple 2D points against the same 4x4 matrix,
708 preload the matrix into XMTRX, then use shz_xmtrx_transform_point2().
709
710 \sa shz_xmtrx_transform_point2()
711*/
712SHZ_INLINE shz_vec2_t shz_mat4x4_transform_point2(const shz_mat4x4_t* mat, shz_vec2_t pt) SHZ_NOEXCEPT;
713
714/*! Transforms a 3D point by a 4x4 matrix.
715
716 This is a routine specializing in one-off transforms of a *single*
717 3D point by a *single* 4x4 matrix. It should be faster than going
718 through XMTRX.
719
720 \note
721 For batch transforming multiple 3D points against the same 4x4 matrix,
722 preload the matrix into XMTRX, then use shz_xmtrx_transform_point3().
723
724 \sa shz_xmtrx_transform_point3()
725*/
726SHZ_INLINE shz_vec3_t shz_mat4x4_transform_point3(const shz_mat4x4_t* mat, shz_vec3_t pt) SHZ_NOEXCEPT;
727
728/*! Transforms a 2D point by the transpose of a 4x4 matrix.
729
730 This is a routine specializing in one-off transforms of a *single*
731 2D point by the transpose of a a *single* 4x4 matrix. It should be
732 faster than going through XMTRX.
733
734 \note
735 For batch transforming multiple 2D points against the same 4x4 matrix,
736 preload the matrix into XMTRX, use shz_xmtrx_transpose(), then use
737 shz_xmtrx_transform_point2().
738
739 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_point2()
740*/
741SHZ_INLINE shz_vec2_t shz_mat4x4_transform_point2_transpose(const shz_mat4x4_t* mat, shz_vec2_t pt) SHZ_NOEXCEPT;
742
743/*! Transforms a 3D point by the transpose of a 4x4 matrix.
744
745 This is a routine specializing in one-off transforms of a *single*
746 3D point by the transpose of a a *single* 4x4 matrix. It should be
747 faster than going through XMTRX.
748
749 \note
750 For batch transforming multiple 3D points against the same 4x4 matrix,
751 preload the matrix into XMTRX, use shz_xmtrx_transpose(), then use
752 shz_xmtrx_transform_point2().
753
754 \sa shz_xmtrx_transpose(), shz_xmtrx_transform_point3()
755*/
756SHZ_INLINE shz_vec3_t shz_mat4x4_transform_point3_transpose(const shz_mat4x4_t* mat, shz_vec3_t pt) SHZ_NOEXCEPT;
757
758//! @}
759
760/*! \name Miscellaneous
761 \brief Other matrix-related operations and routines
762 @{
763*/
764
765//! Converts the given 4x4 orientation matrix into a quaternion.
766SHZ_INLINE shz_quat_t shz_mat4x4_to_quat(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
767
768/*! Stores the transpose of \p mat within \p out.
769
770 \warning This routine clobbers XMTRX.
771*/
772SHZ_INLINE void shz_mat4x4_transpose(const shz_mat4x4_t* mat, shz_mat4x4_t* out) SHZ_NOEXCEPT;
773
774/*! Computes the inverse of a 4x4 matrix.
775
776 \note
777 In-place inversion (mtrx == out) is not supported.
778
779 \warning This routine clobbers XMTRX.
780
781 \param mtrx Pointer to the 4x4 matrix to invert.
782 \param out Pointer to the resulting inverted matrix.
783*/
784void shz_mat4x4_inverse(const shz_mat4x4_t* SHZ_RESTRICT mtrx, shz_mat4x4_t* SHZ_RESTRICT out) SHZ_NOEXCEPT;
785
786/*! Computes the inverse of a 4x4 matrix in block-triangular form.
787
788 This is a special-case faster optimization for 4x4 matrices which take the form:
789
790 A = [ M b ]
791 [ 0 w ]
792
793 Where A is 4x4, M is 3x3, b is 3x1, and the bottom row is (0, 0, 0, w) with
794 w != 0. For this block-triangular form, det(A) = det(M) * w. Then
795
796 inv(A) = [ inv(M) -inv(M) * b / w ]
797 [ 0 1/w ]
798 \note
799 A regular 3D transform matrix is already in this form.
800
801 \note
802 shz_mat4x4_inverse() will dynamically check whether to use this optimization.
803*/
804void shz_mat4x4_inverse_block_triangular(const shz_mat4x4_t* mtx, shz_mat4x4_t* out) SHZ_NOEXCEPT;
805
806//! Returns true if the two matrices are equal, based on either absolute or relative tolerance.
807SHZ_INLINE bool shz_mat4x4_equal(const shz_mat4x4_t* SHZ_RESTRICT mat1, const shz_mat4x4_t* mat2) SHZ_NOEXCEPT;
808
809//! Returns true if the given matrix is in block-triangular form: having a bottom row in the form of `<0.0f, 0.0f, 0.0f, w>`.
810SHZ_INLINE bool shz_mat4x4_is_block_triangular(const shz_mat4x4_t* mat) SHZ_NOEXCEPT;
811
812//! Copies the given \p src 4x4 matrix into the given \p dst 4x4 matrix.
813SHZ_INLINE void shz_mat4x4_copy(shz_mat4x4_t* dst, const shz_mat4x4_t* src) SHZ_NOEXCEPT;
814
815/*! Copies the given unaligned \p src 4x4 matrix into the given \p dst 4x4 matrix.
816
817 \warning This routine clobbers XMTRX.
818*/
819SHZ_INLINE void shz_mat4x4_copy_unaligned(shz_mat4x4_t* dst, const float src[16]) SHZ_NOEXCEPT;
820
821/*! Swaps the contents of the two given matrices, \p matA and \p matB.
822
823 \warning This routine clobbers XMTRX.
824*/
825SHZ_INLINE void shz_mat4x4_swap(shz_mat4x4_t* matA, shz_mat4x4_t* matB) SHZ_NOEXCEPT;
826
827//! @}
828
829//! \cond UNDOCUMENTED
830// Until API is complete.
831typedef SHZ_ALIGNAS(8) struct shz_mat2x2 {
832 union {
833 float elem[4];
834 float elem2D[2][2];
835 shz_vec2_t col[2];
836 };
837} shz_mat2x2_t;
838
839typedef struct shz_mat3x3 {
840 union {
841 float elem[9];
842 float elem2D[3][3];
843 shz_vec3_t col[3];
844 struct {
845 shz_vec3_t left;
846 shz_vec3_t up;
847 shz_vec3_t forward;
848 };
849 };
850} shz_mat3x3_t;
851
852typedef struct shz_mat3x4 {
853 union {
854 float elem[12];
855 float elem2D[4][3];
856 shz_vec3_t col[4];
857 struct {
858 shz_vec3_t left;
859 shz_vec3_t up;
860 shz_vec3_t forward;
861 shz_vec3_t pos;
862 };
863 };
864} shz_mat3x4_t;
865
866typedef struct shz_mat4x3 {
867 union {
868 float elem[12];
869 float elem2D[3][4];
870 shz_vec4_t col[3];
871 struct {
872 shz_vec4_t left;
873 shz_vec4_t up;
874 shz_vec4_t forward;
875 };
876 };
877} shz_mat4x3_t;
878
879/*!
880 Stores the transpose of 3x3 matrix \p mat within \p out.
881
882 mtrx: Pointer to the 3x3 matrix to transpose.
883 out: Pointer to the resulting transposed matrix.
884
885 \warning This routine clobbers XMTRX.
886
887 */
888SHZ_INLINE void shz_mat3x3_transpose(const shz_mat3x3_t* mat, shz_mat3x3_t* out) SHZ_NOEXCEPT;
889
890/*!
891 Computes the inverse of a 3x3 matrix, saves cycles by not scaling by the
892 determinant, which makes sense when used for normals and lighting that are
893 usually normalized later.
894
895 mtrx: Pointer to the 3x3 matrix to invert.
896 out: Pointer to the resulting inverted matrix.
897
898 \note
899 Only valid if the matrix is known to be orthonormal.
900
901 \warning This routine clobbers XMTRX.
902 */
903SHZ_INLINE void shz_mat3x3_inverse_unscaled(const shz_mat3x3_t* mtrx, shz_mat3x3_t* out) SHZ_NOEXCEPT;
904
905/*!
906 Computes the inverse of a 3x3 matrix.
907
908 mtrx: Pointer to the 3x3 matrix to invert.
909 out: Pointer to the resulting inverted matrix.
910
911 \note
912 Only valid for non-singular matrices.
913
914 \warning This routine clobbers XMTRX.
915 */
916SHZ_INLINE void shz_mat3x3_inverse(const shz_mat3x3_t* mtrx, shz_mat3x3_t* out) SHZ_NOEXCEPT;
917
918SHZ_INLINE void shz_mat3x3_scale(shz_mat3x3_t* dst, const shz_mat3x3_t* src, float value) SHZ_NOEXCEPT;
919SHZ_INLINE shz_vec3_t shz_mat3x3_transform_vec3(const shz_mat3x3_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
920SHZ_INLINE shz_vec3_t shz_mat3x3_transform_vec3_transpose(const shz_mat3x3_t* m, shz_vec3_t v) SHZ_NOEXCEPT;
921
922//! \endcond
923
924#include "inline/shz_matrix.inl.h"
925
926SHZ_DECLS_END
927
928#endif // SHZ_MATRIX_H
bool shz_mat4x4_is_block_triangular(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns true if the given matrix is in block-triangular form: having a bottom row in the form of <0....
void shz_mat4x4_apply_rotation_y(shz_mat4x4_t *mat, float yAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by yAngle radians about the Y-axis onto the given matrix...
void shz_mat4x4_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D scaling matrix with the given components.
void shz_mat4x4_init_lower_triangular(shz_mat4x4_t *mat, shz_vec4_t col1, shz_vec3_t col2, shz_vec2_t col3, float col4) SHZ_NOEXCEPT
Initializes the given matrix to a lower triangular matrix whose nonzero entries have the given value.
void shz_mat4x4_init_rotation_z(shz_mat4x4_t *mat, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by zAngle radians over the Z-axis.
void shz_mat4x4_set_diagonal(shz_mat4x4_t *mat, float x, float y, float z, float w) SHZ_NOEXCEPT
Assigns only the 4 elements along the diagonal of the given matrix to the given values.
void shz_mat4x4_transpose(const shz_mat4x4_t *mat, shz_mat4x4_t *out) SHZ_NOEXCEPT
Stores the transpose of mat within out.
void shz_mat4x4_init_rotation_zxy(shz_mat4x4_t *mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_apply_permutation_yzwx(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors...
void shz_mat4x4_init_frustum(shz_mat4x4_t *mat, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT
Initializes the given matrix to a frustum projection matrix.
void shz_mat4x4_decompose(const shz_mat4x4_t *mat, shz_vec3_t *translation, shz_quat_t *rotation, shz_vec3_t *scale) SHZ_NOEXCEPT
Decomposes a 4x4 transform matrix into translation, rotation, and scale.
shz_vec3_t shz_mat4x4_transform_point3(const shz_mat4x4_t *mat, shz_vec3_t pt) SHZ_NOEXCEPT
Transforms a 3D point by a 4x4 matrix.
void shz_mat4x4_mult_transpose_unaligned(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const float rhs[16]) SHZ_NOEXCEPT
Multiplies the regular matrix, lhs, by the transpose of the unaligned matrix, rhs,...
void shz_mat4x4_copy_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Copies the given unaligned src 4x4 matrix into the given dst 4x4 matrix.
void shz_mat4x4_rotate_y(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the Y axis.
void shz_mat4x4_mult_transpose(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const shz_mat4x4_t *rhs) SHZ_NOEXCEPT
Multiplies the regular matrix, lhs, by the transpose of the matrix, rhs, storing the result into mat.
void shz_mat4x4_mult(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const shz_mat4x4_t *rhs) SHZ_NOEXCEPT
Multiplies two 4x4 matrices together, storing the result into a third.
void shz_mat4x4_apply_transpose(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Multiplies and accumulates the transposed src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_apply_transpose_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Multiplies and accumulates the transposed unaligned src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_init_identity(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix to the identity matrix as fast as possible.
void shz_mat4x4_init_rotation(shz_mat4x4_t *mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix about the given axis rotated by angle radians.
void shz_mat4x4_3x3_inverse_unscaled(const shz_mat4x4_t *mat4, shz_mat3x3_t *invmat3) SHZ_NOEXCEPT
Extracts and inverts the top-level, unscaled 3x3 of the given 4x4 matrix.
float shz_mat4x4_3x3_determinant(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the determinant of the given 4x4 matrix's internal top-level 3x3 matrix.
void shz_mat4x4_init_permutation_wxyz(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the matrix to to a permutation matrix, which reorders the components of transformed vecto...
void shz_mat4x4_init_rotation_xyz(shz_mat4x4_t *mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_apply_perspective(shz_mat4x4_t *m, float fov, float aspect, float znear) SHZ_NOEXCEPT
Multiplies and accumulates the perspective matrix constructed from the given values onto the given ma...
void shz_mat4x4_apply_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix with a rotation matrix whose orientation is given by a qu...
void shz_mat4x4_init_rotation_y(shz_mat4x4_t *mat, float yAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by yAngle radians over the Y-axis.
void shz_mat4x4_init_zero(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix with all 0s for its element values.
shz_vec2_t shz_mat4x4_transform_point2(const shz_mat4x4_t *mat, shz_vec2_t pt) SHZ_NOEXCEPT
Transforms a 2D point by a 4x4 matrix.
void shz_mat4x4_swap_rows(shz_mat4x4_t *mat, size_t row1, size_t row2) SHZ_NOEXCEPT
Swaps the 4D row vectors located at row1 and row2 within mat.
float shz_mat4x4_determinant(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the determinant of the given 4x4 matrix.
shz_vec3_t shz_mat4x4_transform_point3_transpose(const shz_mat4x4_t *mat, shz_vec3_t pt) SHZ_NOEXCEPT
Transforms a 3D point by the transpose of a 4x4 matrix.
void shz_mat4x4_init_outer_product(shz_mat4x4_t *mat, shz_vec4_t v1, shz_vec4_t v2) SHZ_NOEXCEPT
Initializes the given matrix to be the result from taking the outer product of the two given 4D vecto...
void shz_mat4x4_apply_screen(shz_mat4x4_t *m, float width, float height) SHZ_NOEXCEPT
Multiplies and accumulates the viewport matrix created with the given components ont othe given matri...
shz_vec2_t shz_mat4x4_transform_vec2(const shz_mat4x4_t *m, shz_vec2_t v) SHZ_NOEXCEPT
Transforms a 2D vector by a 4x4 matrix.
void shz_mat4x4_3x3_inverse(const shz_mat4x4_t *mat4, shz_mat3x3_t *invmat3) SHZ_NOEXCEPT
Extracts and inverts the top-level 3x3 (which may be scaled) of the given 4x4 matrix.
shz_vec2_t shz_mat4x4_transform_point2_transpose(const shz_mat4x4_t *mat, shz_vec2_t pt) SHZ_NOEXCEPT
Transforms a 2D point by the transpose of a 4x4 matrix.
void shz_mat4x4_init_rotation_x(shz_mat4x4_t *mat, float xAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix by xAngle radians over the X-axis.
float shz_mat4x4_trace(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the trace of the given 4x4 matrix.
void shz_mat4x4_apply_rotation_zxy(shz_mat4x4_t *mat, float zAngle, float xAngle, float yAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Z axis, then the X axis, then the Y axis by the given an...
void shz_mat4x4_apply_symmetric_skew(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix with a symmetric skew matrix formed from the given 3D vec...
void shz_mat4x4_apply_self(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates the given matrix onto itself.
void shz_mat4x4_init_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to a 3D scaling matrix with the given dimensions.
void shz_mat4x4_init_identity_safe(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix to the identity matrix, safely zeroing out NaN values.
void shz_mat4x4_init_ortho(shz_mat4x4_t *mat, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT
Initializes the given matrix to an orthographic projection matrix.
void shz_mat4x4_apply_rotation_xyz(shz_mat4x4_t *mat, float xAngle, float yAngle, float zAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the X axis, then the Y axis, then the Z axis by the given an...
void shz_mat4x4_translate(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D translation matrix with the given components.
shz_vec4_t shz_mat4x4_col(const shz_mat4x4_t *mat, size_t col) SHZ_NOEXCEPT
Extracts the col index as a 4D column vector from the given matrix.
void shz_mat4x4_apply_permutation_wxyz(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Multiplies and accumulates a permutation matrix, which reorders the components of transformed vectors...
void shz_mat4x4_rotate(shz_mat4x4_t *mat, float radians, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Multiplies and accumulates mat by the 3D rotation matrix formed by the given axis and angle.
void shz_mat4x4_apply_rotation_yxz(shz_mat4x4_t *mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Y axis, then the X axis, then the Z axis by the given an...
void shz_mat4x4_set_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Assigns only the 3D translation-related elements of the given matrix to the given values.
void shz_mat4x4_init_perspective(shz_mat4x4_t *mat, float fov, float aspect, float znear) SHZ_NOEXCEPT
Initializes the given matrix to a perspective projection matrix.
void shz_mat4x4_rotate_xyz(shz_mat4x4_t *mat, float xRadians, float yRadians, float zRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the X then Y then Z axes.
void shz_mat4x4_init_upper_triangular(shz_mat4x4_t *mat, float col1, shz_vec2_t col2, shz_vec3_t col3, shz_vec4_t col4) SHZ_NOEXCEPT
Initializes the given matrix to an upper triangular matrix whose nonzero entries have the given value...
void shz_mat4x4_apply_frustum(shz_mat4x4_t *m, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT
Multiplies and accumulates the frustum matrix constructed from the given values onto the given matrix...
void shz_mat4x4_rotate_yxz(shz_mat4x4_t *mat, float yRadians, float xRadians, float zRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Y then X then Z axes.
shz_quat_t shz_mat4x4_to_quat(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Converts the given 4x4 orientation matrix into a quaternion.
void shz_mat4x4_apply(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Multiplies and accumulates the src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_rotate_zxy(shz_mat4x4_t *mat, float zRadians, float xRadians, float yRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Z then X then Y axes.
shz_vec3_t shz_mat4x4_transform_vec3_transpose(const shz_mat4x4_t *m, shz_vec3_t v) SHZ_NOEXCEPT
Transforms a 3D vector the the transpose of a 4x4 matrix.
shz_vec4_t shz_mat4x4_transform_vec4(const shz_mat4x4_t *mat, shz_vec4_t in) SHZ_NOEXCEPT
Transforms a 4D vector by a 4x4 matrix.
void shz_mat4x4_swap_cols(shz_mat4x4_t *mat, size_t col1, size_t col2) SHZ_NOEXCEPT
Swaps the 4D column vectors located at col1 and col2 within mat.
void shz_mat4x4_init_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to a 3D translation matrix with the given coordinates.
void shz_mat4x4_apply_rotation_zyx(shz_mat4x4_t *mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT
Rotates the given transform matrix about the Z axis, then the Y axis, then the X axis by the given an...
void shz_mat4x4_copy(shz_mat4x4_t *dst, const shz_mat4x4_t *src) SHZ_NOEXCEPT
Copies the given src 4x4 matrix into the given dst 4x4 matrix.
void shz_mat4x4_rotate_z(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the Z axis.
void shz_mat4x4_apply_rotation_z(shz_mat4x4_t *mat, float zAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by zAngle radians about the Z-axis onto the given matrix...
void shz_mat4x4_set_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Assigns only the 3D scale-related elements of the given matrix to the given values.
void shz_mat4x4_apply_scale(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Multiplies and accumulates the scale-related elements of the given matrix by the given 3D components.
void shz_mat4x4_set_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Sets just the rotational component of the matrix to the orientation given by a quaternion,...
shz_vec2_t shz_mat4x4_transform_vec2_transpose(const shz_mat4x4_t *m, shz_vec2_t v) SHZ_NOEXCEPT
Transforms a 2D vector the the transpose of a 4x4 matrix.
void shz_mat4x4_apply_translation(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Adds the given 3D vector components to the translational values of the given matrix.
void shz_mat4x4_rotate_zyx(shz_mat4x4_t *mat, float zRadians, float yRadians, float xRadians) SHZ_NOEXCEPT
Multiplies and accumulates mat by 3D rotation matrices about the Z then Y then X axes.
void shz_mat4x4_set_row(shz_mat4x4_t *mat, size_t row, shz_vec4_t vec) SHZ_NOEXCEPT
Sets the values of mat at the given row to those of the 4D vector, vec.
void shz_mat4x4_mult_unaligned(shz_mat4x4_t *mat, const shz_mat4x4_t *lhs, const float rhs[16]) SHZ_NOEXCEPT
Multiplies two 4x4 matrices together, with the right handed matrix being unaligned,...
void shz_mat4x4_rotate_x(shz_mat4x4_t *mat, float radians) SHZ_NOEXCEPT
Multiplies and accumulates mat by a 3D rotation matrix about the X axis.
void shz_mat4x4_init_symmetric_skew(shz_mat4x4_t *mat, float x, float y, float z) SHZ_NOEXCEPT
Initializes the given matrix to be the symmetric skew of the given 3D vector components.
void shz_mat4x4_init_lookat(shz_mat4x4_t *mat, shz_vec3_t eye, shz_vec3_t center, shz_vec3_t up) SHZ_NOEXCEPT
Initializes the given matrix to a "lookAt" view matrix.
void shz_mat4x4_init_rotation_yxz(shz_mat4x4_t *mat, float yAngle, float xAngle, float zAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_init_diagonal(shz_mat4x4_t *mat, float x, float y, float z, float w) SHZ_NOEXCEPT
Initializes the given matrix to a diagonal matrix with the given 4 values.
shz_mat4x4_t shz_mat4x4
Alternate shz_mat4x4_t C typedef for those who hate POSIX style.
Definition shz_matrix.h:84
void shz_mat4x4_init_rotation_zyx(shz_mat4x4_t *mat, float zAngle, float yAngle, float xAngle) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given...
void shz_mat4x4_init_permutation_yzwx(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the matrix to to a permutation matrix, which reorders the components of transformed vecto...
shz_vec4_t shz_mat4x4_transform_vec4_transpose(const shz_mat4x4_t *m, shz_vec4_t v) SHZ_NOEXCEPT
Transforms a 4D vector the the transpose of a 4x4 matrix.
void shz_mat4x4_swap(shz_mat4x4_t *matA, shz_mat4x4_t *matB) SHZ_NOEXCEPT
Swaps the contents of the two given matrices, matA and matB.
shz_vec3_t shz_mat4x4_get_translation(const shz_mat4x4_t *mat) SHZ_NOEXCEPT
Returns the translational components from the 4th column as a 3D vector.
void shz_mat4x4_apply_rotation_x(shz_mat4x4_t *mat, float xAngle) SHZ_NOEXCEPT
Multiplies and accumulates a rotation matrix by xAngle radians about the X-axis onto the given matrix...
void shz_mat4x4_init_fill(shz_mat4x4_t *mat, float value) SHZ_NOEXCEPT
Initializes the given matrix with all elements assigned to the given value.
void shz_mat4x4_apply_unaligned(shz_mat4x4_t *dst, const float src[16]) SHZ_NOEXCEPT
Multiplies and accumulates the unaligned src 4x4 matrix onto the dst 4x4 matrix.
void shz_mat4x4_apply_ortho(shz_mat4x4_t *m, float left, float right, float bottom, float top, float znear, float zfar) SHZ_NOEXCEPT
Multiplies and accumulates the ortho matrix constructed from the given values onto the given matrix.
void shz_mat4x4_apply_lookat(shz_mat4x4_t *m, shz_vec3_t pos, shz_vec3_t target, shz_vec3_t up) SHZ_NOEXCEPT
Applies the 3D "lookAt" matrix constructed with the given vector components onto the given matrix.
shz_vec4_t shz_mat4x4_row(const shz_mat4x4_t *mat, size_t row) SHZ_NOEXCEPT
Extracts the row index as a 4D row vector from the given matrix.
bool shz_mat4x4_equal(const shz_mat4x4_t *SHZ_RESTRICT mat1, const shz_mat4x4_t *mat2) SHZ_NOEXCEPT
Returns true if the two matrices are equal, based on either absolute or relative tolerance.
shz_vec3_t shz_mat4x4_transform_vec3(const shz_mat4x4_t *m, shz_vec3_t v) SHZ_NOEXCEPT
Transforms a 3D vector by a 4x4 matrix.
void shz_mat4x4_apply_rotation(shz_mat4x4_t *mat, float angle, float xAxis, float yAxis, float zAxis) SHZ_NOEXCEPT
Rotates the given transform matrix about the arbitrary axis given by a 3D direction vector and angle ...
void shz_mat4x4_inverse(const shz_mat4x4_t *SHZ_RESTRICT mtrx, shz_mat4x4_t *SHZ_RESTRICT out) SHZ_NOEXCEPT
Computes the inverse of a 4x4 matrix.
void shz_mat4x4_init_one(shz_mat4x4_t *mat) SHZ_NOEXCEPT
Initializes the given matrix with all 1s for its element values.
void shz_mat4x4_init_rotation_quat(shz_mat4x4_t *m, shz_quat_t q) SHZ_NOEXCEPT
Initializes the given matrix to a 3D rotation matrix with its orientation given by a quaternion.
void shz_mat4x4_3x3(const shz_mat4x4_t *mat4, shz_mat3x3_t *mat3) SHZ_NOEXCEPT
Extracts the top-left 3x3 of the given 4D matrix.
void shz_mat4x4_inverse_block_triangular(const shz_mat4x4_t *mtx, shz_mat4x4_t *out) SHZ_NOEXCEPT
Computes the inverse of a 4x4 matrix in block-triangular form.
void shz_mat4x4_init_screen(shz_mat4x4_t *mat, float width, float height) SHZ_NOEXCEPT
Initializes the given matrix to the viewport matrix with the given dimenions.
void shz_mat4x4_set_col(shz_mat4x4_t *mat, size_t col, shz_vec4_t vec) SHZ_NOEXCEPT
Sets the values of mat at the given col to those of the 4D vector, vec.
Structure representing a 4x4 column-major matrix.
Definition shz_matrix.h:69
float elem[16]
< Inner convenience union.
Definition shz_matrix.h:71
shz_vec4_t pos
Access the last column of the matrix as a 1x4 vector.
Definition shz_matrix.h:78
shz_vec4_t col[4]
Access the matrix as an array of 4 1x4 column vectors.
Definition shz_matrix.h:73
shz_vec4_t forward
Access the third column of the matrix as a 1x4 vector.
Definition shz_matrix.h:77
shz_vec4_t left
< Named column vectors.
Definition shz_matrix.h:75
shz_vec4_t up
Access the second column of the matrix as a 1x4 vector.
Definition shz_matrix.h:76
float elem2D[4][4]
Access the matrix as a 2D array of 4x4 single-precision floats.
Definition shz_matrix.h:72