aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/constant_medium.rs
blob: 50bddc61000a9452db9380bd76334fb75f0b6bf2 (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
use std::sync::Arc;

use crate::{
    hittable::{HitRecord, Hittable, AABB},
    material::{Isotropic, Material},
    ray::Ray,
    texture::Texture,
    vec3::Vec3,
};

pub struct ConstantMedium {
    boundary: Arc<dyn Hittable>,
    phase_function: Arc<dyn Material>,
    neg_inv_density: f64,
}

impl ConstantMedium {
    pub fn new(boundary: Arc<dyn Hittable>, density: f64, texture: Arc<dyn Texture>) -> Self {
        Self {
            boundary,
            phase_function: Arc::new(Isotropic::from_texture(texture)),
            neg_inv_density: -1.0 / density,
        }
    }
}

impl Hittable for ConstantMedium {
    // TODO: this only support convex shapes.
    fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
        let mut record_1 = self.boundary.hit(ray, -f64::INFINITY, f64::INFINITY)?;
        let mut record_2 = self.boundary.hit(ray, record_1.t + 0.0001, f64::INFINITY)?;

        if record_1.t < t_min {
            record_1.t = t_min;
        }
        if record_2.t > t_max {
            record_2.t = t_max;
        }

        if record_1.t >= record_2.t {
            return None;
        }

        if record_1.t < 0.0 {
            record_1.t = 0.0;
        }

        let ray_length = ray.direction.length();
        let distance_inside_boundary = (record_2.t - record_1.t) * ray_length;
        let hit_distance = self.neg_inv_density * rand::random::<f64>().ln();

        if hit_distance > distance_inside_boundary {
            return None;
        }

        let t = record_1.t + hit_distance / ray_length;
        Some(HitRecord {
            p: ray.at(t),
            t,
            material: Some(self.phase_function.clone()),
            normal: Vec3 {
                x: 1.0,
                y: 0.0,
                z: 0.0,
            }, // arbitrary
            front_face: true, // arbitrary
            u: 0.0,           // arbitrary
            v: 0.0,           // arbitrary
        })
    }

    fn bounding_box(&self, time_start: f64, time_end: f64) -> Option<AABB> {
        self.boundary.bounding_box(time_start, time_end)
    }
}