From 2156e0ce68e2da9099fbc212665f69aafdf0e91c Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Wed, 14 Jun 2023 11:07:11 -0400 Subject: [PATCH] Fix sphere intersection calculation error --- src/main.rs | 57 +++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4588479..e6d26eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::mem; +use std::f32; use std::ops::{Add,Mul}; #[macro_use] @@ -217,10 +217,10 @@ impl Intersectable for Sphere { if t0 < 0.0 && t1 < 0.0 { None - //} else if t0 < 0.0 { - // Some(t1) - //} else if t1 < 0.0 { - // Some(t0) + } else if t0 < 0.0 { + Some(t1) + } else if t1 < 0.0 { + Some(t0) } else { let distance = if t0 < t1 { t0 } else { t1 }; Some(distance) @@ -281,7 +281,7 @@ impl Intersectable for Element { fn create_reflection(normal: Vec3, incident: Vec3, hit_point: Vec3, bias: f64) -> Ray { Ray { - pos: hit_point + (normal.normalize() * bias), + pos: hit_point + (normal.normalize()), dir: incident - (2.0 * incident.dot(&normal) * normal), } } @@ -327,8 +327,9 @@ fn shade_diffuse(camera: &OrthoCamera, object: &Element, hit_point: Vec3, s let light_intensity = if in_light { camera.light.intensity } else { 0.0 }; let light_power = (surface_normal.normalize().dot(&direction_to_light.normalize()) as f32).max(0.0); + let light_reflected = material.albedo / f32::consts::PI; - let light_color = light_intensity * light_power; + let light_color = light_intensity * light_power * light_reflected; color = color + (material.coloration * light_color); return color; @@ -356,24 +357,24 @@ fn main() { // camera.spheres.push(Sphere::new(Vec3::new(125.0, 75.0, 100.0), 20.0)); // camera.spheres.push(Sphere::new(Vec3::new(115.0, 175.0, 100.0), 60.0)); // camera.spheres.push(Sphere::new(Vec3::new(0.0, 0.0, 100.0), 10.0)); - //for i in 0..2 { - // let mut rng = rand::thread_rng(); - // let x: f64 = rng.gen::() * 250.0 * 10.0; - // let y: f64 = rng.gen::() * 250.0 * 10.0; - // let z: f64 = rng.gen::() * 250.0 * 10.0; - // let radius: f64 = rng.gen::() * 40.0 * 10.0; - // let red: f32 = rng.gen::() * 100.0; - // let green: f32 = rng.gen::() * 100.0; - // let blue: f32 = rng.gen::() * 100.0; - // let sphere = Sphere { - // pos: Vec3::new(x, y, 100.0), - // radius: radius, - // //material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Reflective { reflectivity: 0.2 }) - // material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Diffuse), - // }; - // camera.elements.push(Element::Sphere(sphere)); - // //camera.spheres.push(Sphere::new(Vec3::new(x, y, 100.0), radius)); - //} + for i in 0..15 { + let mut rng = rand::thread_rng(); + let x: f64 = rng.gen::() * 250.0 * 10.0; + let y: f64 = rng.gen::() * 250.0 * 10.0; + let z: f64 = rng.gen::() * 250.0 * 10.0; + let radius: f64 = rng.gen::() * 40.0 * 10.0; + let red: f32 = rng.gen::() * 100.0; + let green: f32 = rng.gen::() * 100.0; + let blue: f32 = rng.gen::() * 100.0; + let sphere = Sphere { + pos: Vec3::new(x, y, 100.0), + radius: radius, + material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Reflective { reflectivity: rng.gen::() }), + //material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Diffuse), + }; + camera.elements.push(Element::Sphere(sphere)); + //camera.spheres.push(Sphere::new(Vec3::new(x, y, 100.0), radius)); + } let back_plane = Plane { //pos: Vec3::new(0.0, 0.0, 100.0), @@ -396,15 +397,15 @@ fn main() { pos: Vec3::new(1280.0, 1290.0, 1000.0), radius: 300.0, //material: Material::new(Color::new(20.0, 20.0, 20.0), 2.0, SurfaceType::Diffuse), - material: Material::new(Color::new(255.0, 255.0, 255.0), 2.0, SurfaceType::Reflective { reflectivity: 0.9 }), + material: Material::new(Color::new(255.0, 255.0, 255.0), 2.0, SurfaceType::Reflective { reflectivity: 0.8 }), }; camera.elements.push(Element::Sphere(center_sphere)); let left_sphere = Sphere { pos: Vec3::new(200.0, 1800.0, 500.0), radius: 200.0, - //material: Material::new(Color::new(255.0, 20.0, 20.0), 2.0, SurfaceType::Reflective { reflectivity: 0.1 }) - material: Material::new(Color::new(20.0, 20.0, 200.0), 2.0, SurfaceType::Diffuse), + material: Material::new(Color::new(255.0, 20.0, 20.0), 2.0, SurfaceType::Reflective { reflectivity: 0.1 }), + //material: Material::new(Color::new(20.0, 20.0, 200.0), 2.0, SurfaceType::Diffuse), }; camera.elements.push(Element::Sphere(left_sphere));