ft: 6.5: hitlist
This commit is contained in:
27
src/main.rs
27
src/main.rs
@@ -9,26 +9,14 @@ use std::cmp::max;
|
||||
|
||||
use crate::objects::hit::Hit;
|
||||
use crate::objects::sphere::Sphere;
|
||||
use crate::objects::traits::{Hittable, Normal};
|
||||
use crate::objects::traits::Hittable;
|
||||
use crate::ray::Ray;
|
||||
use crate::vec3::{Colour, Vec3};
|
||||
use log::info;
|
||||
use pretty_env_logger;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ray_colour<T: Hittable>(hittables: &Vec<T>, r: &Ray) -> Colour {
|
||||
let closest = Hit::hit_list(hittables, r);
|
||||
if let Some(hit) = closest {
|
||||
let n = hit.n();
|
||||
return 0.5 * Colour::new(n.x() + 1., n.y() + 1., n.z() + 1.);
|
||||
@@ -42,12 +30,13 @@ where
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// use structs so rust-analyzer can provide lsp
|
||||
let s = Sphere::new(Vec3::new(0., 0., -1.), 0.5);
|
||||
let objects = vec![s];
|
||||
|
||||
pretty_env_logger::init();
|
||||
|
||||
// setup objects
|
||||
let mut objects = vec![Sphere::new(Vec3::new(0., 0., -1.), 0.5)];
|
||||
objects.push(Sphere::new(Vec3::new(0., 0., -0.1), 0.01));
|
||||
objects.push(Sphere::new(Vec3::new(0., -100.5, -1.), 100.));
|
||||
|
||||
let aspect_ratio = 16. / 9.;
|
||||
let image_width = 400;
|
||||
let image_height = max(1, (image_width as f32 / aspect_ratio) as u32);
|
||||
|
||||
Reference in New Issue
Block a user