Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2014 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/e619e7835508af73ad19 to your computer and use it in GitHub Desktop.
Save anonymous/e619e7835508af73ad19 to your computer and use it in GitHub Desktop.
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<R: Rng>(&self, lray: &Lightray<T>, ohit: &ObjectHit<T>, r: &mut R) -> Lightray<T>;
fn has_emission(&self) -> bool;
fn emission(&self, wavelength: f32) -> f32;
}
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