From 1f84cde760546210ee850a4b3119312bc5d7fb67 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Tue, 5 Oct 2021 09:13:08 -0400 Subject: [PATCH] Make default material sampling cosine-weighted instead of uniform --- src/materials/mod.rs | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/materials/mod.rs b/src/materials/mod.rs index 3590692..00de972 100644 --- a/src/materials/mod.rs +++ b/src/materials/mod.rs @@ -1,10 +1,8 @@ use crate::math::Vec3; use super::colour::Photon; +use super::random_distributions::{CosineWeightedHemisphere, RandomDistribution}; -use rand::distributions::{Open01, OpenClosed01}; -use rand::{thread_rng, Rng}; -use std::f64::consts::PI; use std::fmt::Debug; pub mod lambertian_material; @@ -28,22 +26,9 @@ pub trait Material: Debug + Sync + Send { fn bsdf<'a>(&'a self) -> Box Photon + 'a>; fn sample(&self, _w_i: &Vec3, _photon: &Photon) -> MaterialSampleResult { - let mut rng = thread_rng(); - let mut w_o = Vec3::new( - 2.0 * rng.sample::(Open01) - 1.0, - 2.0 * rng.sample::(Open01) - 1.0, - rng.sample::(OpenClosed01), - ); - while w_o.norm_squared() > 1.0 { - w_o = Vec3::new( - 2.0 * rng.sample::(Open01) - 1.0, - 2.0 * rng.sample::(Open01) - 1.0, - rng.sample::(OpenClosed01), - ); - } - MaterialSampleResult { - direction: w_o.normalize(), - pdf: 1.0 / (2.0 * PI), - } + let distribution = CosineWeightedHemisphere::new(); + let direction = distribution.value(); + let pdf = distribution.pdf(direction); + MaterialSampleResult { direction, pdf } } }