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

use std::rc::Rc;

pub struct Lambertian {
    color: Spectrum,
}

impl Lambertian {
    pub fn new(c: Spectrum) -> Lambertian {
        Lambertian {
            color: c,
        }
    }

    pub fn new_rc(c: Spectrum) -> Rc<dyn Material> {
        Rc::new(Self::new(c))
    }
}

impl Material for Lambertian {
    fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> {
        let mut newray = Ray {
            origin: i.p,
            direction: i.n + sampler.get_unit_vector(),
        };

        // Make sure that the resulting direction is not (0, 0, 0)
        if newray.direction.near_zero() {
            newray.direction = i.n;
        }

        Some((self.color.clone(), newray))
    }
}