aboutsummaryrefslogtreecommitdiff
path: root/src/material
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-07 14:25:06 +0100
committerJulian T <julian@jtle.dk>2021-02-07 14:26:20 +0100
commit33e747fa1c0957546c10e4d7b490ac7fbb0fd2d2 (patch)
tree515b2b2e017329cbcada60e898fb5795fbceb5cd /src/material
parenta43ab40a75e9d4c6ee3fbdd583bc93574db19124 (diff)
Abstracted lambertian into material
Diffstat (limited to 'src/material')
-rw-r--r--src/material/lambertian.rs37
-rw-r--r--src/material/mod.rs10
2 files changed, 47 insertions, 0 deletions
diff --git a/src/material/lambertian.rs b/src/material/lambertian.rs
new file mode 100644
index 0000000..28d15d3
--- /dev/null
+++ b/src/material/lambertian.rs
@@ -0,0 +1,37 @@
+use super::Material;
+use crate::core::{Intersection, Ray, Spectrum};
+use crate::sample::Sampler;
+
+use std::rc::Rc;
+
+pub struct Lambertian {
+ color: Spectrum,
+}
+
+impl Lambertian {
+ pub fn new(c: Spectrum) -> Lambertian {
+ Lambertian {
+ color: c,
+ }
+ }
+
+ pub fn new_rc(c: Spectrum) -> Rc<dyn Material> {
+ Rc::new(Self::new(c))
+ }
+}
+
+impl Material for Lambertian {
+ fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> {
+ let mut newray = Ray {
+ origin: i.p,
+ direction: i.n + sampler.get_unit_vector(),
+ };
+
+ // Make sure that the resulting direction is not (0, 0, 0)
+ if newray.direction.near_zero() {
+ newray.direction = i.n;
+ }
+
+ Some((self.color.clone(), newray))
+ }
+}
diff --git a/src/material/mod.rs b/src/material/mod.rs
new file mode 100644
index 0000000..62cbf9a
--- /dev/null
+++ b/src/material/mod.rs
@@ -0,0 +1,10 @@
+use crate::core::{Ray, Intersection, Spectrum};
+use crate::sample::Sampler;
+
+mod lambertian;
+
+pub use lambertian::Lambertian;
+
+pub trait Material {
+ fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)>;
+}