diff --git a/src/main/java/eu/jonahbauer/raytracing/Main.java b/src/main/java/eu/jonahbauer/raytracing/Main.java index 93187d8..e40c3f2 100644 --- a/src/main/java/eu/jonahbauer/raytracing/Main.java +++ b/src/main/java/eu/jonahbauer/raytracing/Main.java @@ -19,6 +19,7 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Random; public class Main { public static void main(String[] args) throws IOException { @@ -47,24 +48,25 @@ public class Main { } private static @NotNull Scene getScene() { + var rng = new Random(1); var objects = new ArrayList(); objects.add(new Sphere(new Vec3(0, -1000, 0), 1000, new LambertianMaterial(new Color(0.5, 0.5, 0.5)))); for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { - var center = new Vec3(a + 0.9 * Math.random(), 0.2, b + 0.9 * Math.random()); + var center = new Vec3(a + 0.9 * rng.nextDouble(), 0.2, b + 0.9 * rng.nextDouble()); if (Vec3.distance(center, new Vec3(4, 0.2, 0)) <= 0.9) continue; Material material; - var rnd = Math.random(); + var rnd = rng.nextDouble(); if (rnd < 0.8) { // diffuse - var albedo = Color.multiply(Color.random(), Color.random()); + var albedo = Color.multiply(Color.random(rng), Color.random(rng)); material = new LambertianMaterial(albedo); } else if (rnd < 0.95) { // metal - var albedo = Color.random(0.5, 1.0); - var fuzz = Math.random() * 0.5; + var albedo = Color.random(rng, 0.5, 1.0); + var fuzz = rng.nextDouble() * 0.5; material = new MetallicMaterial(albedo, fuzz); } else { // glass diff --git a/src/main/java/eu/jonahbauer/raytracing/render/Color.java b/src/main/java/eu/jonahbauer/raytracing/render/Color.java index 0c77b61..a1e9042 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/Color.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/Color.java @@ -2,6 +2,8 @@ package eu.jonahbauer.raytracing.render; import org.jetbrains.annotations.NotNull; +import java.util.Random; + public record Color(double r, double g, double b) { public static final @NotNull Color BLACK = new Color(0.0, 0.0, 0.0); public static final @NotNull Color WHITE = new Color(1.0, 1.0, 1.0); @@ -24,16 +26,16 @@ public record Color(double r, double g, double b) { return new Color(a.r() * b.r(), a.g() * b.g(), a.b() * b.b()); } - public static @NotNull Color random() { - return new Color(Math.random(), Math.random(), Math.random()); + public static @NotNull Color random(@NotNull Random random) { + return new Color(random.nextDouble(), random.nextDouble(), random.nextDouble()); } - public static @NotNull Color random(double min, double max) { + public static @NotNull Color random(@NotNull Random random, double min, double max) { var span = max - min; return new Color( - Math.fma(Math.random(), span, min), - Math.fma(Math.random(), span, min), - Math.fma(Math.random(), span, min) + Math.fma(random.nextDouble(), span, min), + Math.fma(random.nextDouble(), span, min), + Math.fma(random.nextDouble(), span, min) ); }