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
use crate::internal::*;
/// Abstraction for the point of view chosen to take the scene from
#[derive(Clone)]
pub struct Camera {
/// Vertex of the cone of the field of view
orig: Vec3,
/// Bottom left corner of the view
low_left: Vec3,
/// Direction of the bottom edge of the view
horiz: Vec3,
/// Direction of the left edge of the view
vert: Vec3,
}
impl Camera {
/// Build a camera from its position relative to the origin of the space
///
/// eye: vertex of the field of view
///
/// vert: vertical direction
///
/// vfov: field of view (degrees)
///
/// ratio: width/height aspect ratio
pub fn new_absolute(
eye: Vec3,
target: Vec3,
vert: Vec3,
vfov: f64,
ratio: f64,
) -> Self {
let theta = vfov * std::f64::consts::PI / 180.;
let half_hgt = (theta / 2.).tan();
let half_wth = ratio * half_hgt;
let w = (eye - target).unit();
let u = vert.cross(w);
let v = w.cross(u);
Camera {
orig: eye,
low_left: eye - u * half_wth - v * half_hgt - w,
horiz: u * 2. * half_wth,
vert: v * 2. * half_hgt,
}
}
/// Build a camera from its position relative to the point aimed at
///
/// angle: rotation around the target (degrees)
///
/// rise: angle above target (degrees, use negative value for low-angle shot)
/// dist: distance to target
/// tilt: 0 for vertical field of view
/// aperture: vertical field of view (degrees)
/// ratio: width/height aspect ratio
pub fn new_relative(
target: Vec3,
angle: f64,
rise: f64,
dist: f64,
tilt: f64,
aperture: f64,
ratio: f64,
) -> Self {
let theta = aperture * std::f64::consts::PI / 180.;
let half_hgt = (theta / 2.).tan();
let half_wth = ratio * half_hgt;
let eye = {
let angle_rad = angle * std::f64::consts::PI / 180.;
let rise_rad = rise * std::f64::consts::PI / 180.;
let x = angle_rad.sin();
let z = angle_rad.cos();
let y = rise_rad.sin();
Vec3(x, y, z).unit() * dist + target
};
let w = (eye - target).unit();
let vert = {
let tilt_rad = tilt * std::f64::consts::PI / 180.;
let up = Vec3(0.0, 1.0, 0.0);
let horiz = up.cross(w);
up * tilt_rad.cos() + horiz * tilt_rad.sin()
};
let u = vert.cross(w);
let v = w.cross(u);
Camera {
orig: eye,
low_left: eye - u * half_wth - v * half_hgt - w,
horiz: u * 2. * half_wth,
vert: v * 2. * half_hgt,
}
}
/// Calculate the direction of a ray given by the position on the image of its destination
pub fn get_ray(&self, u: f64, v: f64) -> Ray {
Ray {
orig: self.orig,
dir: self.low_left + self.horiz * u + self.vert * v - self.orig,
}
}
}