Add XZ and YZ rects to complement XY.

This commit is contained in:
Bill Thiede 2018-09-23 21:29:50 -07:00
parent 2f0ee09084
commit 3839ff316d
2 changed files with 159 additions and 12 deletions

View File

@ -56,3 +56,107 @@ impl Hit for XYRect {
))
}
}
pub struct XZRect {
x0: f32,
x1: f32,
z0: f32,
z1: f32,
k: f32,
material: Box<Material>,
}
impl XZRect {
pub fn new(x0: f32, x1: f32, z0: f32, z1: f32, k: f32, material: Box<Material>) -> XZRect {
XZRect {
x0,
x1,
z0,
z1,
k,
material,
}
}
}
impl Hit for XZRect {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
let t = (self.k - r.origin.y) / r.direction.y;
if t < t_min || t > t_max {
return None;
}
let x = r.origin.x + t * r.direction.x;
let z = r.origin.z + t * r.direction.z;
if x < self.x0 || x > self.x1 || z < self.z0 || z > self.z1 {
return None;
}
let u = (x - self.x0) / (self.x1 - self.x0);
let v = (z - self.z0) / (self.z1 - self.z0);
Some(HitRecord {
t,
uv: (u, v),
p: r.point_at_parameter(t),
normal: Vec3::new(0., 1., 0.),
material: &*self.material,
})
}
fn bounding_box(&self, _t_min: f32, _t_max: f32) -> Option<AABB> {
Some(AABB::new(
Vec3::new(self.x0, self.k - 0.0001, self.z0),
Vec3::new(self.x1, self.k + 0.0001, self.z1),
))
}
}
pub struct YZRect {
y0: f32,
y1: f32,
k: f32,
z0: f32,
z1: f32,
material: Box<Material>,
}
impl YZRect {
pub fn new(y0: f32, y1: f32, z0: f32, z1: f32, k: f32, material: Box<Material>) -> YZRect {
YZRect {
y0,
y1,
z0,
z1,
k,
material,
}
}
}
impl Hit for YZRect {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
let t = (self.k - r.origin.x) / r.direction.x;
if t < t_min || t > t_max {
return None;
}
let y = r.origin.y + t * r.direction.y;
let z = r.origin.z + t * r.direction.z;
if y < self.y0 || y > self.y1 || z < self.z0 || z > self.z1 {
return None;
}
let u = (y - self.y0) / (self.y1 - self.y0);
let v = (z - self.z0) / (self.z1 - self.z0);
Some(HitRecord {
t,
uv: (u, v),
p: r.point_at_parameter(t),
normal: Vec3::new(1., 0., 0.),
material: &*self.material,
})
}
fn bounding_box(&self, _t_min: f32, _t_max: f32) -> Option<AABB> {
Some(AABB::new(
Vec3::new(self.k - 0.0001, self.y0, self.z0),
Vec3::new(self.k + 0.0001, self.y1, self.z1),
))
}
}

View File

@ -7,6 +7,8 @@ use kdtree::KDTree;
use material::DiffuseLight;
use material::Lambertian;
use rect::XYRect;
use rect::XZRect;
use rect::YZRect;
use renderer::Opt;
use renderer::Scene;
use sphere::Sphere;
@ -16,7 +18,7 @@ use texture::NoiseTexture;
use vec3::Vec3;
pub fn new(opt: &Opt) -> Scene {
let lookfrom = Vec3::new(30., 2., 0.);
let lookfrom = Vec3::new(20., 20., 20.);
let lookat = Vec3::new(0., 1., 0.);
let dist_to_focus = 10.0;
let aperture = 0.0;
@ -41,6 +43,7 @@ pub fn new(opt: &Opt) -> Scene {
let world_image_bytes = include_bytes!("../../images/world.jpg");
let it = ImageTexture::new(image::load_from_memory(world_image_bytes).unwrap().to_rgb());
let it2 = ImageTexture::new(image::load_from_memory(world_image_bytes).unwrap().to_rgb());
let objects: Vec<Box<Hit>> = vec![
// Big sphere
Box::new(Sphere::new(
@ -50,26 +53,66 @@ pub fn new(opt: &Opt) -> Scene {
)),
// Earth sized sphere
Box::new(Sphere::new(
Vec3::new(0., -1000., 0.),
1000.,
Vec3::new(0., -100., 0.),
100.,
// Box::new(Lambertian::new(ground_color)),
Box::new(Lambertian::new(Box::new(NoiseTexture::with_scale(10.)))),
)),
Box::new(XYRect::new(
0.,
4.,
0.,
Box::new(XZRect::new(
-100.,
100.,
-100.,
1000.,
60.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(1., 1., 1.),
)))),
)),
Box::new(YZRect::new(
1.,
3.,
-1.,
1.,
4.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(4., 0., 4.),
)))),
)),
Box::new(YZRect::new(
1.,
3.,
-1.,
1.,
-4.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(4., 4., 4.),
Vec3::new(0., 4., 0.),
)))),
)),
Box::new(XZRect::new(
-1.,
1.,
-1.,
1.,
6.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(4., 4., 0.),
)))),
)),
Box::new(XYRect::new(
0.,
4.,
0.,
4.,
-1.,
1.,
1.,
3.,
-4.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(0., 0., 4.),
)))),
)),
Box::new(XYRect::new(
-1.,
1.,
1.,
3.,
4.,
Box::new(DiffuseLight::new(Box::new(ConstantTexture::new(
Vec3::new(0., 4., 4.),