aboutsummaryrefslogtreecommitdiff
path: root/src/material/metal.rs
blob: 880f23d0e4e6d18a85c4cc015d832103878ba2af (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
use super::Material;
use crate::ray::Ray;
use crate::vec3::Color;
use crate::{hittable::HitRecord, vec3::Vec3};

pub struct Metal {
    pub albedo: Color,
    pub fuzz: f64, // This should have value 0.0 - 1.0; this is not enforced
}

impl Material for Metal {
    fn scatter(
        &self,
        ray_in: &Ray,
        hit_record: &HitRecord,
        attenuation: &mut Color,
        scattered: &mut Ray,
    ) -> bool {
        let reflected = ray_in.direction.unit_vector().reflect(&hit_record.normal);
        *scattered = Ray {
            origin: hit_record.p.clone(),
            direction: reflected + self.fuzz * Vec3::random_in_unit_sphere(),
            time: ray_in.time,
        };
        *attenuation = self.albedo.clone();
        scattered.direction.dot(&hit_record.normal) > 0.0
    }
}