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

use crate::prelude::*;

/// A traditional pinhole camera with an origin, center and fov.
pub struct Pinhole {
    origin          : PTF3,
    center          : PTF3,

    fov             : PTF,
}

impl Camera3D for Pinhole {

    fn new() -> Self {

        let origin = PTF3::new(0.0, 0.0, 3.0);
        let center = PTF3::new(0.0, 0.0, 0.0);

        Self {
            origin,
            center,

            fov     : 80.0,
        }
    }

    fn set(&mut self, origin: PTF3, center: PTF3) {
        self.origin = origin;
        self.center = center;
    }

    fn set_fov(&mut self, fov: PTF) {
        self.fov = fov;
    }


    #[inline(always)]
    fn gen_ray(&self, p: Vector2<PTF>, offset: PTF2, width: PTF, height: PTF) -> [Vector3<PTF>; 2] {
        let ratio = width / height;

        let pixel_size = PTF2::new( 1.0 / width, 1.0 / height);

        let half_width = (self.fov.to_radians() * 0.5).tan();
        let half_height = half_width / ratio;

        let up_vector = PTF3::new(0.0, 1.0, 0.0);

        let w = glm::normalize(&(self.origin - self.center));
        let u = glm::cross(&up_vector, &w);
        let v = glm::cross(&w, &u);

        let lower_left = self.origin - half_width * u - half_height * v - w;
        let horizontal = u * half_width * 2.0;
        let vertical = v * half_height * 2.0;

        let mut rd = lower_left - self.origin;
        rd += horizontal * (pixel_size.x * offset.x + p.x);
        rd += vertical * (pixel_size.y * offset.y + p.y);

        [self.origin, glm::normalize(&rd)]
    }
}