summaryrefslogtreecommitdiff
path: root/racer-tracer/src/material/metal.rs
blob: 162342d078c9a12dc4eb50e1dc9c2cc182c7528e (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
use crate::{
    material::Material,
    ray::Ray,
    vec3::{random_in_unit_sphere, reflect, Color},
};

pub struct Metal {
    color: Color,
    fuzz: f64,
}

impl Metal {
    pub fn new(color: Color, fuzz: f64) -> Self {
        Self { color, fuzz }
    }
}

impl Material for Metal {
    fn scatter(
        &self,
        ray: &crate::ray::Ray,
        rec: &crate::geometry::HitRecord,
    ) -> Option<(Ray, Color)> {
        let reflected = reflect(&ray.direction().unit_vector(), &rec.normal);
        let scattered = Ray::new(rec.point, reflected + self.fuzz * random_in_unit_sphere());

        if scattered.direction().dot(&rec.normal) < 0.0 {
            None
        } else {
            Some((scattered, self.color))
        }
    }
}