ft: 5,6.1: sphere class + normals

This commit is contained in:
2026-04-15 13:32:29 +02:00
parent 67c1eb861d
commit ebf39d014b
8 changed files with 126 additions and 12 deletions

View File

@@ -1,16 +1,40 @@
#![feature(core_float_math)]
mod objects;
mod ray;
mod vec3;
use core::f32;
use std::cmp::max;
use crate::objects::hit::Hit;
use crate::objects::sphere::Sphere;
use crate::objects::traits::{Hittable, Normal};
use crate::ray::Ray;
use crate::vec3::{Colour, Vec3};
use log::info;
use pretty_env_logger;
fn ray_colour(r: &Ray) -> Colour {
fn ray_colour<T>(hittables: &Vec<T>, r: &Ray) -> Colour
where
T: Hittable + Normal,
{
let mut closest: Option<Hit> = None;
for hittable in hittables {
if let Some(hit) = hittable.hit(r) {
// hit happens in front of camera and is closer than closest
if *hit.t() > 0. && (closest.is_none() || closest.as_ref().unwrap().t() > hit.t()) {
closest = Some(hit);
}
}
}
if let Some(hit) = closest {
let n = hit.n();
return 0.5 * Colour::new(n.x() + 1., n.y() + 1., n.z() + 1.);
}
// background
let unit_dir = r.dir().get_unit();
let a = 0.5 * (unit_dir.y() + 1.);
@@ -19,8 +43,8 @@ fn ray_colour(r: &Ray) -> Colour {
fn main() {
// use structs so rust-analyzer can provide lsp
let v = Vec3::new(255., 0., 255.);
let _ = Ray::new(v.clone(), v.clone());
let s = Sphere::new(Vec3::new(0., 0., -1.), 0.5);
let objects = vec![s];
pretty_env_logger::init();
@@ -56,7 +80,7 @@ fn main() {
let r = Ray::new(camera_center, ray_dir);
let pixel = imgbuf.get_pixel_mut(i, j);
*pixel = ray_colour(&r).output();
*pixel = ray_colour(&objects, &r).output();
}
}