diff --git a/src/main/java/eu/jonahbauer/raytracing/math/BoundingBox.java b/src/main/java/eu/jonahbauer/raytracing/math/BoundingBox.java new file mode 100644 index 0000000..e6f5ec0 --- /dev/null +++ b/src/main/java/eu/jonahbauer/raytracing/math/BoundingBox.java @@ -0,0 +1,10 @@ +package eu.jonahbauer.raytracing.math; + +import org.jetbrains.annotations.NotNull; + +public record BoundingBox(@NotNull Vec3 min, @NotNull Vec3 max) { + + public @NotNull Vec3 center() { + return Vec3.average(min, max, 2); + } +} diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java b/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java index 9dd1bd1..8065941 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/Hittable.java @@ -1,5 +1,6 @@ package eu.jonahbauer.raytracing.scene; +import eu.jonahbauer.raytracing.math.BoundingBox; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; import org.jetbrains.annotations.NotNull; @@ -14,4 +15,8 @@ public interface Hittable { * @param ray a ray */ @NotNull Optional hit(@NotNull Ray ray, @NotNull Range range); + + default @NotNull Optional getBoundingBox() { + return Optional.empty(); + } } diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java b/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java index 805aff6..cfe5446 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/Sphere.java @@ -1,6 +1,7 @@ package eu.jonahbauer.raytracing.scene; import eu.jonahbauer.raytracing.material.Material; +import eu.jonahbauer.raytracing.math.BoundingBox; import eu.jonahbauer.raytracing.math.Range; import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.math.Vec3; @@ -44,6 +45,14 @@ public record Sphere(@NotNull Vec3 center, double radius, @NotNull Material mate return Optional.of(new HitResult(t, position, frontFace ? normal : normal.times(-1), material, frontFace)); } + @Override + public @NotNull Optional getBoundingBox() { + return Optional.of(new BoundingBox( + center.minus(radius, radius, radius), + center.plus(radius, radius, radius) + )); + } + public @NotNull Sphere withCenter(@NotNull Vec3 center) { return new Sphere(center, radius, material); }