aboutsummaryrefslogtreecommitdiffstats
path: root/src/material.rs
blob: 3cbbb4a031debac1419a0a58f913615a2bd549b6 (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
use cgmath::Vector3;
use rand::prelude::*;
use crate::ray::{Ray, NextRay};
use crate::hittable::Hit;
use crate::random::random_unit_vector;

pub trait Material {
    fn scatter(&self, rng: &mut ThreadRng, ray: &Ray, hit: &Hit) -> Option<NextRay>;
}

pub struct Lambertian {
    pub albedo: Vector3<f64>
}

impl Lambertian {
    pub fn new(albedo: Vector3<f64>) -> Lambertian {
        Lambertian { albedo }
    }
}

impl Material for Lambertian {
    fn scatter(&self, rng: &mut ThreadRng, _ray: &Ray, hit: &Hit) -> Option<NextRay> {
        let scatter_direction = hit.normal + random_unit_vector(rng);
        Some(NextRay::new(self.albedo, Ray::new(hit.p, scatter_direction)))
    }
}