From aeaf4994fe98c7ab49eab53c89f1ead1a013b92b Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 5 Oct 2018 18:16:42 -0700 Subject: [PATCH] Implement isotropic scattering for ConstantMedium volumes. --- rtiow/src/constant_medium.rs | 24 ++++++++++++++---------- rtiow/src/material.rs | 31 ++++++++++++++++++++++++++++++- rtiow/src/scenes/cornell_smoke.rs | 4 ++-- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/rtiow/src/constant_medium.rs b/rtiow/src/constant_medium.rs index 5d5cb07..13b22a8 100644 --- a/rtiow/src/constant_medium.rs +++ b/rtiow/src/constant_medium.rs @@ -6,38 +6,42 @@ use rand::Rng; use aabb::AABB; use hitable::Hit; use hitable::HitRecord; -use material::Material; +use material::Isotropic; use ray::Ray; +use texture::Texture; use vec3::Vec3; -pub struct ConstantMedium +pub struct ConstantMedium where H: Hit, - M: Material, + T: Texture, { density: f32, - material: M, + material: Isotropic, hitable: H, } -impl ConstantMedium +impl ConstantMedium where H: Hit, - M: Material, + T: Texture, { - pub fn new(hitable: H, density: f32, material: M) -> ConstantMedium { + pub fn new(hitable: H, density: f32, texture: T) -> ConstantMedium + where + T: Texture, + { ConstantMedium { density, - material, + material: Isotropic::new(texture), hitable, } } } -impl Hit for ConstantMedium +impl Hit for ConstantMedium where H: Hit, - M: Material, + T: Texture, { fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option { let mut rng = rand::thread_rng(); diff --git a/rtiow/src/material.rs b/rtiow/src/material.rs index 277b393..0a24ded 100644 --- a/rtiow/src/material.rs +++ b/rtiow/src/material.rs @@ -56,11 +56,40 @@ impl Material for Box { } } +pub struct Isotropic +where + T: Texture, +{ + albedo: T, +} + +impl Isotropic +where + T: Texture, +{ + pub fn new(texture: T) -> Isotropic { + Isotropic { albedo: texture } + } +} + +impl Material for Isotropic +where + T: Texture, +{ + fn scatter(&self, _r_in: &Ray, rec: &HitRecord) -> ScatterResponse { + let (u, v) = rec.uv; + ScatterResponse { + attenutation: self.albedo.value(u, v, rec.p), + scattered: Ray::new(rec.p, random_in_unit_sphere(), 0.), + reflected: true, + } + } +} + pub struct Lambertian where T: Texture, { - // TODO(wathiede): implement texture sharing via references albedo: T, } diff --git a/rtiow/src/scenes/cornell_smoke.rs b/rtiow/src/scenes/cornell_smoke.rs index 6f5b05b..470ede0 100644 --- a/rtiow/src/scenes/cornell_smoke.rs +++ b/rtiow/src/scenes/cornell_smoke.rs @@ -59,7 +59,7 @@ pub fn new(opt: &Opt) -> Scene { Vec3::new(130., 0., 65.), ), 0.01, - Lambertian::new(ConstantTexture::new([1., 1., 1.])), + ConstantTexture::new([1., 1., 1.]), )), // Black smoke box on the left Box::new(ConstantMedium::new( @@ -75,7 +75,7 @@ pub fn new(opt: &Opt) -> Scene { Vec3::new(265., 0., 295.), ), 0.01, - Lambertian::new(ConstantTexture::new([0., 0., 0.])), + ConstantTexture::new([0., 0., 0.]), )), // Green wall left Box::new(FlipNormals::new(YZRect::new(