Make default material sampling cosine-weighted instead of uniform

This commit is contained in:
Matthew Gordon 2021-10-05 09:13:08 -04:00
parent b0c704f79a
commit 1f84cde760
1 changed files with 5 additions and 20 deletions

View File

@ -1,10 +1,8 @@
use crate::math::Vec3; use crate::math::Vec3;
use super::colour::Photon; 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; use std::fmt::Debug;
pub mod lambertian_material; pub mod lambertian_material;
@ -28,22 +26,9 @@ pub trait Material: Debug + Sync + Send {
fn bsdf<'a>(&'a self) -> Box<dyn Fn(&Vec3, &Vec3, &Photon) -> Photon + 'a>; fn bsdf<'a>(&'a self) -> Box<dyn Fn(&Vec3, &Vec3, &Photon) -> Photon + 'a>;
fn sample(&self, _w_i: &Vec3, _photon: &Photon) -> MaterialSampleResult { fn sample(&self, _w_i: &Vec3, _photon: &Photon) -> MaterialSampleResult {
let mut rng = thread_rng(); let distribution = CosineWeightedHemisphere::new();
let mut w_o = Vec3::new( let direction = distribution.value();
2.0 * rng.sample::<f64, _>(Open01) - 1.0, let pdf = distribution.pdf(direction);
2.0 * rng.sample::<f64, _>(Open01) - 1.0, MaterialSampleResult { direction, pdf }
rng.sample::<f64, _>(OpenClosed01),
);
while w_o.norm_squared() > 1.0 {
w_o = Vec3::new(
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
rng.sample::<f64, _>(OpenClosed01),
);
}
MaterialSampleResult {
direction: w_o.normalize(),
pdf: 1.0 / (2.0 * PI),
}
} }
} }