From c24d1e52b3f173ba5f4cc04f5a6a449a011a60c7 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 7 Feb 2021 17:52:30 +0100 Subject: Add reflecting material --- src/material/reflectant.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/material/reflectant.rs (limited to 'src/material/reflectant.rs') diff --git a/src/material/reflectant.rs b/src/material/reflectant.rs new file mode 100644 index 0000000..f4b110c --- /dev/null +++ b/src/material/reflectant.rs @@ -0,0 +1,39 @@ +use crate::Float; +use crate::core::{Ray, Intersection, Spectrum, Vector3f}; +use super::Material; +use crate::sample::Sampler; + +pub struct Reflectant { + color: Spectrum, + fuzz: Option, +} + +impl Reflectant { + pub fn new(c: Spectrum, fuzz: Option) -> Reflectant { + Reflectant { + color: c, + fuzz, + } + } +} + +fn reflect(v: &Vector3f, n: &Vector3f) -> Vector3f { + *v - *n * (2.0 * v.dot(n)) +} + +impl Material for Reflectant { + fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> { + // Find reflectance vector + let mut reflected = reflect(&ray.direction, &i.n); + if let Some(fuzz) = self.fuzz { + reflected += &(sampler.get_unit_vector() * fuzz); + } + + Some(( + self.color, + Ray::new(i.p, reflected.norm()), + )) + } +} + + -- cgit v1.2.3