Add CheckerTexture.

This commit is contained in:
Bill Thiede 2018-09-22 20:23:46 -07:00
parent a1d3cce4e4
commit 96d49e685f
2 changed files with 27 additions and 2 deletions

View File

@ -5,10 +5,10 @@ use hitable::Hit;
use hitable_list::HitableList;
use kdtree::KDTree;
use material::Lambertian;
use material::Metal;
use renderer::Opt;
use renderer::Scene;
use sphere::Sphere;
use texture::CheckerTexture;
use texture::ConstantTexture;
use vec3::Vec3;
@ -33,7 +33,10 @@ pub fn new(opt: &Opt) -> Scene {
let mut objects: Vec<Box<Hit>> = vec![Box::new(Sphere::new(
Vec3::new(0., 0., 0.),
5.,
Box::new(Metal::new(Vec3::new(0.5, 0.5, 0.5), 0.)),
Box::new(Lambertian::new(Box::new(CheckerTexture::new(
Box::new(ConstantTexture::new(Vec3::new(0., 0., 0.))),
Box::new(ConstantTexture::new(Vec3::new(1., 1., 1.))),
)))),
))];
let num_spheres = 6;
let radius = 7.;

View File

@ -19,3 +19,25 @@ impl Texture for ConstantTexture {
self.color
}
}
pub struct CheckerTexture {
odd: Box<Texture>,
even: Box<Texture>,
}
impl CheckerTexture {
pub fn new(odd: Box<Texture>, even: Box<Texture>) -> CheckerTexture {
CheckerTexture { odd, even }
}
}
impl Texture for CheckerTexture {
fn value(&self, u: f32, v: f32, p: Vec3) -> Vec3 {
let sines = (10. * p.x).sin() * (10. * p.y).sin() * (10. * p.z).sin();
if sines < 0. {
self.odd.value(u, v, p)
} else {
self.even.value(u, v, p)
}
}
}