summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 18:16:50 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 18:16:50 +0700
commit202f7a315bcc0f7cd836348a29e9d06c7a4fa198 (patch)
tree899323bfdcbf6195862bf71ef9d07937b18e57c6
parent7c868a16da77f3aefbf0d33f14dd3d0ded542fc4 (diff)
upd: supposedly a ball appears
-rw-r--r--src/renderer.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index f712939..59a6b7b 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,7 +1,10 @@
-use crate::calculus::calculus::{Ray, Vec3};
+use crate::calculus::calculus::{Point3, Ray, Vec3};
use crate::global::{DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
fn ray_color(ray: &Ray) -> Vec3 {
+ if hit_sphere(&Point3 {x: 0f32, y: 0f32, z: -1f32}, 0.5, ray) {
+ return Vec3 {x: 1f32, y: 0f32, z: 0f32}
+ }
let unit_direction = ray.direction.unit();
let a = 0.5 * (unit_direction.y + 1.0);
let vec_1 = Vec3 { x:1.0, y: 1.0, z: 1.0 };
@@ -10,6 +13,15 @@ fn ray_color(ray: &Ray) -> Vec3 {
.add(&vec_2.scalar_mul(a))
}
+fn hit_sphere(center: &Point3, radius: f32, ray: &Ray) -> bool {
+ let oc = center.sub(&ray.origin);
+ let a = ray.direction.dot_prod(&ray.direction);
+ let b = -2.0 * ray.direction.dot_prod(&oc);
+ let mut c = oc.dot_prod(&oc) - radius * radius;
+ let discriminant = b * b - 4.0 * a * c;
+ discriminant >= 0.0
+}
+
pub fn render(display_buffer: &mut DisplayBuffer) {
let viewport_hor_vector = Vec3{ x: VIEWPORT_WIDTH as f32, y: 0.0, z: 0.0 };
let viewport_ver_vector = Vec3 { x: 0.0, y: -1f32 * VIEWPORT_HEIGHT as f32, z: 0.0 };