diff options
author | Julian T <julian@jtle.dk> | 2021-02-07 17:52:30 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-07 17:52:46 +0100 |
commit | c24d1e52b3f173ba5f4cc04f5a6a449a011a60c7 (patch) | |
tree | ff39abb0473a78aa15fbd2ab4c7f5c82660fe3ab /src/material | |
parent | 33e747fa1c0957546c10e4d7b490ac7fbb0fd2d2 (diff) |
Add reflecting material
Diffstat (limited to 'src/material')
-rw-r--r-- | src/material/mod.rs | 2 | ||||
-rw-r--r-- | src/material/reflectant.rs | 39 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/material/mod.rs b/src/material/mod.rs index 62cbf9a..c939385 100644 --- a/src/material/mod.rs +++ b/src/material/mod.rs @@ -2,8 +2,10 @@ use crate::core::{Ray, Intersection, Spectrum}; use crate::sample::Sampler; mod lambertian; +mod reflectant; pub use lambertian::Lambertian; +pub use reflectant::Reflectant; pub trait Material { fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)>; 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<Float>, +} + +impl Reflectant { + pub fn new(c: Spectrum, fuzz: Option<Float>) -> 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()), + )) + } +} + + |