From 3839ff316d66df1b5f27f5bad7943c8543777a03 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 23 Sep 2018 21:29:50 -0700 Subject: [PATCH] Add XZ and YZ rects to complement XY. --- rtiow/src/rect.rs | 104 +++++++++++++++++++++++++++++++++++++++ rtiow/src/scenes/test.rs | 67 ++++++++++++++++++++----- 2 files changed, 159 insertions(+), 12 deletions(-) diff --git a/rtiow/src/rect.rs b/rtiow/src/rect.rs index 2998601..07f7059 100644 --- a/rtiow/src/rect.rs +++ b/rtiow/src/rect.rs @@ -56,3 +56,107 @@ impl Hit for XYRect { )) } } + +pub struct XZRect { + x0: f32, + x1: f32, + z0: f32, + z1: f32, + k: f32, + material: Box, +} + +impl XZRect { + pub fn new(x0: f32, x1: f32, z0: f32, z1: f32, k: f32, material: Box) -> XZRect { + XZRect { + x0, + x1, + z0, + z1, + k, + material, + } + } +} + +impl Hit for XZRect { + fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option { + 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 { + 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, +} + +impl YZRect { + pub fn new(y0: f32, y1: f32, z0: f32, z1: f32, k: f32, material: Box) -> YZRect { + YZRect { + y0, + y1, + z0, + z1, + k, + material, + } + } +} + +impl Hit for YZRect { + fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option { + 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 { + Some(AABB::new( + Vec3::new(self.k - 0.0001, self.y0, self.z0), + Vec3::new(self.k + 0.0001, self.y1, self.z1), + )) + } +} diff --git a/rtiow/src/scenes/test.rs b/rtiow/src/scenes/test.rs index 4bc7a23..22db712 100644 --- a/rtiow/src/scenes/test.rs +++ b/rtiow/src/scenes/test.rs @@ -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> = 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.),