42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
use crate::hittable;
|
|
use crate::types_defined::{HitRecord, Point, Ray, Sphere};
|
|
|
|
impl Sphere {
|
|
pub fn new(c: Point, r: f32) -> Self {
|
|
Self {
|
|
center: c,
|
|
radius: r,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl hittable::Hittable for Sphere {
|
|
fn hit(self, r: &Ray, t_min: f32, t_max: f32) -> HitRecord {
|
|
let a: f32 = r.direction.length_squared();
|
|
let h = r.direction.dot(self.center - r.point);
|
|
// let b = -2.0 * r.direction.dot(sphere_center - r.point);
|
|
let c = (self.center - r.point).length_squared() - self.radius * self.radius;
|
|
|
|
let discriminant = h * h - a * c;
|
|
|
|
// // 两个焦点
|
|
let disc_sqrt = discriminant.sqrt();
|
|
let near = (h - disc_sqrt) / a;
|
|
let far = (h + disc_sqrt) / a;
|
|
|
|
let mut root = near;
|
|
if root <= t_min || root >= t_max {
|
|
root = far;
|
|
if root <= t_min || root >= t_max {
|
|
root = -1.0;
|
|
}
|
|
}
|
|
|
|
HitRecord {
|
|
t: root,
|
|
p: r.at(root),
|
|
normal: r.at(root) - self.center,
|
|
}
|
|
}
|
|
}
|