diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-01 16:43:50 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-01 16:43:50 +0700 |
commit | 084875ac8e9aa12cf6cc1892def9e4bc5fd4cdda (patch) | |
tree | 99fbc7b743fdf7a2b26ded0e8073c9a02be5240b /src/renderer.rs | |
parent | 2d7457db1c0c719de3e1f76161a35af3fb5ae0c9 (diff) |
object list, something is still wrong
Diffstat (limited to 'src/renderer.rs')
-rw-r--r-- | src/renderer.rs | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/src/renderer.rs b/src/renderer.rs index 49d8046..2fb76d2 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,18 +1,18 @@ use crate::calculus::calculus::{Point3, Ray, Vec3}; use crate::global::{Color, DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH}; +use crate::object::{HitRecord, Hittable, HittableList, Sphere}; -fn ray_color(ray: &Ray) -> Color { - let t = hit_sphere(&Point3 {x: 0.0, y: 0.0, z: -1.0}, 0.5, ray); - if t > 0.0 { - let normal = ray - .at(t) - .sub(&Vec3 {x: 0.0, y: 0.0, z: -1.0}) - .unit(); - return Color::new( - normal.x + 1.0, - normal.y + 1.0, - normal.z + 1.0, - ).mul_scalar(0.5); +fn ray_color(ray: &Ray, world: &HittableList) -> Color { + let mut rec = HitRecord::default(); + if world.hit(ray, 0.0, f32::INFINITY, &mut rec) { + let color = Color::new(1.0, 1.0, 1.0) + .add(&Color::new( + rec.normal.x, + rec.normal.y, + rec.normal.z) + ) + .mul_scalar(0.5); + return color; } let unit_direction = ray.direction.unit(); @@ -22,22 +22,9 @@ fn ray_color(ray: &Ray) -> Color { color1.add(&color2) } -fn hit_sphere(center: &Point3, radius: f32, ray: &Ray) -> f32 { - let oc = center.sub(&ray.origin); - 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 { - (h - discriminant.sqrt()) / a - } -} - 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 }; + let viewport_hor_vector = Vec3{ x: VIEWPORT_WIDTH, y: 0.0, z: 0.0 }; + let viewport_ver_vector = Vec3 { x: 0.0, y: -1f32 * VIEWPORT_HEIGHT, z: 0.0 }; let delta_pixel_u = viewport_hor_vector.scalar_mul(1.0 / IMG_WIDTH as f32); let delta_pixel_v = viewport_ver_vector.scalar_mul(1.0 / IMG_HEIGHT as f32); @@ -50,6 +37,17 @@ pub fn render(display_buffer: &mut DisplayBuffer) { &delta_pixel_u.add(&delta_pixel_u).scalar_mul(0.5) ); + let mut world = HittableList::new(); + + world.push( + Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5) + ); + + // world.push( + // Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0) + // ); + + (0..IMG_HEIGHT).for_each(|j| { (0..IMG_WIDTH).for_each(|i| { let pixel_center = pixel_upper_left @@ -60,7 +58,7 @@ pub fn render(display_buffer: &mut DisplayBuffer) { origin: &CAMERA_CENTER, direction: &ray_direction, }; - let color = ray_color(&ray); + let color = ray_color(&ray, &world); display_buffer[j][i] = Pixel::from_color(&color); }) }) |