Fix sphere intersection calculation error

This commit is contained in:
maddiebaka
2023-06-14 11:07:11 -04:00
parent dac57cc035
commit 2156e0ce68

View File

@@ -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<f64>, incident: Vec3<f64>, hit_point: Vec3<f64>, 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<f64>, 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::<f64>() * 250.0 * 10.0;
// let y: f64 = rng.gen::<f64>() * 250.0 * 10.0;
// let z: f64 = rng.gen::<f64>() * 250.0 * 10.0;
// let radius: f64 = rng.gen::<f64>() * 40.0 * 10.0;
// let red: f32 = rng.gen::<f32>() * 100.0;
// let green: f32 = rng.gen::<f32>() * 100.0;
// let blue: f32 = rng.gen::<f32>() * 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::<f64>() * 250.0 * 10.0;
let y: f64 = rng.gen::<f64>() * 250.0 * 10.0;
let z: f64 = rng.gen::<f64>() * 250.0 * 10.0;
let radius: f64 = rng.gen::<f64>() * 40.0 * 10.0;
let red: f32 = rng.gen::<f32>() * 100.0;
let green: f32 = rng.gen::<f32>() * 100.0;
let blue: f32 = rng.gen::<f32>() * 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::<f32>() }),
//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));