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