hex_math/
matrix.rs

1use super::vector::{Vector, R3};
2
3pub type Mat4 = [f32; 16];
4
5pub fn perspective(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) -> Mat4 {
6    [
7        2.0 * near / (right - left),
8        0.0,
9        (right + left) / (right - left),
10        0.0,
11        0.0,
12        2.0 * near / (top - bottom),
13        (top + bottom) / (top - bottom),
14        0.0,
15        0.0,
16        0.0,
17        -(far + near) / (far - near),
18        -2.0 * far * near / (far - near),
19        0.0,
20        0.0,
21        -1.0,
22        0.0,
23    ]
24}
25
26pub fn orthographic(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) -> Mat4 {
27    [
28        2.0 / (right - left),
29        0.0,
30        0.0,
31        -(right + left) / (right - left),
32        0.0,
33        2.0 / (top - bottom),
34        0.0,
35        -(top + bottom) / (top - bottom),
36        0.0,
37        0.0,
38        -2.0 / (far - near),
39        -(far + near) / (far - near),
40        0.0,
41        0.0,
42        0.0,
43        1.0,
44    ]
45}
46pub fn look_at(from: R3, to: R3) -> Mat4 {
47    let up: R3 = [0.0, 1.0, 0.0].into();
48    let z = (from - to).norm();
49    let x = up.cross(z).norm();
50    let y = z.cross(x);
51
52    let t_x = -x.dot(from);
53    let t_y = -y.dot(from);
54    let t_z = -z.dot(from);
55
56    [
57        x[0], y[0], z[0], 0.0, x[1], y[1], z[1], 0.0, x[2], y[2], z[2], 0.0, t_x, t_y, t_z, 1.0,
58    ]
59}