-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmat4.h
435 lines (392 loc) · 13.3 KB
/
mat4.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#ifndef MAT4_H
#define MAT4_H
#include <stdint.h>
/**
* Print a mat4 matrix to stderr
*
* @param {mat4} the matrix to dump
*/
void mat4_dump(float dst[16]);
/**
* Set a mat4 to the identity matrix
*
* @param {mat4} out the receiving matrix
*/
void mat4_identity(float dst[16]);
/**
* Copy the values from one mat4 to another
*
* @param {mat4} out the receiving matrix
* @param {mat4} a the source matrix
*/
void mat4_copy(float* dst, float* src);
/**
* Set the components of a mat4 to the given values
*
* @param {mat4} out the receiving matrix
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m02 Component in column 0, row 2 position (index 2)
* @param {Number} m03 Component in column 0, row 3 position (index 3)
* @param {Number} m10 Component in column 1, row 0 position (index 4)
* @param {Number} m11 Component in column 1, row 1 position (index 5)
* @param {Number} m12 Component in column 1, row 2 position (index 6)
* @param {Number} m13 Component in column 1, row 3 position (index 7)
* @param {Number} m20 Component in column 2, row 0 position (index 8)
* @param {Number} m21 Component in column 2, row 1 position (index 9)
* @param {Number} m22 Component in column 2, row 2 position (index 10)
* @param {Number} m23 Component in column 2, row 3 position (index 11)
* @param {Number} m30 Component in column 3, row 0 position (index 12)
* @param {Number} m31 Component in column 3, row 1 position (index 13)
* @param {Number} m32 Component in column 3, row 2 position (index 14)
* @param {Number} m33 Component in column 3, row 3 position (index 15)
*/
void mat4_set(float* dst, float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33);
/**
* Transpose the values of a mat4
*
* @param {mat4} out the receiving matrix
*/
void mat4_transpose(float* dst);
/**
* Inverts a mat4
*
* @param {mat4} out the receiving matrix
*/
void mat4_invert(float* dst);
/**
* Calculates the adjugate of a mat4
*
* @param {mat4} out the receiving matrix
*/
void mat4_adjoint(float* dst);
/**
* Calculates the determinant of a mat4
*
* @param {mat4} a the source matrix
* @returns {Number} determinant of a
*/
float mat4_determinant(float* dst);
/**
* Multiplies two mat4s
*
* @param {mat4} out the receiving matrix
* @param {mat4} b the first operand
*/
void mat4_multiply(float* dst, float* b);
/**
* Translate a mat4 by the given vector
*
* @param {mat4} out the receiving matrix
* @param {vec3} v vector to translate by
*/
void mat4_translate(float dst[16], float v[3]);
/**
* Translate a mat4 by the given flat 4 floats
*
* @param {mat4} out the receiving matrix
* @param {x} X translation
* @param {y} Y translation
* @param {z} Z translation
*/
void mat4_translatef(float dst[16], float x, float y, float z);
/**
* Scales the mat4 by the dimensions in the given vec3 not using vectorization
*
* @param {mat4} out the receiving matrix
* @param {vec3} v the vec3 to scale the matrix by
**/
void mat4_scale(float* dst, float* v);
/**
* Rotates a mat4 by the given angle around the given axis
*
* @param {mat4} out the receiving matrix
* @param {Number} rad the angle to rotate the matrix by
* @param {vec3} axis the axis to rotate around
*/
void mat4_rotate(float* dst, float rad, float* axis);
/**
* Rotates a matrix by the given angle around the X axis
*
* @param {mat4} out the receiving matrix
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_rotateX(float* dst, float rad);
/**
* Rotates a matrix by the given angle around the Y axis
*
* @param {mat4} out the receiving matrix
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_rotateY(float* dst, float rad);
/**
* Rotates a matrix by the given angle around the Z axis
*
* @param {mat4} out the receiving matrix
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_rotateZ(float* dst, float rad);
/**
* Initializes a matrix from a vector translation
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_translate(dest, vec);
*
* @param {mat4} out mat4 receiving operation result
* @param {vec3} v Translation vector
*/
void mat4_fromTranslation(float* dst, float* v);
/**
* Initializes a matrix from a vector scaling
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_scale(dest, vec);
*
* @param {mat4} out mat4 receiving operation result
* @param {vec3} v Scaling vector
*/
void mat4_fromScaling(float* dst, float* v);
/**
* Initializes a matrix from a given angle around a given axis
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_rotate(dest, rad, axis);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @param {vec3} axis the axis to rotate around
*/
void mat4_fromRotation(float* dst, float rad, float* axis);
/**
* Initializes a matrix from the given angle around the X axis
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_rotateX(dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_fromXRotation(float* dst, float rad);
/**
* Initializes a matrix from the given angle around the Y axis
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_rotateY(dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_fromYRotation(float* dst, float rad);
/**
* Initializes a matrix from the given angle around the Z axis
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_rotateZ(dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
*/
void mat4_fromZRotation(float* dst, float rad);
/**
* Initializes a matrix from a quaternion rotation and vector translation
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_translate(dest, vec);
* float quatMat = mat4.create();
* quat4_toMat4(quat, quatMat);
* mat4_multiply(dest, quatMat);
*
* @param {mat4} out mat4 receiving operation result
* @param {quat4} q Rotation quaternion
* @param {vec3} v Translation vector
*/
void mat4_fromRotationTranslation(float* dst, float* q, float* v);
/**
* Creates a new mat4 from a dual quat.
*
* @param {mat4} out Matrix
* @param {quat2} a Dual Quaternion
*/
void mat4_fromQuat2(float* dst, float* a);
/**
* Returns the translation vector component of a transformation
* matrix. If a matrix is built with fromRotationTranslation,
* the returned vector will be the same as the translation vector
* originally supplied.
* @param {vec3} out Vector to receive translation component
* @param {mat4} mat Matrix to be decomposed (input)
*/
void mat4_getTranslation(float* dst, float* mat);
/**
* Returns the scaling factor component of a transformation
* matrix. If a matrix is built with fromRotationTranslationScale
* with a normalized Quaternion paramter, the returned vector will be
* the same as the scaling vector
* originally supplied.
* @param {vec3} out Vector to receive scaling factor component
* @param {mat4} mat Matrix to be decomposed (input)
*/
void mat4_getScaling(float* dst, float* mat);
/**
* Returns a quaternion representing the rotational component
* of a transformation matrix. If a matrix is built with
* fromRotationTranslation, the returned quaternion will be the
* same as the quaternion originally supplied.
* @param {quat} out Quaternion to receive the rotation component
* @param {mat4} mat Matrix to be decomposed (input)
*/
void mat4_getRotation(float* dst, float* mat);
/**
* Initializes a matrix from a quaternion rotation, vector translation and vector scale
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_translate(dest, vec);
* float quatMat = mat4_create();
* quat4_toMat4(quat, quatMat);
* mat4_multiply(dest, quatMat);
* mat4_scale(dest, scale)
*
* @param {mat4} out mat4 receiving operation result
* @param {quat4} q Rotation quaternion
* @param {vec3} v Translation vector
* @param {vec3} s Scaling vector
*/
void mat4_fromRotationTranslationScale(float* dst, float* q, float* v, float* s);
/**
* Initializes a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
* This is equivalent to (but much faster than):
*
* mat4_identity(dest);
* mat4_translate(dest, vec);
* mat4_translate(dest, origin);
* float quatMat = mat4_create();
* quat4_toMat4(quat, quatMat);
* mat4_multiply(dest, quatMat);
* mat4_scale(dest, scale)
* mat4_translate(dest, negativeOrigin);
*
* @param {mat4} out mat4 receiving operation result
* @param {quat4} q Rotation quaternion
* @param {vec3} v Translation vector
* @param {vec3} s Scaling vector
* @param {vec3} o The origin vector around which to scale and rotate
*/
void mat4_fromRotationTranslationScaleOrigin(float* dst, float* q, float* v, float* s, float* o);
/**
* Calculates a 4x4 matrix from the given quaternion
*
* @param {mat4} out mat4 receiving operation result
* @param {quat} q Quaternion to create matrix from
*
* @returns {mat4} out
*/
void mat4_fromQuat(float* dst, float* q);
/**
* Generates a frustum matrix with the given bounds
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {Number} left Left bound of the frustum
* @param {Number} right Right bound of the frustum
* @param {Number} bottom Bottom bound of the frustum
* @param {Number} top Top bound of the frustum
* @param {Number} near Near bound of the frustum
* @param {Number} far Far bound of the frustum
*/
void mat4_frustum(float* dst, float left, float right, float bottom, float top, float near, float far);
/**
* Generates a perspective projection matrix with the given bounds.
* Passing null/undefined/no value for far will generate infinite projection matrix.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} fovy Vertical field of view in radians
* @param {number} aspect Aspect ratio. typically viewport width/height
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum, can be 0 or FLT_MAX
*/
void mat4_perspective(float* dst, float fovy, float aspect, float near, float far);
/**
* Generates a orthogonal projection matrix with the given bounds
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} left Left bound of the frustum
* @param {number} right Right bound of the frustum
* @param {number} bottom Bottom bound of the frustum
* @param {number} top Top bound of the frustum
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum
*/
void mat4_ortho(float* dst, float left, float right, float bottom, float top, float near, float far);
/**
* Generates a look-at matrix with the given eye position, focal point, and up axis.
* If you want a matrix that actually makes an object look at another object, you should use targetTo instead.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {vec3} eye Position of the viewer
* @param {vec3} center Point the viewer is looking at
* @param {vec3} up vec3 pointing up
* @returns {mat4} out
*/
void mat4_lookAt(float* dst, float* eye, float* center, float* up);
/**
* Generates a matrix that makes something look at something else.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {vec3} eye Position of the viewer
* @param {vec3} center Point the viewer is looking at
* @param {vec3} up vec3 pointing up
* @returns {mat4} out
*/
void mat4_targetTo(float* dst, float* eye, float* target, float* up);
/**
* Returns Frobenius norm of a mat4
*
* @param {mat4} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
float mat4_frob(float* a);
/**
* Adds two mat4's
*
* @param {mat4} out the receiving matrix
* @param {mat4} b the second operand
*/
void mat4_add(float* dst, float* b);
/**
* Subtracts matrix b from matrix a
*
* @param {mat4} out the receiving matrix
* @param {mat4} b the second operand
*/
void mat4_subtract(float* dst, float* b);
/**
* Multiply each element of the matrix by a scalar.
*
* @param {mat4} out the receiving matrix
* @param {Number} b amount to scale the matrix's elements by
*/
void mat4_multiplyScalar(float* dst, float b);
/**
* Adds two mat4's after multiplying each element of the second operand by a scalar value.
*
* @param {mat4} out the receiving vector
* @param {mat4} b the second operand
* @param {Number} scale the amount to scale b's elements by before adding
*/
void mat4_multiplyScalarAndAdd(float* dst, float* b, float scale);
/**
* Returns whether or not the matrices have exactly the same elements.
*
* @param {mat4} a The first matrix.
* @param {mat4} b The second matrix.
* @returns {uint8_t} True if the matrices are equal, false otherwise.
*/
uint8_t mat4_equals(float* a, float* b);
#endif