aboutsummaryrefslogtreecommitdiff
path: root/src/material/reflectant.rs
blob: dea7acaea15fcb26a885af1fad5cb9a1792b0a5e (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,
        }
    }
}

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()),
                ))
    }
}