From 68f158f3c917971c5430171a3125f5baf1709521 Mon Sep 17 00:00:00 2001 From: bijan2005 Date: Sat, 20 Mar 2021 18:26:23 -0400 Subject: [PATCH] Fix bugs --- src/main.rs | 9 ++++----- src/object/point_light.rs | 2 +- src/render.rs | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 70fb5c0..30f9fb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,16 +36,15 @@ fn render(camera: &Camera, scene: &Scene, filename: &str) -> std::io::Result<()> fn main() -> std::io::Result<()> { - let camera = Camera::new(Point3::new(0.0,0.0,3.0), Vector3::new(0.0,0.0,-1.0), 1.0, 16.0 / 9.0, 2.0, 720); + let camera = Camera::new(Point3::new(0.0,5.0,0.0), Vector3::new(0.0,-1.0,0.0), 1.0, 16.0 / 9.0, 2.0, 720); let scene = Scene { objects: vec![ - Object::new(Sphere::new_solid(1.0, 0.2, 1.7, 0.2, Texture::new(1.0, 1.0, 1.0, 1.0))), - Object::new(Sphere::new_solid(0.0, -1.0, 0.0, 1.0, Texture::new(1.0, 1.0, 1.0, 1.0))) + Object::new(Plane::xz(|_, _| Texture { color: Color::white(), albedo: 0.8 })), + ], lights: vec![ - Box::new(PointLight::new(Point3::new(1.5, 1.0, 2.5), Color::white(), 3.0)), - Box::new(PointLight::new(Point3::new(1.0, -0.4, 1.5), Color::white(), 2.0)) + ], background: Color::gray(0.5) }; diff --git a/src/object/point_light.rs b/src/object/point_light.rs index 96c0667..3276356 100644 --- a/src/object/point_light.rs +++ b/src/object/point_light.rs @@ -22,7 +22,7 @@ impl Light for PointLight { fn check_shadow(&self, point: Point3f, objects: &Vec) -> bool { let max_d = distance(&self.pos, &point); objects.iter() - .filter_map(|&obj| obj.intersect(Ray::from_points(self.pos, point))) + .filter_map(|obj| obj.intersect(Ray::from_points(self.pos, point))) .all(|d| d - max_d > -1e-3 ) } diff --git a/src/render.rs b/src/render.rs index 729e420..8a6b174 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,27 +1,26 @@ extern crate nalgebra as na; -use std::f32::consts::PI; use std::cmp::Ordering; +use std::f32::consts::PI; use na::*; use na::geometry::Point3; -use crate::types::*; use crate::object::*; +use crate::types::*; fn trace(ray: Ray, objects: &Vec) -> Option<(&Object, f32)> { objects.iter() - .filter_map(|&obj| obj.intersect(ray) - .map(|x| (obj, x))) - .min_by(|&a, &b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) + .filter_map(|obj| obj.intersect(ray) + .map(|x| (obj, x))) + .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) } fn light_point(objects: &Vec, obj: &Object, point: Point3f, light: &dyn Light) -> Color { if light.check_shadow(point, objects) { + let texture = obj.get_texture(point); - let texture = obj.gettexture(point); - - light.getcolor(point) * (texture.albedo / PI) * light.intensity(point) * obj.normal(point).dot(&*light.direction(point)) + light.get_color(point) * (texture.albedo / PI) * light.intensity(point) * obj.normal(point).dot(&*light.direction(point)) } else { // Point is in shadow Color::black() @@ -31,11 +30,10 @@ fn light_point(objects: &Vec, obj: &Object, point: Point3f, light: &dyn pub fn cast_ray(ray: Ray, scene: &Scene) -> Color { if let Some((obj, dist)) = trace(ray, &scene.objects) { let point = ray.project(dist); - let surface_color = obj.gettexture(point).color; + let surface_color = obj.get_texture(point).color; scene.lights.iter() - .map(|&light| light_point(&scene.objects, obj, point, &*light)) - .fold(Color::black(), |acc, c| acc + c) * surface_color - } - else { scene.background } + .map(|light| light_point(&scene.objects, obj, point, &**light)) + .fold(Color::black(), |acc, c| acc + c) * surface_color + } else { scene.background } }