ft: 5,6.1: sphere class + normals
This commit is contained in:
32
src/main.rs
32
src/main.rs
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user