aboutsummaryrefslogtreecommitdiff
path: root/src/material/reflectant.rs
blob: b5ec0aef1b1ff0029b5c67736c73f6d0551609b3 (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
use crate::Float;
use crate::core::{Ray, Spectrum, Vector3f};
use crate::world::Intersection;
use super::Material;
use crate::sample::Sampler;

pub struct Reflectant {
    color: Spectrum,
    fuzz: Option<Float>,
}

impl Reflectant {
    pub fn new(c: Spectrum, fuzz: Option<Float>) -> Reflectant {
        Reflectant {
            color: c,
            fuzz,
        }
    }
}

pub fn reflect(v: Vector3f, n: Vector3f) -> Vector3f {
    v - n * (2.0 * v.dot(&n))
}

impl Material for Reflectant {
    fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> {
        // Find reflectance vector
        let mut reflected = reflect(ray.direction, i.n);
        if let Some(fuzz) = self.fuzz {
            reflected += &(sampler.get_unit_vector() * fuzz);
        }

        Some((
                self.color,
                Ray::new(i.p, reflected.norm()),
                ))
    }
}