1#ifndef TATOOINE_RENDERING_MATRICES_H
2#define TATOOINE_RENDERING_MATRICES_H
10template <
typename Real>
12 auto constexpr O = Real(0);
13 auto constexpr I = Real(1);
14 return Mat4<Real>{{I, O, O, x}, {O, I, O, y}, {O, O, I, z}, {O, O, O, I}};
17template <
typename Real>
19 auto constexpr O = Real(0);
20 auto constexpr I = Real(1);
22 {I, O, O, t.
x()}, {O, I, O, t.
y()}, {O, O, I, t.
z()}, {O, O, O, I}};
25template <
typename Real>
27 auto constexpr O = Real(0);
28 auto constexpr I = Real(1);
29 return Mat4<Real>{{s, O, O, O}, {O, s, O, O}, {O, O, s, O}, {O, O, O, I}};
32template <
typename Real>
34 auto constexpr O = Real(0);
35 auto constexpr I = Real(1);
36 return Mat4<Real>{{x, O, O, O}, {O, y, O, O}, {O, O, z, O}, {O, O, O, I}};
39template <
typename Real>
41 auto constexpr O = Real(0);
42 auto constexpr I = Real(1);
44 {s.
x(), O, O, O}, {O, s.
y(), O, O}, {O, O, s.
z(), O}, {O, O, O, I}};
47template <
typename Real>
50 Real
const s = gcem::sin(
angle);
51 Real
const c = gcem::cos(
angle);
52 auto constexpr O = Real(0);
53 auto constexpr I = Real(1);
54 return Mat4<Real>{{u * u + (v * v + w * w) * c, u * v * (1 - c) - w * s,
55 u * w * (1 - c) + v * s, O},
56 {u * v + (1 - c) + w * s, v * v * (u * u + w * w) * c,
57 v * w * (1 - c) + u * s, O},
58 {u * w + (1 - c) - v * s, v * v * (1 - c) + u * s,
59 w * w + (u * u + v * v) * c, O},
63template <
typename Real>
68template <
typename Real>
70 Real
const bottom, Real
const top,
71 Real
const near, Real
const far) {
72 auto constexpr O = Real(0);
73 auto constexpr I = Real(1);
74 auto const width = right - left;
75 auto const inv_width = 1 / width;
76 auto const height = top - bottom;
77 auto const inv_height = 1 / height;
78 auto const depth = far - near;
79 auto const inv_depth = 1 / depth;
81 return Mat4<Real>{{2 * inv_width, O, O, -(right + left) * inv_width},
82 {O, 2 * inv_height, O, -(top + bottom) * inv_height},
83 {O, O, -2 * inv_depth, -(far + near) * inv_depth},
88template <
typename Real>
93 auto constexpr O = Real(0);
94 auto constexpr I = Real(1);
96 auto const zaxis =
normalize(center - eye);
98 auto const yaxis =
cross(xaxis, zaxis);
99 return Mat4<Real>{{xaxis.x(), yaxis.x(), zaxis.x(), eye.
x()},
100 {xaxis.y(), yaxis.y(), zaxis.y(), eye.
y()},
101 {xaxis.z(), yaxis.z(), zaxis.z(), eye.
z()},
106template <
typename Real>
110 auto constexpr O = Real(0);
111 auto constexpr I = Real(1);
113 auto const zaxis =
normalize(center - eye);
115 auto const yaxis =
cross(xaxis, zaxis);
117 return Mat4<Real>{{xaxis.x(), xaxis.y(), xaxis.z(), -
dot(xaxis, eye)},
118 {yaxis.x(), yaxis.y(), yaxis.z(), -
dot(yaxis, eye)},
119 {zaxis.x(), zaxis.y(), zaxis.z(), -
dot(zaxis, eye)},
124template <
typename Real>
128 auto const zaxis =
normalize(eye - center);
130 auto const yaxis =
cross(zaxis, xaxis);
131 return Mat4<Real>{{xaxis.x(), yaxis.x(), zaxis.x(), eye.
x()},
132 {xaxis.y(), yaxis.y(), zaxis.y(), eye.
y()},
133 {xaxis.z(), yaxis.z(), zaxis.z(), eye.
z()},
134 {Real(0), Real(0), Real(0), Real(1)}};
138template <
typename Real>
142 auto constexpr O = Real(0);
143 auto constexpr I = Real(1);
145 auto const zaxis =
normalize(eye - center);
147 auto const yaxis =
cross(zaxis, xaxis);
148 return Mat4<Real>{{xaxis.x(), xaxis.y(), xaxis.z(), -
dot(xaxis, eye)},
149 {yaxis.x(), yaxis.y(), yaxis.z(), -
dot(yaxis, eye)},
150 {zaxis.x(), zaxis.y(), zaxis.z(), -
dot(zaxis, eye)},
154template <
typename Real>
161template <
typename Real>
169template <
typename Real>
174 auto constexpr O = Real(0);
175 auto constexpr I = Real(1);
177 auto const cos_pitch = gcem::cos(pitch);
178 auto const sin_pitch = gcem::sin(pitch);
179 auto const cos_yaw = gcem::cos(yaw);
180 auto const sin_yaw = gcem::sin(yaw);
184 normalize(
vec{sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch});
186 normalize(
vec{-sin_yaw * cos_pitch, sin_pitch, -cos_pitch * cos_yaw});
188 return Mat4<Real>{{xaxis.x(), yaxis.x(), zaxis.x(), eye.
x()},
189 {xaxis.y(), yaxis.y(), zaxis.y(), eye.
y()},
190 {xaxis.z(), yaxis.z(), zaxis.z(), eye.
z()},
195template <
typename Real>
200 auto constexpr O = Real(0);
201 auto constexpr I = Real(1);
203 auto const cos_pitch = gcem::cos(pitch);
204 auto const sin_pitch = gcem::sin(pitch);
205 auto const cos_yaw = gcem::cos(yaw);
206 auto const sin_yaw = gcem::sin(yaw);
208 auto const xaxis =
normalize(
vec{-cos_yaw, 0, sin_yaw});
210 normalize(
vec{sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch});
212 normalize(
vec{-sin_yaw * cos_pitch, sin_pitch, -cos_pitch * cos_yaw});
214 return Mat4<Real>{{xaxis.x(), xaxis.y(), xaxis.z(), -
dot(xaxis, eye)},
215 {yaxis.x(), yaxis.y(), yaxis.z(), -
dot(yaxis, eye)},
216 {zaxis.x(), zaxis.y(), zaxis.z(), -
dot(zaxis, eye)},
221template <
typename Real>
226 auto constexpr O = Real(0);
227 auto constexpr I = Real(1);
229 auto cos_pitch = gcem::cos(pitch);
230 auto sin_pitch = gcem::sin(pitch);
231 auto cos_yaw = gcem::cos(yaw);
232 auto sin_yaw = gcem::sin(yaw);
236 normalize(
vec{sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch});
238 normalize(
vec{sin_yaw * cos_pitch, -sin_pitch, cos_pitch * cos_yaw});
240 return Mat4<Real>{{xaxis.x(), yaxis.x(), zaxis.x(), eye.
x()},
241 {xaxis.y(), yaxis.y(), zaxis.y(), eye.
y()},
242 {xaxis.z(), yaxis.z(), zaxis.z(), eye.
z()},
247template <
typename Real>
252 auto constexpr O = Real(0);
253 auto constexpr I = Real(1);
255 auto const cos_pitch = gcem::cos(pitch);
256 auto const sin_pitch = gcem::sin(pitch);
257 auto const cos_yaw = gcem::cos(yaw);
258 auto const sin_yaw = gcem::sin(yaw);
260 auto const xaxis =
normalize(
vec{cos_yaw, 0, -sin_yaw});
262 normalize(
vec{sin_yaw * sin_pitch, cos_pitch, cos_yaw * sin_pitch});
264 normalize(
vec{sin_yaw * cos_pitch, -sin_pitch, cos_pitch * cos_yaw});
266 return Mat4<Real>{{xaxis.x(), xaxis.y(), xaxis.z(), -
dot(xaxis, eye)},
267 {yaxis.x(), yaxis.y(), yaxis.z(), -
dot(yaxis, eye)},
268 {zaxis.x(), zaxis.y(), zaxis.z(), -
dot(zaxis, eye)},
273template <
typename Real>
281template <
typename Real>
288template <
typename Real>
290 Real
const bottom, Real
const top,
291 Real
const near, Real
const far) {
292 auto constexpr O = Real(0);
293 auto constexpr I = Real(1);
295 auto const n2 = 2 * near;
296 auto const width = right - left;
297 auto const inv_width = 1 / width;
298 auto const xs = n2 * inv_width;
299 auto const xzs = (right + left) * inv_width;
301 auto const height = top - bottom;
302 auto const inv_height = 1 / height;
303 auto const ys = n2 * inv_height;
304 auto const yzs = (top + bottom) * inv_height;
306 auto const depth = far - near;
307 auto const inv_depth = 1 / depth;
308 auto const zs = -(far + near) * inv_depth;
309 auto const zt = -2 * near * far * inv_depth;
318template <
typename Real>
320 Real
const near, Real
const far) {
321 auto constexpr half = Real(1) / Real(2);
322 auto constexpr angles_to_radians_factor = Real(M_PI) / Real(180);
323 auto const fov_radians = fov_angles * angles_to_radians_factor;
324 auto const scale = gcem::tan(fov_radians * half) * near;
325 auto const right = scale * aspect_ratio;
326 auto const left = -right;
327 auto const top = scale;
328 auto const bottom = -top;
Definition: concepts.h:33
auto constexpr fps_look_at_matrix_left_hand(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw) -> Mat4< Real >
Can be used as transform matrix of an object.
Definition: matrices.h:170
auto constexpr inv_fps_look_at_matrix_left_hand(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw) -> Mat4< Real >
Can be used as view matrix of a camera.
Definition: matrices.h:196
auto constexpr look_at_matrix_right_hand(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0})
Can be used as transform matrix of an object.
Definition: matrices.h:125
auto constexpr fps_look_at_matrix(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw) -> Mat4< Real >
Can be used as transform matrix of an object.
Definition: matrices.h:274
auto constexpr rotation_matrix(Real angle, Real u, Real v, Real w) -> Mat4< Real >
Definition: matrices.h:48
auto constexpr inv_look_at_matrix(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0})
Can be used as view matrix of a camera.
Definition: matrices.h:162
auto constexpr look_at_matrix_left_hand(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0}) -> Mat4< Real >
Can be used as transform matrix of an object.
Definition: matrices.h:89
auto constexpr scale_matrix(Real s)
Definition: matrices.h:26
auto constexpr perspective_matrix(Real const fov_angles, Real const aspect_ratio, Real const near, Real const far)
Definition: matrices.h:319
auto constexpr orthographic_matrix(Real const left, Real const right, Real const bottom, Real const top, Real const near, Real const far)
Definition: matrices.h:69
auto constexpr inv_fps_look_at_matrix(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw)
Can be used as view matrix of a camera.
Definition: matrices.h:282
auto constexpr inv_look_at_matrix_right_hand(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0})
Can be used as view matrix of a camera.
Definition: matrices.h:139
auto constexpr translation_matrix(Real const x, Real const y, Real const z)
Definition: matrices.h:11
auto constexpr inv_fps_look_at_matrix_right_hand(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw) -> Mat4< Real >
Can be used as view matrix of a camera.
Definition: matrices.h:248
auto constexpr frustum_matrix(Real const left, Real const right, Real const bottom, Real const top, Real const near, Real const far)
Definition: matrices.h:289
auto constexpr look_at_matrix(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0}) -> Mat4< Real >
Definition: matrices.h:155
auto constexpr inv_look_at_matrix_left_hand(Vec3< Real > const &eye, Vec3< Real > const ¢er, Vec3< Real > const &up={0, 1, 0})
Can be used as view matrix of a camera.
Definition: matrices.h:107
auto constexpr fps_look_at_matrix_right_hand(Vec3< Real > const &eye, arithmetic auto const pitch, arithmetic auto const yaw) -> Mat4< Real >
Can be used as transform matrix of an object.
Definition: matrices.h:222
constexpr auto normalize(base_tensor< Tensor, T, N > const &t_in) -> vec< T, N >
Definition: tensor_operations.h:100
constexpr auto dot(base_tensor< Tensor0, T0, N > const &lhs, base_tensor< Tensor1, T1, N > const &rhs)
Definition: tensor_operations.h:120
constexpr auto angle(base_tensor< Tensor0, T0, N > const &v0, base_tensor< Tensor1, T1, N > const &v1)
Returns the angle of two normalized vectors.
Definition: tensor_operations.h:46
constexpr auto cross(base_tensor< Tensor0, T0, 3 > const &lhs, base_tensor< Tensor1, T1, 3 > const &rhs)
Definition: cross.h:9
auto constexpr x() const -> auto const &requires(N >=1)
Definition: vec.h:102
auto constexpr z() const -> auto const &requires(N >=3)
Definition: vec.h:122
auto constexpr y() const -> auto const &requires(N >=2)
Definition: vec.h:106