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
use nalgebra::{Point3, Vector3};
use ray::Ray;
use std::f64::consts;
#[derive(Copy, Clone)]
pub struct Camera {
origin: Point3<f64>,
lower_left_corner: Vector3<f64>,
horizontal: Vector3<f64>,
vertical: Vector3<f64>,
}
impl Camera {
pub fn new(vert_fov: f64, aspect_ratio: f64) -> Camera {
let theta = vert_fov * consts::PI / 180.0;
let half_height = (theta / 2.0).tan();
let half_width = aspect_ratio * half_height;
Camera {
origin: Point3::new(0.0, 0.0, 0.0),
lower_left_corner: Vector3::new(-half_width, -half_height, -1.0),
horizontal: Vector3::new(2.0 * half_width, 0.0, 0.0),
vertical: Vector3::new(0.0, 2.0 * half_height, 0.0),
}
}
pub fn get_ray(&self, u: f64, v: f64) -> Ray {
let direction = self.lower_left_corner + u * self.horizontal + v * self.vertical;
Ray::new(self.origin.clone(), direction)
}
}