latest changes
This commit is contained in:
29
src/hittable.rs
Normal file
29
src/hittable.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::ray::*;
|
||||
use crate::vec3::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HitRecord {
|
||||
pub p: Point3,
|
||||
pub normal: Vec3,
|
||||
pub t: f64,
|
||||
pub front_face: bool,
|
||||
}
|
||||
|
||||
impl HitRecord {
|
||||
pub fn new_empty() -> Self {
|
||||
HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false }
|
||||
}
|
||||
|
||||
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) {
|
||||
self.front_face = r.direction().dot(outward_normal) < 0.0;
|
||||
self.normal = if self.front_face {
|
||||
*outward_normal
|
||||
} else {
|
||||
*outward_normal * -1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Hittable {
|
||||
fn hit(&self, r: &Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool;
|
||||
}
|
||||
29
src/hittable_list.rs
Normal file
29
src/hittable_list.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::hittable::*;
|
||||
|
||||
struct HittableList {
|
||||
pub objects: Vec<Box<dyn Hittable>>
|
||||
}
|
||||
|
||||
impl HittableList {
|
||||
pub fn add(&mut self, object: Box<dyn Hittable>) {
|
||||
self.objects.push(object);
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.objects.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for HittableList {
|
||||
fn hit(&self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
|
||||
let mut temp_rec = HitRecord::new_empty();
|
||||
let mut hit_anything = false;
|
||||
let mut closest_so_far = t_max;
|
||||
|
||||
for object in &self.objects {
|
||||
|
||||
}
|
||||
|
||||
hit_anything
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
pub mod vec3;
|
||||
pub mod color;
|
||||
pub mod ray;
|
||||
pub mod sphere;
|
||||
pub mod hittable;
|
||||
pub mod hittable_list;
|
||||
|
||||
use std::{io::{BufWriter, Write}, fs::File};
|
||||
use crate::{
|
||||
vec3::*,
|
||||
color::*,
|
||||
ray::*
|
||||
ray::*,
|
||||
};
|
||||
|
||||
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
|
||||
|
||||
42
src/sphere.rs
Normal file
42
src/sphere.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::hittable::*;
|
||||
use crate::vec3::*;
|
||||
|
||||
struct Sphere {
|
||||
center: Point3,
|
||||
radius: f64
|
||||
}
|
||||
|
||||
impl Sphere {
|
||||
pub fn new(center: Point3, radius: f64) -> Self {
|
||||
Sphere {center, radius}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for Sphere {
|
||||
fn hit(&self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
|
||||
let oc = r.origin() - self.center;
|
||||
let a = r.direction().length_squared();
|
||||
let half_b = oc.dot(&r.direction());
|
||||
let c = oc.length_squared() - self.radius*self.radius;
|
||||
|
||||
let discriminant = half_b*half_b - a*c;
|
||||
if discriminant < 0.0 { return false; }
|
||||
let sqrtd = discriminant.sqrt();
|
||||
|
||||
// Find the nearest root that lies in the acceptable range
|
||||
let mut root = (-half_b - sqrtd) / a;
|
||||
if root < t_min || t_max < root {
|
||||
root = (-half_b + sqrtd) / a;
|
||||
if root < t_min || t_max < root {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
rec.t = root;
|
||||
rec.p = r.at(rec.t);
|
||||
let outward_normal = (rec.p - self.center) / self.radius;
|
||||
rec.set_face_normal(r, &outward_normal);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user