diff --git a/.gitignore b/.gitignore index e7594a5..e1d680e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.ppm .idea *.iml +.vscode/ diff --git a/src/hittable.rs b/src/hittable.rs index 2c060bc..51f25b6 100644 --- a/src/hittable.rs +++ b/src/hittable.rs @@ -42,16 +42,16 @@ impl Hittable for Sphere { let discriminant = half_b * half_b - a * c; if discriminant < 0.0 { return None; } - let sqrtd = f64::sqrt(discriminant); - let root = (-half_b - sqrtd) / a; + let sqrtd = discriminant.sqrt(); + let mut root = (-half_b - sqrtd) / a; if root < t_min || t_max < root { - let root = (-half_b + sqrtd) / a; + root = (-half_b + sqrtd) / a; if root < t_min || t_max < root { return None; } } - let point = ray.at(root); let normal = (point - self.center) / self.radius; - let front_face = ray.direction().dot(&normal) < 0.0; + let dot = ray.direction().dot(&normal); + let front_face = dot < 0.0; let normal = if front_face { normal } else { -normal }; Some(HitRecord { point, diff --git a/src/material.rs b/src/material.rs index 20354ba..5676a16 100644 --- a/src/material.rs +++ b/src/material.rs @@ -104,6 +104,7 @@ impl Scatterable for Dielectric { self.index_of_refraction }; let unit_direction = ray.direction().unit_vector(); + let cos_theta = ((-unit_direction).dot(&hit_record.normal)).min(1.0); let sin_theta = (1.0 - cos_theta*cos_theta).sqrt(); let cannot_refract = refraction_ratio * sin_theta > 1.0; @@ -112,12 +113,10 @@ impl Scatterable for Dielectric { if cannot_refract || reflectance > rng.gen::() { let reflected = unit_direction.reflected(&hit_record.normal); - let reflected = Vec3::new(-reflected.x(), reflected.y(), -reflected.z()); let scattered = Ray::new(hit_record.point, reflected); Some((Some(scattered), color)) } else { - //let direction = unit_direction.refract(&hit_record.normal, refraction_ratio); - let direction = unit_direction.refract_sort_of_works(&hit_record.normal, refraction_ratio); + let direction = unit_direction.refract(&hit_record.normal, refraction_ratio); let scattered = Ray::new(hit_record.point, direction); Some((Some(scattered), color)) } diff --git a/src/ray.rs b/src/ray.rs index 7746b6c..ecf6684 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -3,6 +3,7 @@ use crate::hittable::HitRecord; use crate::material::Scatterable; use crate::vec3::Point3; +#[derive(Debug)] pub struct Ray { origin: Point3, direction: Vec3 diff --git a/src/vec3.rs b/src/vec3.rs index 7072ea7..88c26fa 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -84,21 +84,6 @@ impl Vec3 { let out_parallel = r * (*normal); out_perp + out_parallel } - pub fn refract_sort_of_works(&self, normal: &Vec3, etai_over_etat: f64) -> Vec3 { - let dot = (-(*self)).dot(normal); - let cos_theta = dot.min(1.0); - let out_perp = etai_over_etat * ((*self) + cos_theta * (*normal)); - let inner = 1.0 - out_perp.length_squared(); - let abs = inner.abs(); - let r = -(abs.sqrt()); - let out_parallel = r * (*normal); - // Why? - let mut res = out_perp + out_parallel; - res.x = -res.x; - //res.y = -res.y; - res.z = -res.z; - res - } } #[test]