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
use cgmath::Matrix4;
pub fn perspective_transform(
near: f32,
far: f32,
left: f32,
right: f32,
bottom: f32,
top: f32,
) -> Matrix4<f32> {
let r1c1 = (2.0 * near) / (right - left);
let r1c3 = (right + left) / (right - left);
let r2c2 = (2.0 * near) / (top - bottom);
let r2c3 = (top + bottom) / (top - bottom);
let r3c3 = -(far + near) / (far - near);
let r3c4 = -(2.0 * far * near) / (far - near);
#[cfg_attr(rustfmt, rustfmt_skip)]
Matrix4::new(
r1c1, 0.0, 0.0, 0.0,
0.0, r2c2, 0.0, 0.0,
r1c3, r2c3, r3c3, -1.0,
0.0, 0.0, r3c4, 0.0
)
}
pub fn fov_perspective_transform(
field_of_view: f32,
aspect_ratio: f32,
near: f32,
far: f32,
) -> Matrix4<f32> {
let top = near * (field_of_view / 2.0).tan();
let bottom = -top;
let right = top * aspect_ratio;
let left = -right;
perspective_transform(near, far, left, right, bottom, top)
}
pub fn fov_near_distance(field_of_view: f32) -> f32 {
1.0 / (field_of_view / 2.0).tan()
}