ft: 5,6.1: sphere class + normals
This commit is contained in:
44
src/objects/sphere.rs
Normal file
44
src/objects/sphere.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use core::f32::math::sqrt;
|
||||
|
||||
use crate::objects::hit::Hit;
|
||||
use crate::objects::traits::{Hittable, Normal};
|
||||
use crate::Vec3;
|
||||
|
||||
pub struct Sphere {
|
||||
center: Vec3,
|
||||
radius: f32,
|
||||
}
|
||||
|
||||
impl Sphere {
|
||||
pub fn new(center: Vec3, radius: f32) -> Sphere {
|
||||
Sphere {
|
||||
center: center,
|
||||
radius: radius,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for Sphere {
|
||||
fn hit(&self, r: &crate::ray::Ray) -> Option<Hit> {
|
||||
let oc = self.center - r.origin();
|
||||
let a = r.dir().dot(r.dir());
|
||||
let b = -2. * r.dir().dot(&oc);
|
||||
let c = oc.dot(&oc) - self.radius * self.radius;
|
||||
|
||||
let d = b * b - 4. * a * c;
|
||||
|
||||
if d < 0. {
|
||||
None
|
||||
} else {
|
||||
let t = (-b - sqrt(d)) / (2.0 * a);
|
||||
let p = r.at(t);
|
||||
Some(Hit::new(t, p, self.normal_at(&p)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Normal for Sphere {
|
||||
fn normal_at(&self, p: &Vec3) -> Vec3 {
|
||||
(*p - self.center).get_unit()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user