File include/math/matrix/matrix_functions.h changed (mode: 100644) (index c9360cb..4e227d6) |
... |
... |
namespace math |
6 |
6 |
{ |
{ |
7 |
7 |
#define T_IMPL template<typename T> constexpr inline |
#define T_IMPL template<typename T> constexpr inline |
8 |
8 |
#define MATRIX4 matrix<4, 4, T> |
#define MATRIX4 matrix<4, 4, T> |
9 |
|
#define MATRIXN_IMP template<unsigned int size, typename T> constexpr inline matrix<size, size, T> |
|
10 |
|
#define VECTOR3 vector<3, T> |
|
|
9 |
|
#define MATRIXN_IMP \ |
|
10 |
|
template<unsigned int size, typename T> \ |
|
11 |
|
constexpr inline matrix<size, size, T> |
|
12 |
|
#define VEC3 vector<3, T> |
11 |
13 |
|
|
12 |
|
T_IMPL MATRIX4 lookAtLH (const VECTOR3& position, const VECTOR3& orientation, const VECTOR3& up); |
|
13 |
|
T_IMPL MATRIX4 lookAtRH (const VECTOR3& position, const VECTOR3& orientation, const VECTOR3& up); |
|
|
14 |
|
T_IMPL MATRIX4 look_at(const VEC3& pos, const VEC3& orient, const VEC3& up, |
|
15 |
|
const VEC3 &axis_mult = {1,1,1}); |
14 |
16 |
|
|
15 |
|
T_IMPL MATRIX4 lookAtUpLH(const VECTOR3& position, const VECTOR3& up, const VECTOR3& right); |
|
16 |
|
T_IMPL MATRIX4 lookAtUpRH(const VECTOR3& position, const VECTOR3& up, const VECTOR3& left); |
|
|
17 |
|
T_IMPL MATRIX4 projection(T fov, T aspect, T zNear, T zFar); |
17 |
18 |
|
|
18 |
|
T_IMPL MATRIX4 perspectiveRH(T fov, T aspect, T zNear, T zFar); |
|
19 |
|
T_IMPL MATRIX4 perspectiveLH(T fov, T aspect, T zNear, T zFar); |
|
|
19 |
|
T_IMPL MATRIX4 lookAtUpLH(const VEC3& position, const VEC3& up, const VEC3& right); |
|
20 |
|
T_IMPL MATRIX4 lookAtUpRH(const VEC3& position, const VEC3& up, const VEC3& left); |
20 |
21 |
|
|
21 |
|
T_IMPL matrix<3, 3, T> rotation(const VECTOR3& unit, T radians); |
|
|
22 |
|
T_IMPL matrix<3, 3, T> rotation(const VEC3& unit, T radians); |
22 |
23 |
|
|
23 |
24 |
T_IMPL matrix<3, 3, T> rotation(T x_angle, T y_angle, T z_angle); |
T_IMPL matrix<3, 3, T> rotation(T x_angle, T y_angle, T z_angle); |
24 |
25 |
|
|
|
... |
... |
namespace math |
29 |
30 |
#undef MATRIXN_IMP |
#undef MATRIXN_IMP |
30 |
31 |
#undef T_IMPL |
#undef T_IMPL |
31 |
32 |
#undef MATRIX4 |
#undef MATRIX4 |
32 |
|
#undef VECTOR3 |
|
|
33 |
|
#undef VEC3 |
33 |
34 |
} |
} |
File include/math/matrix/matrix_functions.hpp changed (mode: 100644) (index 01f7f3b..fc88d82) |
... |
... |
namespace math |
11 |
11 |
#define T_IMPL template<typename T> constexpr inline |
#define T_IMPL template<typename T> constexpr inline |
12 |
12 |
#define MATRIX4 matrix<4, 4, T> |
#define MATRIX4 matrix<4, 4, T> |
13 |
13 |
#define VECTOR3 vector<3, T> |
#define VECTOR3 vector<3, T> |
14 |
|
#define MATRIXN_IMP template<unsigned int size, typename T> constexpr inline matrix<size, size, T> |
|
15 |
|
|
|
16 |
|
T_IMPL MATRIX4 lookAtLH (const VECTOR3& position, const VECTOR3& orientation, const VECTOR3& up) |
|
17 |
|
{ |
|
18 |
|
const VECTOR3 point = normalize(orientation); |
|
19 |
|
const VECTOR3 right = normalize(cross(up, point)); |
|
20 |
|
const VECTOR3 bottom = cross(point, right); |
|
21 |
|
|
|
22 |
|
return {{{ right.x, bottom.x, point.x, 0 }, |
|
23 |
|
{ right.y, bottom.y, point.y, 0 }, |
|
24 |
|
{ right.z, bottom.z, point.z, 0 }, |
|
25 |
|
{ - dot(right, position), - dot(bottom, position), - dot(point, position), one<T> }}}; |
|
26 |
|
} |
|
27 |
|
T_IMPL MATRIX4 lookAtRH (const VECTOR3& position, const VECTOR3& orientation, const VECTOR3& up) |
|
28 |
|
{ |
|
29 |
|
const VECTOR3 point = normalize(orientation); |
|
30 |
|
const VECTOR3 left = normalize(cross(point, up)); |
|
31 |
|
const VECTOR3 bottom = cross(left, point); |
|
32 |
|
|
|
33 |
|
return {{{ left.x, bottom.x, - point.x, 0 }, |
|
34 |
|
{ left.y, bottom.y, - point.y, 0 }, |
|
35 |
|
{ left.z, bottom.z, - point.z, 0 }, |
|
36 |
|
{ - dot(left, position), - dot(bottom, position), dot(point, position), one<T> }}}; |
|
37 |
|
} |
|
|
14 |
|
#define MATRIXN_IMP \ |
|
15 |
|
template<unsigned int size, typename T> \ |
|
16 |
|
constexpr inline matrix<size, size, T> |
|
17 |
|
|
|
18 |
|
T_IMPL MATRIX4 |
|
19 |
|
look_at(const VECTOR3& pos, const VECTOR3& orient, const VECTOR3& up, |
|
20 |
|
const VECTOR3 &axis_mult) |
|
21 |
|
{ |
|
22 |
|
const VECTOR3 z = normalize(orient ) * axis_mult.z; |
|
23 |
|
const VECTOR3 x = normalize(cross(up, z)) * axis_mult.x; |
|
24 |
|
const VECTOR3 y = cross(z, x) * axis_mult.y; |
|
25 |
|
const VECTOR3 w = { dot(x, - pos), - dot(y, - pos), - dot(z, - pos) }; |
|
26 |
|
return {{{ x.x, y.x, z.x, 0 }, |
|
27 |
|
{ x.y, y.y, z.y, 0 }, |
|
28 |
|
{ x.z, y.z, z.z, 0 }, |
|
29 |
|
{ w.x, w.y, w.z, 1 }}}; |
|
30 |
|
} |
|
31 |
|
|
|
32 |
|
T_IMPL MATRIX4 |
|
33 |
|
projection(T fov_y, T aspect_x_to_y, T z_near, T z_far) |
|
34 |
|
{ |
|
35 |
|
const T y_scale = 1 / std::tan(fov_y / 2); |
|
36 |
|
const T x_scale = y_scale / aspect_x_to_y; |
|
37 |
|
const T z_scale = z_far / (z_far - z_near); |
|
38 |
|
const T z_shift = - z_scale * z_near; |
|
39 |
|
return {{{ x_scale, 0, 0, 0 }, |
|
40 |
|
{ 0, y_scale, 0, 0 }, |
|
41 |
|
{ 0, 0, z_scale, 1 }, |
|
42 |
|
{ 0, 0, z_shift, 0 }}}; |
|
43 |
|
} |
38 |
44 |
|
|
39 |
45 |
T_IMPL MATRIX4 lookAtUpLH(const VECTOR3& position, const VECTOR3& up, const VECTOR3& right) |
T_IMPL MATRIX4 lookAtUpLH(const VECTOR3& position, const VECTOR3& up, const VECTOR3& right) |
40 |
46 |
{ |
{ |
|
... |
... |
namespace math |
59 |
65 |
{ - dot(leftNormalized, position), - dot(bottom, position), dot(point, position), one<T> }}}; |
{ - dot(leftNormalized, position), - dot(bottom, position), dot(point, position), one<T> }}}; |
60 |
66 |
} |
} |
61 |
67 |
|
|
62 |
|
T_IMPL MATRIX4 perspectiveLH(T fov, T aspect, T zNear, T zFar) |
|
63 |
|
{ |
|
64 |
|
const T tanHalfFov = std::tan(fov / cast<T>(2)); |
|
65 |
|
|
|
66 |
|
return {{{ one<T> / (aspect * tanHalfFov), 0, 0, 0 }, |
|
67 |
|
{ 0, - one<T> / (tanHalfFov), 0, 0 }, |
|
68 |
|
{ 0, 0, zFar / (zFar - zNear), one<T> }, |
|
69 |
|
{ 0, 0, - (zFar * zNear) / (zFar - zNear), 0 }}}; |
|
70 |
|
} |
|
71 |
|
T_IMPL MATRIX4 perspectiveRH(T fov, T aspect, T zNear, T zFar) |
|
72 |
|
{ |
|
73 |
|
const T tanHalfFov = std::tan(fov / cast<T>(2)); |
|
74 |
|
|
|
75 |
|
return {{{ one<T> / (aspect * tanHalfFov), 0, 0, 0 }, |
|
76 |
|
{ 0, - one<T> / (tanHalfFov), 0, 0 }, |
|
77 |
|
{ 0, 0, zFar / (zNear - zFar), - one<T> }, |
|
78 |
|
{ 0, 0, - (zFar * zNear) / (zFar - zNear), 0 }}}; |
|
79 |
|
} |
|
80 |
68 |
|
|
81 |
69 |
T_IMPL matrix<3, 3, T> rotation(const VECTOR3& unit, T radians) |
T_IMPL matrix<3, 3, T> rotation(const VECTOR3& unit, T radians) |
82 |
70 |
{ |
{ |