From 78ddaff5855bf8446adef9e18eb0d7b7ddcee52a Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:45:56 +0000 Subject: init --- src/material/lambertian.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/material/lambertian.rs (limited to 'src/material/lambertian.rs') diff --git a/src/material/lambertian.rs b/src/material/lambertian.rs new file mode 100644 index 0000000..95f698e --- /dev/null +++ b/src/material/lambertian.rs @@ -0,0 +1,34 @@ +use std::sync::Arc; + +use super::Material; +use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3}; +use crate::vec3::Color; +use crate::texture::SolidColor; +use crate::ray::Ray; + +pub struct Lambertian { + pub albedo: Arc, +} + +impl Lambertian { + pub fn from_color(color: Color) -> Self { + Self { + albedo: Arc::new(SolidColor { color_value: color.clone() }), + } + } +} + +impl Material for Lambertian { + fn scatter(&self, ray_in : &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { + let mut scatter_direction = &hit_record.normal + Vec3::random_unit_vector(); + + // Catch zero-vector scatter directions that will generate issues later + if scatter_direction.near_zero() { + scatter_direction = hit_record.normal.clone(); + } + + *scattered = Ray { origin: hit_record.p.clone(), direction: scatter_direction, time: ray_in.time }; + *attenuation = self.albedo.value(hit_record.u, hit_record.v, &hit_record.p); + true + } +} -- cgit v1.2.3