diff --git a/src/main/java/eu/jonahbauer/raytracing/math/AABB.java b/src/main/java/eu/jonahbauer/raytracing/math/AABB.java index 38a4f1f..cdb8219 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/AABB.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/AABB.java @@ -55,7 +55,7 @@ public record AABB(@NotNull Vec3 min, @NotNull Vec3 max) { return new AABB(Vec3.min(this.min, box.min), Vec3.max(this.max, box.max)); } - public boolean hit(@NotNull Ray ray) { + public boolean hit(@NotNull Ray ray, @NotNull Range range) { var origin = ray.origin(); var direction = ray.direction(); var invDirection = direction.inv(); @@ -82,7 +82,7 @@ public record AABB(@NotNull Vec3 min, @NotNull Vec3 max) { } } - return tlmax < tumin; + return tlmax < tumin && tumin >= range.min() && tlmax <= range.max(); } public static double @NotNull[] intersect(@NotNull Vec3 corner, @NotNull Vec3 origin, @NotNull Vec3 invDirection) { diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java index c9a1582..5675318 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java @@ -61,9 +61,12 @@ public record Box(@NotNull AABB box, @NotNull Material material) implements Hitt } } - if (tlmax >= tumin) return Optional.empty(); - assert entry != null && exit != null; - return hit0(tlmax, tumin, entry, exit, ray, range); + if (tlmax < tumin && tumin >= range.min() && tlmax <= range.max()) { + assert entry != null && exit != null; + return hit0(tlmax, tumin, entry, exit, ray, range); + } else { + return Optional.empty(); + } } private @NotNull Optional hit0(double tmin, double tmax, @NotNull Side entry, @NotNull Side exit, @NotNull Ray ray, @NotNull Range range) { diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableBinaryTree.java b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableBinaryTree.java index 2eacad0..7415a28 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableBinaryTree.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableBinaryTree.java @@ -47,7 +47,7 @@ public final class HittableBinaryTree extends HittableCollection { @Override public void hit(@NotNull Ray ray, @NotNull State state) { - if (!bbox.hit(ray)) return; + if (!bbox.hit(ray, state.getRange())) return; if (left instanceof HittableCollection coll) { coll.hit(ray, state); } else if (left != null) { diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableCollection.java b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableCollection.java index 682f9a2..35904cd 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableCollection.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableCollection.java @@ -41,6 +41,10 @@ public abstract class HittableCollection implements Hittable { this.range = Objects.requireNonNull(range); } + public @NotNull Range getRange() { + return range; + } + private @NotNull Optional getResult() { return Optional.ofNullable(result); }