diff options
author | Julian T <julian@jtle.dk> | 2021-08-04 17:21:03 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-08-04 17:21:03 +0200 |
commit | 02fedfc9653cb37219849de7dae14ae13d2f6c36 (patch) | |
tree | d76c804f6fcd1d8c9aa036251843198bf78eadd2 /src/world/shapes | |
parent | 351d9132c0b2c54dfa9f50bfe328d25ccf059cea (diff) |
Add translation and move center position from sphere
Diffstat (limited to 'src/world/shapes')
-rw-r--r-- | src/world/shapes/sphere.rs | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs index fc2cfe2..36c9d13 100644 --- a/src/world/shapes/sphere.rs +++ b/src/world/shapes/sphere.rs @@ -3,35 +3,30 @@ //! Spheres are relatively easy to calculate intersections between use crate::{Float, NEAR_ZERO}; use crate::core::{Ray, Vector3f, Bound3f}; -use crate::world::{Hittable, DynHittable, Intersection}; +use crate::world::{Hittable, DynHittable, Intersection, Instancable}; pub struct Sphere { radius: Float, - center: Vector3f, } impl Sphere { - pub fn new(radius: Float, center: Vector3f) -> Sphere { + pub fn new(radius: Float) -> Sphere { Sphere { radius, - center, } } fn norm_at(&self, point: &Vector3f) -> Vector3f { - let mut v = *point - self.center; - v /= self.radius; - v + *point / self.radius } } impl Hittable for Sphere { // Implementation from ray tracing in a weekend fn intersect(&self, ray: &Ray) -> Option<Intersection> { - let oc = ray.origin - self.center; let a = ray.direction.len_squared(); - let half_b = oc.dot(&ray.direction); - let c = oc.len_squared() - self.radius * self.radius; + let half_b = ray.origin.dot(&ray.direction); + let c = ray.origin.len_squared() - self.radius * self.radius; let disc = half_b*half_b - a*c; if disc < 0.0 { @@ -67,18 +62,21 @@ impl Hittable for Sphere { /// use rendering::core::Vector3f; /// use rendering::world::{Hittable, shapes::Sphere}; /// - /// let sph = Sphere::new(1.0, Vector3f::new(0.0)); + /// let sph = Sphere::new(1.0); /// let b = sph.bounding_box(); /// /// assert!(b.min.x == -1.0 && b.min.y == -1.0 && b.min.z == -1.0); /// assert!(b.max.x == 1.0 && b.max.y == 1.0 && b.max.z == 1.0); + /// ``` fn bounding_box(&self) -> Bound3f { let offset = Vector3f::new(self.radius); - Bound3f::new(self.center - offset, self.center + offset) + Bound3f::new(-offset, offset) } } +impl Instancable for Sphere {} + impl Into<DynHittable> for Sphere { fn into(self) -> DynHittable { DynHittable::new(Box::new(self)) @@ -91,7 +89,7 @@ mod tests { #[test] fn sphere_intersect() { - let sph = Sphere::new(2.0, Vector3f::new_xyz(2.0, 3.0, 4.0)); + let sph = Sphere::new(2.0); let ray = Ray { origin: Vector3f::new_xyz(1.0, 0.0, 0.0), @@ -99,6 +97,7 @@ mod tests { }; let dist = sph.intersect(&ray).unwrap(); - assert!((dist.t - 3.28).abs() < 0.01); + println!("Yay {}", dist.t); + assert!((dist.t - 1.732).abs() < 0.01); } } |