diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-01 12:24:35 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-01 12:24:35 +0700 |
commit | 2d7457db1c0c719de3e1f76161a35af3fb5ae0c9 (patch) | |
tree | 5851522043e7e990e90f6eab344caba5691df6f9 | |
parent | 60e688d89e822f74058e726f67f6ac804f9e2ca8 (diff) |
upd hit intersection simplified
-rw-r--r-- | src/renderer.rs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/renderer.rs b/src/renderer.rs index d27edb4..49d8046 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -14,6 +14,7 @@ fn ray_color(ray: &Ray) -> Color { normal.z + 1.0, ).mul_scalar(0.5); } + let unit_direction = ray.direction.unit(); let a = 0.5 * (unit_direction.y + 1.0); let color1 = Color::new(1.0, 1.0, 1.0).mul_scalar(1.0 - a); @@ -23,14 +24,14 @@ fn ray_color(ray: &Ray) -> Color { fn hit_sphere(center: &Point3, radius: f32, ray: &Ray) -> f32 { let oc = center.sub(&ray.origin); - let a = ray.direction.dot_prod(&ray.direction); - let b = -2.0 * ray.direction.dot_prod(&oc); - let c = oc.dot_prod(&oc) - radius * radius; - let discriminant = b * b - 4.0 * a * c; + let a = ray.direction.mag_sqr(); + let h = ray.direction.dot_prod(&oc); + let c = oc.mag_sqr() - radius * radius; + let discriminant = h * h - a * c; if discriminant < 0.0 { -1.0 } else { - (-b - discriminant.sqrt()) / (2.0 * a) + (h - discriminant.sqrt()) / a } } |