From 87f62eedb09916954889f2fcc4ed278fa0f1f0da Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 12 Sep 2020 09:45:21 -0400 Subject: [PATCH] Add photon parameter to Material::sample(), other changes to integrate() --- src/integrators.rs | 74 +++++++++++++++------------- src/materials/mod.rs | 2 +- src/materials/reflective_material.rs | 2 +- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/integrators.rs b/src/integrators.rs index 49b5d6e..c37da97 100644 --- a/src/integrators.rs +++ b/src/integrators.rs @@ -55,35 +55,36 @@ impl Integrator for WhittedIntegrator { } }) .chain( - [info.material.sample(&(world_to_bsdf_space * info.retro))] - .iter() - .map(|direction| { - let world_space_direction = bsdf_to_world_space * direction; - match sampler.sample( - &Ray::new(info.location, world_space_direction).bias(0.000_000_1), - ) { - Some(recursive_hit) => { - if recursion_limit > 0 { - let photon = info.material.bsdf()( - &(world_to_bsdf_space * info.retro), - direction, - &self.integrate( - &sampler, - &recursive_hit, - &photon, - recursion_limit - 1, - ), - ); - photon.scale_intensity( - world_space_direction.dot(&info.normal).abs(), - ) - } else { - photon.scale_intensity(0.0) - } + [info + .material + .sample(&(world_to_bsdf_space * info.retro), &photon)] + .iter() + .map(|direction| { + let world_space_direction = bsdf_to_world_space * direction; + match sampler + .sample(&Ray::new(info.location, world_space_direction).bias(0.000_000_1)) + { + Some(recursive_hit) => { + if recursion_limit > 0 { + let photon = info.material.bsdf()( + &(world_to_bsdf_space * info.retro), + direction, + &self.integrate( + &sampler, + &recursive_hit, + &photon, + recursion_limit - 1, + ), + ); + photon + .scale_intensity(world_space_direction.dot(&info.normal).abs()) + } else { + photon.scale_intensity(0.0) } - None => photon.scale_intensity(0.0), } - }), + None => photon.scale_intensity(0.0), + } + }), ) .fold(photon.clone(), |a, b| { let mut result = a; @@ -115,18 +116,23 @@ impl Integrator for SimpleRandomIntegrator { let bsdf_to_world_space = world_to_bsdf_space .try_inverse() .expect("Expected matrix to be invertable."); - let w_i = info.material.sample(&(world_to_bsdf_space * info.retro)); - let world_space_w_i = bsdf_to_world_space * w_i; + let world_space_w_i = info.retro; + let w_i = world_to_bsdf_space * world_space_w_i; + let w_o = info.material.sample(&w_i, &photon); + let world_space_w_o = bsdf_to_world_space * w_o; info.material.bsdf()( - &(world_to_bsdf_space * info.retro), - &w_i, - &match sampler.sample(&Ray::new(info.location, world_space_w_i).bias(0.000_000_1)) { - None => photon.set_intensity(world_space_w_i.y()), + &w_o.normalize(), + &w_i.normalize(), + &match sampler.sample(&Ray::new(info.location, world_space_w_o).bias(0.000_000_1)) { + None => photon.set_intensity(test_lighting_environment( + &world_space_w_o, + photon.wavelength, + )), Some(recursive_hit) => { self.integrate(&sampler, &recursive_hit, &photon, recursion_limit - 1) } } - .scale_intensity(world_space_w_i.dot(&info.normal).abs()), + .scale_intensity(world_space_w_o.dot(&info.normal).abs()), ) } } diff --git a/src/materials/mod.rs b/src/materials/mod.rs index 8a170e4..fb2be32 100644 --- a/src/materials/mod.rs +++ b/src/materials/mod.rs @@ -18,7 +18,7 @@ pub use reflective_material::ReflectiveMaterial; pub trait Material: Debug + Sync + Send { fn bsdf<'a>(&'a self) -> Box Photon + 'a>; - fn sample(&self, _w_i: &Vec3) -> Vec3 { + fn sample(&self, _w_i: &Vec3, _photon: &Photon) -> Vec3 { let mut rng = thread_rng(); let mut w_o = Vec3::new( 2.0 * rng.sample::(Open01) - 1.0, diff --git a/src/materials/reflective_material.rs b/src/materials/reflective_material.rs index 3446229..05ff4e9 100644 --- a/src/materials/reflective_material.rs +++ b/src/materials/reflective_material.rs @@ -39,7 +39,7 @@ impl Material for ReflectiveMaterial { }) } - fn sample(&self, w_o: &Vec3) -> Vec3 { + fn sample(&self, w_o: &Vec3, _photon: &Photon) -> Vec3 { Vec3::new(-w_o.x(), -w_o.y(), w_o.z()) } }