diff options
author | Julian T <julian@jtle.dk> | 2021-08-06 11:58:49 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-08-06 11:58:49 +0200 |
commit | 978580ff9464f7470c46f3084ee26021b71933c8 (patch) | |
tree | bb4d42fb5f1730a63a019727aee0ed2828e7033d /src/world/shapes | |
parent | 19914dfc7d8ce04f36539abd92f10e52baa7f740 (diff) |
Add other axis rectangles and render empty box
Diffstat (limited to 'src/world/shapes')
-rw-r--r-- | src/world/shapes/rectangle.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/world/shapes/rectangle.rs b/src/world/shapes/rectangle.rs index 313ebb8..1630c00 100644 --- a/src/world/shapes/rectangle.rs +++ b/src/world/shapes/rectangle.rs @@ -6,6 +6,8 @@ const OUT_XY: Vector3f = Vector3f { x: 0.0, y: 0.0, z: 1.0 }; pub enum Plane { XY, + XZ, + YZ, } pub struct Rect { @@ -35,19 +37,20 @@ impl Rect { impl Hittable for Rect { fn intersect(&self, ray: &Ray) -> Option<Intersection> { - let t = (self.offset-ray.origin.z) / ray.direction.z; + let t = (self.offset - self.plane.translate(ray.origin).z) + / self.plane.translate(ray.direction).z; if t <= NEAR_ZERO { return None; } - let poi = ray.at(t); + let poi = self.plane.translate(ray.at(t)); // Check if at is inside rectangle on plane if poi.x.abs() > (self.d1/2.0) || poi.y.abs() > (self.d2/2.0) { // No collision return None; } - Some(Intersection::new(OUT_XY, poi, ray, t)) + Some(Intersection::new(self.plane.translate_inv(OUT_XY), self.plane.translate_inv(poi), ray, t)) } fn bounding_box(&self) -> Bound3f { @@ -68,9 +71,19 @@ impl Into<DynHittable> for Rect { impl Instancable for Rect {} impl Plane { - pub fn switch(&self, v: Vector3f) -> Vector3f { + pub fn translate(&self, v: Vector3f) -> Vector3f { match self { Plane::XY => v, + Plane::XZ => Vector3f::new_xyz(v.x, v.z, v.y), + Plane::YZ => Vector3f::new_xyz(v.y, v.z, v.x), + } + } + + pub fn translate_inv(&self, v: Vector3f) -> Vector3f { + match self { + Plane::XY => v, + Plane::XZ => Vector3f::new_xyz(v.x, v.z, v.y), + Plane::YZ => Vector3f::new_xyz(v.z, v.x, v.y), } } } |