-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmath.h
529 lines (426 loc) · 13.5 KB
/
vmath.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2021-2024 Chris Dragan
#pragma once
#ifdef _WIN32
# ifdef min
# undef min
# endif
# ifdef max
# undef max
# endif
#endif
namespace vmath {
constexpr double pi_double = 3.141592653589793;
constexpr float pi = static_cast<float>(pi_double);
constexpr float pi_squared = static_cast<float>(pi_double * pi_double);
constexpr float pi_half = static_cast<float>(pi_double / 2);
constexpr float two_pi = static_cast<float>(pi_double * 2);
constexpr float to_radians = static_cast<float>(pi_double / 180.0);
constexpr float to_degrees = static_cast<float>(180.0 / pi_double);
template<typename T>
static inline constexpr T radians(T deg)
{
return deg * T{to_radians};
}
template<typename T>
static inline constexpr T degrees(T rad)
{
return rad * T{to_degrees};
}
template<unsigned dim> struct vec;
struct quat;
struct mat3;
struct mat4;
typedef struct vec<2> vec2;
typedef struct vec<3> vec3;
typedef struct vec<4> vec4;
template<>
struct vec<2> {
union {
struct {
alignas(2 * sizeof(float))
float x;
float y;
};
alignas(2 * sizeof(float)) float data[2];
};
vec() = default;
constexpr explicit vec(float ax)
: x(ax), y(ax) { }
constexpr vec(float ax, float ay)
: x(ax), y(ay) { }
constexpr explicit vec(const float* ptr)
: x(ptr[0]), y(ptr[1]) { }
constexpr explicit vec(const vec3& v);
constexpr explicit vec(const vec4& v);
constexpr float& operator[](unsigned i) {
return data[i];
}
constexpr const float& operator[](unsigned i) const {
return data[i];
}
constexpr bool operator==(const vec& v) const {
return (x == v.x) && (y == v.y);
}
constexpr bool operator!=(const vec& v) const {
return (x != v.x) || (y != v.y);
}
constexpr vec operator-() const {
return vec(-x, -y);
}
constexpr vec& operator+=(const vec& v) {
x += v.x;
y += v.y;
return *this;
}
constexpr vec& operator-=(const vec& v) {
x -= v.x;
y -= v.y;
return *this;
}
constexpr vec& operator*=(float c) {
x *= c;
y *= c;
return *this;
}
constexpr vec& operator/=(float c) {
x /= c;
y /= c;
return *this;
}
constexpr vec& operator*=(const vec& v) {
x *= v.x;
y *= v.y;
return *this;
}
constexpr vec& operator/=(const vec& v) {
x /= v.x;
y /= v.y;
return *this;
}
};
template<>
struct vec<3> {
union {
struct {
alignas(4 * sizeof(float))
float x;
float y;
float z;
};
alignas(4 * sizeof(float)) float data[3];
};
vec() = default;
constexpr explicit vec(float ax)
: x(ax), y(ax), z(ax) { }
constexpr vec(float ax, float ay, float az)
: x(ax), y(ay), z(az) { }
constexpr explicit vec(const float* ptr)
: x(ptr[0]), y(ptr[1]), z(ptr[2]) { }
constexpr explicit vec(const vec2& v)
: x(v.x), y(v.y), z(0) { }
constexpr explicit vec(const vec2& v, float az)
: x(v.x), y(v.y), z(az) { }
constexpr explicit vec(const vec4& v);
constexpr float& operator[](unsigned i) {
return data[i];
}
constexpr const float& operator[](unsigned i) const {
return data[i];
}
constexpr bool operator==(const vec& v) const {
return (x == v.x) && (y == v.y) && (z == v.z);
}
constexpr bool operator!=(const vec& v) const {
return (x != v.x) || (y != v.y) || (z != v.z);
}
constexpr vec operator-() const {
return vec(-x, -y, -z);
}
vec& operator+=(const vec& v);
vec& operator-=(const vec& v);
vec& operator*=(const float c);
vec& operator/=(const float c);
vec& operator*=(const vec& v);
vec& operator/=(const vec& v);
};
template<>
struct vec<4> {
union {
struct {
alignas(4 * sizeof(float))
float x;
float y;
float z;
float w;
};
alignas(4 * sizeof(float)) float data[4];
};
vec() = default;
constexpr explicit vec(float ax)
: x(ax), y(ax), z(ax), w(ax) { }
constexpr vec(float ax, float ay, float az)
: x(ax), y(ay), z(az), w(1) { }
constexpr vec(float ax, float ay, float az, float aw)
: x(ax), y(ay), z(az), w(aw) { }
constexpr explicit vec(const float* ptr)
: x(ptr[0]), y(ptr[1]), z(ptr[2]), w(ptr[3]) { }
constexpr explicit vec(const vec2& v)
: x(v.x), y(v.y), z(0), w(1) { }
constexpr vec(const vec2& v, float az)
: x(v.x), y(v.y), z(az), w(1) { }
constexpr vec(const vec2& v, float az, float aw)
: x(v.x), y(v.y), z(az), w(aw) { }
constexpr explicit vec(const vec3& v)
: x(v.x), y(v.y), z(v.z), w(1) { }
constexpr vec(const vec3& v, float aw)
: x(v.x), y(v.y), z(v.z), w(aw) { }
constexpr float& operator[](unsigned i) {
return data[i];
}
constexpr const float& operator[](unsigned i) const {
return data[i];
}
vec& operator+=(const vec& v);
vec& operator-=(const vec& v);
vec& operator*=(const float c);
vec& operator/=(const float c);
vec& operator*=(const vec& v);
vec& operator/=(const vec& v);
bool operator==(const vec& v) const;
bool operator!=(const vec& v) const;
vec operator-() const;
};
constexpr inline vec<2>::vec(const vec3& v)
: x(v.x), y(v.y) { }
constexpr inline vec<2>::vec(const vec4& v)
: x(v.x), y(v.y) { }
constexpr inline vec<3>::vec(const vec4& v)
: x(v.x), y(v.y), z(v.z) { }
template<unsigned dim>
inline vec<dim> operator+(vec<dim> v1, const vec<dim>& v2)
{
v1 += v2;
return v1;
}
template<unsigned dim>
inline vec<dim> operator-(vec<dim> v1, const vec<dim>& v2)
{
v1 -= v2;
return v1;
}
template<unsigned dim>
inline vec<dim> operator*(vec<dim> v, float c)
{
v *= c;
return v;
}
template<unsigned dim>
inline vec<dim> operator*(float c, vec<dim> v)
{
v *= c;
return v;
}
template<unsigned dim>
inline vec<dim> operator/(vec<dim> v, float c)
{
v /= c;
return v;
}
template<unsigned dim>
inline vec<dim> operator*(vec<dim> v1, const vec<dim>& v2)
{
v1 *= v2;
return v1;
}
template<unsigned dim>
inline vec<dim> operator/(vec<dim> v1, const vec<dim>& v2)
{
v1 /= v2;
return v1;
}
template<unsigned dim>
float dot_product(const vec<dim>& v1, const vec<dim>& v2);
vec3 cross_product(const vec3& v1, const vec3& v2);
template<unsigned dim>
float length(const vec<dim>& v);
template<unsigned dim>
float rlength(const vec<dim>& v);
template<unsigned dim>
vec<dim> normalize(const vec<dim>& v);
template<unsigned dim>
vec<dim> min(const vec<dim>& v1, const vec<dim>& v2);
template<unsigned dim>
vec<dim> max(const vec<dim>& v1, const vec<dim>& v2);
struct quat {
union {
struct {
alignas(4 * sizeof(float))
float x;
float y;
float z;
float w;
};
alignas(4 * sizeof(float)) float data[4];
};
quat() = default;
constexpr quat(float ax, float ay, float az, float aw)
: x(ax), y(ay), z(az), w(aw) { }
constexpr explicit quat(const float* ptr)
: x(ptr[0]), y(ptr[1]), z(ptr[2]), w(ptr[3]) { }
quat(const vec3& axis, float angle_radians);
explicit quat(const mat3& rot_mtx);
explicit quat(const mat4& rot_mtx);
static quat from_euler(const vec3& euler_xyz);
constexpr float& operator[](unsigned i) {
return data[i];
}
constexpr const float& operator[](unsigned i) const {
return data[i];
}
quat& operator*=(const quat& q);
bool operator==(const quat& q) const;
bool operator!=(const quat& q) const;
quat operator-() const;
vec3 rotate(const vec3& v) const;
};
inline quat operator*(quat q1, const quat& q2)
{
q1 *= q2;
return q1;
}
quat conjugate(const quat& q);
quat normalize(const quat& q);
// mat3 represents a 3x3 matrix with column-major layout
// The storage for mat3 is actually mat3x4 (3 columns, 4 rows) to match
// how we pass mat3 data to shaders to satisfy alignment requirements
struct mat3 {
union {
struct {
alignas(4 * sizeof(float))
float a00;
float a10, a20, a30;
float a01, a11, a21, a31;
float a02, a12, a22, a32;
};
alignas(4 * sizeof(float)) float data[12];
};
mat3() = default;
explicit mat3(const mat4& mtx);
explicit mat3(const float* ptr);
explicit mat3(const quat& q);
void set_identity();
static mat3 identity();
};
// Transposes a matrix
mat3 transpose(const mat3& mtx);
// Creates an inverse matrix
mat3 inverse(const mat3& mtx);
// mat4 represents a 4x4 matrix with column-major layout
// The matrices are typically used in vec * mat multiplication with row vectors
// (NOT mat * vec with column vectors!), and column-major layout simplifies operations with vector
// instructions (e.g. SSE).
struct mat4 {
union {
struct {
alignas(4 * sizeof(float))
float a00;
float a10, a20, a30;
float a01, a11, a21, a31;
float a02, a12, a22, a32;
float a03, a13, a23, a33;
};
alignas(4 * sizeof(float)) float data[16];
};
mat4() = default;
explicit mat4(const mat3& mtx);
explicit mat4(const float* ptr);
explicit mat4(const quat& q);
void set_identity();
static mat4 identity();
};
mat4 operator*(const mat4& m1, const mat4& m2);
// Multiplies a row vector by a matrix, i.e. vec * mat
// All matrices are constructed with assumption that this operation is used
// to perform transformations.
vec4 operator*(const vec4& v, const mat4& mtx);
// Multiplies a matrix by a column vector, i.e. mat * vec
// This operation is rarely used, the vec * mat operation is preferred, all matrices
// assume that vec * mat is used.
vec4 operator*(const mat4& mtx, const vec4& v);
// Transposes a matrix
mat4 transpose(const mat4& mtx);
// Creates perspective projection transform matrix in the following form:
//
// [ xf 0 0 0 ]
// [ 0 yf 0 0 ]
// [ 0 0 zf 1 ]
// [ 0 0 wf 0 ]
//
// This projection matrix is for left-handed coordinate system and is used for vec * mat
// multiplications with row vectors (NOT mat * vec with column vectors!).
//
// Input:
// - aspect - aspect ratio of the rendered view rectangle
// - fov_radians - horizontal field of view, in radians
// - near_plane - distance of the near plane, in view space coordinates
// - far_plane - distance of the far plane, in view space coordinates
//
// The matrix contains the following values (aside from 0s and 1s):
// - xf - multiplication factor for x coordinate = 1 / (aspect * tan(fov_radians / 2))
// - yf - multiplication factor for y coordinate = 1 / tan(fov_radians / 2)
// - zf - multiplication factor for z coordinate = -near_plane / (far_plane - near_plane)
// - wf - multiplication factor for w coordinate = (far_plane * near_plane) / (far_plane - near_plane)
mat4 projection(float aspect, float fov_radians, float near_plane, float far_plane);
// Creates a vector containing non-0/1 factors of the perspective projection transform matrix
// The returned vector is: [xf, yf, zf, wf]
// See projection() for the meaning of inputs and output factors.
// This form simplifies calculations in shaders.
vec4 projection_vector(float aspect, float fov_radians, float near_plane, float far_plane);
// Creates orthographic projection matrix in the form of:
//
// [ xf 0 0 0 ]
// [ 0 yf 0 0 ]
// [ 0 0 zf 0 ]
// [ 0 0 wf 1 ]
//
// Input:
// - aspect - aspect ratio of the rendered view rectangle
// - height - height of view, in view space coordinates
// - near_plane - distance of the near plane, in view space coordinates
// - far_plane - distance of the far plane, in view space coordinates
//
// The matrix contains the following values (aside from 0s and 1s):
// - xf - multiplication factor for x coordinate = 1 / (aspect * height / 2)
// - yf - multiplication factor for y coordinate = 1 / (height / 2)
// - zf - multiplication factor for z coordinate = -1 / (far_plane - near_plane)
// - wf - multiplication factor for w coordinate = far_plane / (far_plane - near_plane)
mat4 ortho(float aspect, float height, float near_plane, float far_plane);
// Creates a vector containing non-0/1 factors of the orthographic projection transform matrix
// The returned vector is: [xf, yf, zf, wf]
// See ortho() for the meaning of inputs and output factors.
// This form simplifies calculations in shaders.
vec4 ortho_vector(float aspect, float height, float near_plane, float far_plane);
// Creates a look-at view transform matrix, used for transforming points from world coordinate space
// into view coordinate space.
//
// Input:
// - eye_pos - position of the camera's "eye"
// - target - point at which the camera is looking
// - up - vector pointing in the "up" direction
mat4 look_at(const vec3& eye_pos, const vec3& target, const vec3& up);
// Creates a translation transform matrix
mat4 translate(float x, float y, float z);
// Creates a scale transform matrix
mat4 scale(float x, float y, float z);
// Creates a translation transform matrix
inline mat4 translate(const vec3& v)
{
return translate(v.x, v.y, v.z);
}
// Creates a scale transform matrix
inline mat4 scale(const vec3& v)
{
return scale(v.x, v.y, v.z);
}
} // namespace vmath