From 94b14ec5a24f587c97b11d6c56b9116a58a7385a Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 7 Mar 2021 18:25:35 +0100 Subject: Add light materials --- src/trace/pathtrace.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/trace/pathtrace.rs') diff --git a/src/trace/pathtrace.rs b/src/trace/pathtrace.rs index 2d15976..f53e433 100644 --- a/src/trace/pathtrace.rs +++ b/src/trace/pathtrace.rs @@ -1,23 +1,23 @@ use crate::world::{Hittable, Scene}; use crate::core::{Ray, Spectrum}; +use crate::material::Material; use crate::sample::Sampler; -use crate::material::{Lambertian, Material}; use super::Tracer; pub struct PathTracer<'a> { depth: i32, scn: &'a Scene, - default_mat: Box, + background: Option>, } impl PathTracer<'_> { - pub fn new(scn: &Scene, depth: Option) -> PathTracer { + pub fn new(scn: &Scene, depth: Option, background: Option>) -> PathTracer { let depth = depth.unwrap_or(-1); PathTracer { depth, scn, - default_mat: Box::new(Lambertian::new(Spectrum::ZERO)), + background, } } @@ -28,17 +28,28 @@ impl PathTracer<'_> { } if let Some(i) = self.scn.intersect(ray) { - if let Some((scalar, nray)) = i.m.unwrap_or(self.default_mat.as_ref()).scatter(ray, &i, sampler) { - return self.trace_recur(sampler, &nray, depth-1) * scalar; - } else { - return Spectrum::ZERO; + // Extract material or default + if let Some(mat) = i.m { + let mut col = Spectrum::ZERO; + + if let Some((scalar, nray)) = mat.scatter(ray, &i, sampler) { + col += &(self.trace_recur(sampler, &nray, depth-1) * scalar); + } + + if let Some(c) = mat.emitted(ray) { + col += &c; + } + + return col; } } - // Simulates a sky - let t = (ray.direction.norm().y + 1.0) * 0.5; - Spectrum::new_rgb(1.0, 1.0, 1.0) * (1.0-t) + Spectrum::new_rgb(0.5, 0.7, 1.0) * t - + // If no color return background + if let Some(back) = &self.background { + back.emitted(ray).unwrap_or(Spectrum::ZERO) + } else { + Spectrum::ZERO + } } } -- cgit v1.2.3