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