-
-
Save eddyb/71cec6776cf4d26d55e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::num::zero; | |
use std::rand::Rng; | |
use std::rand::distributions::range::SampleRange; | |
use vecmath::vector::Vec3; | |
use vecmath::ray::Ray; | |
use lightray::Lightray; | |
use objecthit::ObjectHit; | |
pub trait Material<T> { | |
fn reflect_with_gen_range(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, gen_range: |T, T| -> T) -> Lightray<T>; | |
fn has_emission(&self) -> bool; | |
fn emission(&self, wavelength: f32) -> f32; | |
} | |
impl<T> Material<T> for &Material<T> { | |
fn reflect_with_gen_range(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, gen_range: |T, T| -> T) -> Lightray<T> { | |
self.reflect_with_gen_range(lray, ohit, gen_range) | |
} | |
fn has_emission(&self) -> bool { | |
self.has_emission() | |
} | |
fn emission(&self, wavelength: f32) -> f32 { | |
self.emission(wavelength) | |
} | |
} | |
pub trait MaterialWithRng<T> { | |
fn reflect<R: Rng>(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, r: &mut R) -> Lightray<T>; | |
} | |
impl<T, M: Material<T>> MaterialWithRng<T> { | |
fn reflect<R: Rng>(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, r: &mut R) -> Lightray<T> { | |
self.reflect_with_gen_range(lray, ohit, |a, b| r.gen_range(a, b)) | |
} | |
} | |
pub struct Clay; | |
impl<T: Num + FloatMath + PartialOrd + SampleRange> Material<T> for Clay { | |
fn reflect<R: Rng>(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, rng: &mut R) -> Lightray<T> { | |
let new_dir = Vec3::cos_hemisphere(rng).align( | |
&if lray.ray.dir.dotv(&ohit.hit.normal) < zero() { ohit.hit.normal } else { ohit.hit.normal.cflip() } | |
); | |
Lightray::new(Ray::new(ohit.hit.pos, new_dir), lray.wavelength, 1f32) | |
} | |
fn has_emission(&self) -> bool { false } | |
fn emission(&self, wavelength: f32) -> f32 { 0.0f32 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment