From 09f613f59f0c93a58b43494f090b6d8817bb294c Mon Sep 17 00:00:00 2001 From: STP Date: Fri, 17 Nov 2023 14:15:50 -0500 Subject: [PATCH] Changed colour ray to an optional return --- src/raytracer.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/raytracer.rs b/src/raytracer.rs index 52a8815..e2bc34c 100644 --- a/src/raytracer.rs +++ b/src/raytracer.rs @@ -7,7 +7,8 @@ use crate::{ }; use std::sync::Arc; -use nalgebra::{distance, Matrix4, Point3, Vector3, Vector4}; +use nalgebra::{distance, Matrix4, Point3, Unit, Vector3, Vector4}; + static ZERO_VECTOR: Vector3 = Vector3::new(0.0, 0.0, 0.0); static ONE_VECTOR: Vector3 = Vector3::new(1.0, 1.0, 1.0); @@ -28,11 +29,11 @@ pub fn shade_rays(scene: &Scene, rays: &Vec, width: i32, height: i32) -> Ve pixel_data } //Shade a single ray -pub fn shade_ray(scene: &Scene, ray: &Ray) -> Vector3 { +pub fn shade_ray(scene: &Scene, ray: &Ray) -> Option> { let intersect = get_closest_intersection(scene.primitives.clone(), ray); match intersect { - Some(intersect) => phong_shade_point(&scene, &intersect), - None => Vector3::new(0, 0, 0), + Some(intersect) => Some(phong_shade_point(&scene, &intersect)), + None => None, } } @@ -91,7 +92,7 @@ pub fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3 // Get light incidence vector let to_light = light_position - point; let light_distance = to_light.norm(); - let light_incidence = to_light.normalize(); + let light_incidence = Unit::new_normalize(to_light); // Compute light falloff let falloff = 1.0 @@ -108,7 +109,7 @@ pub fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3 }; // Compute specular - let h = (light_incidence + incidence).normalize(); + let h = (&light_incidence.into_inner() + incidence.into_inner()).normalize(); let n_dot_h = normal.dot(&h); let specular = if n_dot_h > 0.0 { ks * n_dot_h.powf(shininess)