ft: 5,6.1: sphere class + normals
This commit is contained in:
25
src/objects/hit.rs
Normal file
25
src/objects/hit.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use crate::vec3::Vec3;
|
||||
|
||||
pub struct Hit {
|
||||
t: f32,
|
||||
p: Vec3,
|
||||
n: Vec3,
|
||||
}
|
||||
|
||||
impl Hit {
|
||||
pub fn new(t: f32, p: Vec3, n: Vec3) -> Hit {
|
||||
Hit { t: t, p: p, n: n }
|
||||
}
|
||||
|
||||
pub fn t(&self) -> &f32 {
|
||||
&self.t
|
||||
}
|
||||
|
||||
pub fn p(&self) -> &Vec3 {
|
||||
&self.p
|
||||
}
|
||||
|
||||
pub fn n(&self) -> &Vec3 {
|
||||
&self.n
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
11
src/objects/traits.rs
Normal file
11
src/objects/traits.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::objects::hit::Hit;
|
||||
use crate::Ray;
|
||||
use crate::Vec3;
|
||||
|
||||
pub trait Hittable {
|
||||
fn hit(&self, r: &Ray) -> Option<Hit>;
|
||||
}
|
||||
|
||||
pub trait Normal {
|
||||
fn normal_at(&self, p: &Vec3) -> Vec3;
|
||||
}
|
||||
Reference in New Issue
Block a user