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 | |
parent | 351d9132c0b2c54dfa9f50bfe328d25ccf059cea (diff) |
Add translation and move center position from sphere
-rw-r--r-- | src/core/bound3.rs | 8 | ||||
-rw-r--r-- | src/core/vector3.rs | 13 | ||||
-rw-r--r-- | src/main.rs | 14 | ||||
-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 |
7 files changed, 114 insertions, 23 deletions
diff --git a/src/core/bound3.rs b/src/core/bound3.rs index ce6bb09..8c467c0 100644 --- a/src/core/bound3.rs +++ b/src/core/bound3.rs @@ -64,6 +64,13 @@ impl<T: Number> Bound3<T> { let diag = self.max - self.min; diag.x * diag.y * diag.z } + + pub fn offset(&self, offset: Vector3<T>) -> Self { + Self { + min: self.min + offset, + max: self.max + offset, + } + } } impl Bound3f { @@ -86,7 +93,6 @@ impl Bound3f { /// assert!(!b.intersect(&r3, 0.0, INFTY)); /// ``` pub fn intersect(&self, ray: &Ray, t_min: Float, t_max: Float) -> bool { - println!("BIN: {} -> {}", self.min, self.max); // Method stolen from Ray tracing the next week. // They mention its from pixar for i in 0..3 { diff --git a/src/core/vector3.rs b/src/core/vector3.rs index 1cc6f60..9ba0191 100644 --- a/src/core/vector3.rs +++ b/src/core/vector3.rs @@ -2,7 +2,7 @@ //! //! Also add more 3d math things needed for shading and 3d calculations. use crate::{Float, Number, NEAR_ZERO}; -use std::ops::{Mul, Sub, Add, DivAssign, Neg, AddAssign, Index}; +use std::ops::{Mul, Sub, Add, Div, DivAssign, Neg, AddAssign, Index}; use std::fmt; #[derive(Clone, Copy)] @@ -73,6 +73,17 @@ impl<T: Number> Mul<T> for Vector3<T> { } } +impl<T: Number> Div<T> for Vector3<T> { + type Output = Self; + fn div(self, op: T) -> Self::Output { + Self::Output::new_xyz( + self.x / op, + self.y / op, + self.z / op, + ) + } +} + impl<T: Number> Neg for Vector3<T> { type Output = Self; diff --git a/src/main.rs b/src/main.rs index f98a806..221752b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use rendering::camera::{Camera, Film, CameraSettings}; -use rendering::world::{Scene, Object, shapes::Sphere}; +use rendering::world::{Scene, Object, shapes::Sphere, Instancable}; use rendering::trace::DefaultTracer; use rendering::core::{Vector2i, Vector3f, Spectrum}; use rendering::render::{RenderContext, RenderCoord}; @@ -31,12 +31,12 @@ fn main() { let mut scn = Scene::new(); scn.add_objects(vec![ - Object::new(glass, Sphere::new(0.2, Vector3f::new_xyz(0.0, 0.0, -1.0))), - Object::new(blue, Sphere::new(0.5, Vector3f::new_xyz(1.0, 0.0, -1.5))), - Object::new(green, Sphere::new(0.3, Vector3f::new_xyz(0.5, 0.0, -2.5))), - Object::new(brown, Sphere::new(100.0, Vector3f::new_xyz(0.0, -100.5, -1.0))), - Object::new(metal, Sphere::new(0.2, Vector3f::new_xyz(-0.5, 0.0, -1.0))), - Object::new(sun, Sphere::new(0.4, Vector3f::new_xyz(-1.0, 3.0, 0.0))), + Object::new(glass, Sphere::new(0.2).translate(0.0, 0.0, -1.0)), + Object::new(blue, Sphere::new(0.5).translate(1.0, 0.0, -1.5)), + Object::new(green, Sphere::new(0.3).translate(0.5, 0.0, -2.5)), + Object::new(brown, Sphere::new(100.0).translate(0.0, -100.5, -1.0)), + Object::new(metal, Sphere::new(0.2).translate(-0.5, 0.0, -1.0)), + Object::new(sun, Sphere::new(0.4).translate(-1.0, 3.0, 0.0)), ]); let tracer = DefaultTracer::new(&scn, Some(50), 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); } } |