diff options
Diffstat (limited to 'src/material')
-rw-r--r-- | src/material/dielectric.rs | 56 | ||||
-rw-r--r-- | src/material/diffuse_light.rs | 27 | ||||
-rw-r--r-- | src/material/lambertian.rs | 38 | ||||
-rw-r--r-- | src/material/mod.rs | 25 | ||||
-rw-r--r-- | src/material/reflectant.rs | 40 | ||||
-rw-r--r-- | src/material/sky_light.rs | 19 |
6 files changed, 0 insertions, 205 deletions
diff --git a/src/material/dielectric.rs b/src/material/dielectric.rs deleted file mode 100644 index c8dc279..0000000 --- a/src/material/dielectric.rs +++ /dev/null @@ -1,56 +0,0 @@ -use super::Material; -use crate::core::{min, Vector3f, Spectrum, Ray}; -use crate::world::Intersection; -use crate::sample::Sampler; -use crate::Float; -use crate::material::reflectant::reflect; - -pub struct Dielectric { - ratio: Float, -} - -// Implementation from RTIOW -fn refract(v: Vector3f, n: Vector3f, r_ratio: Float, cos_theta: Float) -> Vector3f { - let r_perp = (v + n * cos_theta) * r_ratio; - let r_parallel = n * (-(1.0 - r_perp.len_squared()).abs().sqrt()); - - r_perp + r_parallel -} - -// Schlick Approximation, explained in RTIOW -fn fresnel(cos: Float, ratio: Float) -> Float { - let mut r0 = (1.0-ratio) / (1.0+ratio); - r0 = r0 * r0; - - r0 + (1.0-r0)*(1.0-cos).powi(5) -} - -impl Dielectric { - pub fn new(ratio: Float) -> Self { - Self { ratio } - } -} - -impl Material for Dielectric { - // Implementation from RTIOW - fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> { - let ratio = if i.front {1.0/self.ratio} else {self.ratio}; - - let ray_dir = ray.direction.norm(); - let cos_theta = min((-ray_dir).dot(&i.n), 1.0); - let sin_theta = (1.0 - cos_theta*cos_theta).sqrt(); - - // Test if it is possible for the ray the retract or if it must reflect. - let cannot_refract = (ratio * sin_theta) > 1.0; - let direction = if cannot_refract || (fresnel(cos_theta, ratio) > sampler.get_sample()) { - reflect(ray_dir, i.n) - } else { - refract(ray_dir, i.n, ratio, cos_theta) - }; - - Some(( - Spectrum::WHITE, - Ray::new(i.p, direction), - )) - } -} diff --git a/src/material/diffuse_light.rs b/src/material/diffuse_light.rs deleted file mode 100644 index fe462a8..0000000 --- a/src/material/diffuse_light.rs +++ /dev/null @@ -1,27 +0,0 @@ -use super::Material; -use crate::core::{Ray, Spectrum}; -use crate::Float; - -pub struct DiffuseLight { - color: Spectrum, -} - -impl DiffuseLight { - pub fn new(c: Spectrum) -> Self { - Self { - color: c, - } - } - - pub fn new_white(s: Float) -> Self { - Self { - color: Spectrum::new_rgb(s, s, s), - } - } -} - -impl Material for DiffuseLight { - fn emitted(&self, _: &Ray) -> Option<Spectrum> { - Some(self.color) - } -} diff --git a/src/material/lambertian.rs b/src/material/lambertian.rs deleted file mode 100644 index 3df6522..0000000 --- a/src/material/lambertian.rs +++ /dev/null @@ -1,38 +0,0 @@ -use super::Material; -use crate::core::{Ray, Spectrum}; -use crate::world::Intersection; -use crate::sample::Sampler; - -use std::rc::Rc; - -pub struct Lambertian { - color: Spectrum, -} - -impl Lambertian { - pub fn new(c: Spectrum) -> Lambertian { - Lambertian { - color: c, - } - } - - pub fn new_rc(c: Spectrum) -> Rc<dyn Material> { - Rc::new(Self::new(c)) - } -} - -impl Material for Lambertian { - fn scatter(&self, _: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> { - let mut newray = Ray { - origin: i.p, - direction: i.n + sampler.get_unit_vector(), - }; - - // Make sure that the resulting direction is not (0, 0, 0) - if newray.direction.near_zero() { - newray.direction = i.n; - } - - Some((self.color, newray)) - } -} diff --git a/src/material/mod.rs b/src/material/mod.rs deleted file mode 100644 index 6732598..0000000 --- a/src/material/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::core::{Ray, Spectrum}; -use crate::world::Intersection; -use crate::sample::Sampler; - -mod lambertian; -mod diffuse_light; -mod sky_light; -mod dielectric; -pub mod reflectant; - -pub use lambertian::Lambertian; -pub use diffuse_light::DiffuseLight; -pub use sky_light::SkyLight; -pub use dielectric::Dielectric; -pub use reflectant::Reflectant; - -pub trait Material: Sync + Send { - fn scatter(&self, _: &Ray, _: &Intersection, _: &mut dyn Sampler) -> Option<(Spectrum, Ray)> { - None - } - - fn emitted(&self, _: &Ray) -> Option<Spectrum> { - None - } -} diff --git a/src/material/reflectant.rs b/src/material/reflectant.rs deleted file mode 100644 index b5ec0ae..0000000 --- a/src/material/reflectant.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::Float; -use crate::core::{Ray, Spectrum, Vector3f}; -use crate::world::Intersection; -use super::Material; -use crate::sample::Sampler; - -pub struct Reflectant { - color: Spectrum, - fuzz: Option<Float>, -} - -impl Reflectant { - pub fn new(c: Spectrum, fuzz: Option<Float>) -> Reflectant { - Reflectant { - color: c, - fuzz, - } - } -} - -pub fn reflect(v: Vector3f, n: Vector3f) -> Vector3f { - v - n * (2.0 * v.dot(&n)) -} - -impl Material for Reflectant { - fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> { - // Find reflectance vector - let mut reflected = reflect(ray.direction, i.n); - if let Some(fuzz) = self.fuzz { - reflected += &(sampler.get_unit_vector() * fuzz); - } - - Some(( - self.color, - Ray::new(i.p, reflected.norm()), - )) - } -} - - diff --git a/src/material/sky_light.rs b/src/material/sky_light.rs deleted file mode 100644 index e499b6e..0000000 --- a/src/material/sky_light.rs +++ /dev/null @@ -1,19 +0,0 @@ -use super::Material; -use crate::core::{Ray, Spectrum}; - -pub struct SkyLight { -} - -impl SkyLight { - pub fn new() -> Self { - Self {} - } -} - -impl Material for SkyLight { - fn emitted(&self, ray: &Ray) -> Option<Spectrum> { - let t = (ray.direction.norm().y + 1.0) * 0.5; - - Some(Spectrum::new_rgb(1.0, 1.0, 1.0) * (1.0-t) + Spectrum::new_rgb(0.5, 0.7, 1.0) * t) - } -} |