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 | |
parent | 351d9132c0b2c54dfa9f50bfe328d25ccf059cea (diff) |
Add translation and move center position from sphere
Diffstat (limited to 'src/world')
-rw-r--r-- | src/world/hittable.rs | 3 | ||||
-rw-r--r-- | src/world/instancing.rs | 69 | ||||
-rw-r--r-- | src/world/mod.rs | 3 | ||||
-rw-r--r-- | src/world/shapes/sphere.rs | 27 |
4 files changed, 88 insertions, 14 deletions
diff --git a/src/world/hittable.rs b/src/world/hittable.rs index 4fbd3d0..9713c7c 100644 --- a/src/world/hittable.rs +++ b/src/world/hittable.rs @@ -1,6 +1,7 @@ use crate::core::{Vector3f, Bound3f, Ray}; use crate::Float; use crate::material::Material; +use crate::world::Instancable; use std::ops::Deref; @@ -57,3 +58,5 @@ impl Deref for DynHittable { self.0.as_ref() } } + +impl Instancable for DynHittable {} diff --git a/src/world/instancing.rs b/src/world/instancing.rs new file mode 100644 index 0000000..0a92746 --- /dev/null +++ b/src/world/instancing.rs @@ -0,0 +1,69 @@ +use crate::Float; +use crate::core::{Ray, Vector3f, Bound3f}; +use crate::world::{Hittable, DynHittable, Intersection}; + +pub struct Instance { + inner: DynHittable, + translate: Vector3f, +} + +pub trait Instancable: Into<DynHittable> { + fn translate(self, x: Float, y: Float, z: Float) -> Instance { + self.translate_vec(Vector3f::new_xyz(x, y, z)) + } + + fn translate_vec(self, offset: Vector3f) -> Instance { + Instance::new_translate(self.into(), offset) + } +} + +impl Instance { + pub fn new_translate(inner: DynHittable, offset: Vector3f) -> Self { + Self { + inner, + translate: offset, + } + } +} + +impl Hittable for Instance { + fn intersect(&self, ray: &Ray) -> Option<Intersection> { + let ray = Ray::new(ray.origin - self.translate, ray.direction); + + self.inner.intersect(&ray).map(|mut int| { + int.p += &self.translate; + int + }) + } + + fn bounding_box(&self) -> Bound3f { + self.inner.bounding_box().offset(self.translate) + } +} + +impl Into<DynHittable> for Instance { + fn into(self) -> DynHittable { + DynHittable::new(Box::new(self)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::world::shapes::Sphere; + + #[test] + fn sphere_translation() { + //let sph = Sphere::new(2.0).translate(2.0, 3.0, 4.0); + let sph = Sphere::new(2.0).translate(2.0, 3.0, 4.0); + + let ray = Ray { + origin: Vector3f::new_xyz(1.0, 0.0, 0.0), + direction: Vector3f::new_xyz(0.0, 1.0, 1.5).norm(), + }; + + let dist = sph.intersect(&ray).unwrap(); + println!("hey {}", dist.t); + assert!((dist.t - 3.28).abs() < 0.01); + } +} diff --git a/src/world/mod.rs b/src/world/mod.rs index 0779551..dd96b91 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -4,9 +4,12 @@ pub mod shapes; mod scene; pub mod container; mod hittable; +mod instancing; + pub use scene::*; pub use hittable::{Intersection, Hittable, DynHittable}; pub use shapes::Shape; +pub use instancing::{Instance, Instancable}; use std::sync::Arc; use crate::material::Material; 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); } } |