aboutsummaryrefslogtreecommitdiff
path: root/src/world/hittable.rs
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-21 18:01:56 +0100
committerJulian T <julian@jtle.dk>2021-02-21 18:01:56 +0100
commitda1c3949a449f3fafe579c62ff6b14ffd993a197 (patch)
tree754df5c9b5e9f0fa0a8bb7a8cd3dd4b12fe5ad89 /src/world/hittable.rs
parentc695da871a75bb6786c08c3546ef71ed032bd61d (diff)
Add 3d bounding box and merged SceneIntersection and Intersection
Diffstat (limited to 'src/world/hittable.rs')
-rw-r--r--src/world/hittable.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/world/hittable.rs b/src/world/hittable.rs
new file mode 100644
index 0000000..720a019
--- /dev/null
+++ b/src/world/hittable.rs
@@ -0,0 +1,33 @@
+use crate::core::{Vector3f, Bound3f, Ray};
+use crate::Float;
+use crate::material::Material;
+
+/// Returns the context of a intersection
+pub struct Intersection<'a> {
+ /// Normal vector at intersection
+ pub n: Vector3f,
+ pub p: Vector3f,
+ pub t: Float,
+ pub m: Option<&'a dyn Material>,
+}
+
+impl Intersection<'_> {
+ pub fn norm_against_ray(&self, r: &Ray) -> Vector3f {
+ if self.n.dot(&r.direction) < 0.0 {
+ self.n
+ } else {
+ -self.n
+ }
+ }
+}
+
+/// Defines a common trait for objects in the scene
+pub trait Hittable: Sync + Send {
+ /// Returns the intersection with ray
+ fn intersect(&self, ray: &Ray) -> Option<Intersection>;
+
+ /// Returns the axis alligned bounding box containing self
+ fn bounding_box(&self) -> Option<Bound3f> {
+ None
+ }
+}