diff --git a/src/main/java/eu/jonahbauer/raytracing/math/Ray.java b/src/main/java/eu/jonahbauer/raytracing/math/Ray.java index cdddd36..ca3d79c 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/Ray.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/Ray.java @@ -12,9 +12,9 @@ public record Ray(@NotNull Vec3 origin, @NotNull Vec3 direction) { public @NotNull Vec3 at(double t) { return new Vec3( - origin().x() + t * direction.x(), - origin().y() + t * direction.y(), - origin().z() + t * direction.z() + Math.fma(t, direction.x(), origin.x()), + Math.fma(t, direction.y(), origin.y()), + Math.fma(t, direction.z(), origin.z()) ); } diff --git a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java index 0ddc26a..767febe 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java @@ -23,15 +23,20 @@ public record Vec3(double x, double y, double z) { public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) { var vec = new Vec3( - 2 * random.nextDouble() - 1, - 2 * random.nextDouble() - 1, - 2 * random.nextDouble() - 1 + Math.fma(2, random.nextDouble(), -1), + Math.fma(2, random.nextDouble(), -1), + Math.fma(2, random.nextDouble(), -1) ); return unit ? vec.unit() : vec; } public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) { - return vec.minus(normal.times(2 * normal.times(vec))); + var factor = - 2 * normal.times(vec); + return new Vec3( + Math.fma(factor, normal.x(), vec.x()), + Math.fma(factor, normal.y(), vec.y()), + Math.fma(factor, normal.z(), vec.z()) + ); } public static @NotNull Optional refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) { @@ -56,10 +61,11 @@ public record Vec3(double x, double y, double z) { } public static @NotNull Vec3 average(@NotNull Vec3 current, @NotNull Vec3 next, int index) { + var factor = 1d / index; return new Vec3( - current.x() + (next.x() - current.x()) / index, - current.y() + (next.y() - current.y()) / index, - current.z() + (next.z() - current.z()) / index + Math.fma(factor, next.x() - current.x(), current.x()), + Math.fma(factor, next.y() - current.y(), current.y()), + Math.fma(factor, next.z() - current.z(), current.z()) ); } diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableList.java b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableList.java index 06a060a..29b87d4 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableList.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/util/HittableList.java @@ -5,6 +5,7 @@ import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.scene.Hittable; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.List; public final class HittableList extends HittableCollection { @@ -12,7 +13,7 @@ public final class HittableList extends HittableCollection { private final @NotNull AABB bbox; public HittableList(@NotNull List objects) { - this.objects = List.copyOf(objects); + this.objects = new ArrayList<>(objects); this.bbox = AABB.getBoundingBox(this.objects).orElse(AABB.EMPTY); }