summaryrefslogtreecommitdiff
path: root/racer-tracer/src/camera.rs
blob: 31c23917e1d5d62ff4dbe9f2f2cb7492e78fde50 (plain)
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
use crate::image::Image;
use crate::ray::Ray;
use crate::util::degrees_to_radians;
use crate::vec3::Vec3;

#[derive(Clone)]
pub struct Camera {
    pub viewport_height: f64,
    pub viewport_width: f64,
    pub focal_length: f64,
    pub origin: Vec3,
    pub horizontal: Vec3,
    pub vertical: Vec3,
    pub upper_left_corner: Vec3,
    pub forward: Vec3,
    pub right: Vec3,
    pub up: Vec3,
}

impl Camera {
    pub fn new(
        look_from: Vec3,
        look_at: Vec3,
        up: Vec3,
        vfov: f64,
        image: &Image,
        focal_length: f64,
    ) -> Camera {
        let h = (degrees_to_radians(vfov) / 2.0).tan();
        let viewport_height = 2.0 * h;
        let viewport_width = image.aspect_ratio * viewport_height;

        let forward = (look_from - look_at).unit_vector();
        let right = up.cross(&forward).unit_vector();
        let up = forward.cross(&right);

        let horizontal = viewport_width * right;
        let vertical = viewport_height * up;

        Camera {
            viewport_height,
            viewport_width,
            focal_length,
            origin: look_from,
            horizontal,
            vertical,
            upper_left_corner: look_from + vertical / 2.0 - horizontal / 2.0 - forward,
            forward,
            right,
            up,
        }
    }

    pub fn get_ray(&self, u: f64, v: f64) -> Ray {
        Ray::new(
            self.origin,
            self.upper_left_corner + u * self.horizontal - v * self.vertical - self.origin,
        )
    }

    // TODO: Add support for rotation

    // TODO: Use forward facing vector
    pub fn go_forward(&mut self, go: f64) {
        self.origin += self.forward * go;

        self.upper_left_corner =
            self.origin + self.vertical / 2.0 - self.horizontal / 2.0 - self.forward;
    }

    // TODO: Use right facing vector
    pub fn go_right(&mut self, go: f64) {
        self.origin += self.right * go;

        self.upper_left_corner =
            self.origin + self.vertical / 2.0 - self.horizontal / 2.0 - self.forward;
    }
}