make scene deterministic

main
jbb01 6 months ago
parent 07cdc0c213
commit 828c332e76

@ -19,6 +19,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
@ -47,24 +48,25 @@ public class Main {
} }
private static @NotNull Scene getScene() { private static @NotNull Scene getScene() {
var rng = new Random(1);
var objects = new ArrayList<Hittable>(); var objects = new ArrayList<Hittable>();
objects.add(new Sphere(new Vec3(0, -1000, 0), 1000, new LambertianMaterial(new Color(0.5, 0.5, 0.5)))); 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 a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) { 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; if (Vec3.distance(center, new Vec3(4, 0.2, 0)) <= 0.9) continue;
Material material; Material material;
var rnd = Math.random(); var rnd = rng.nextDouble();
if (rnd < 0.8) { if (rnd < 0.8) {
// diffuse // diffuse
var albedo = Color.multiply(Color.random(), Color.random()); var albedo = Color.multiply(Color.random(rng), Color.random(rng));
material = new LambertianMaterial(albedo); material = new LambertianMaterial(albedo);
} else if (rnd < 0.95) { } else if (rnd < 0.95) {
// metal // metal
var albedo = Color.random(0.5, 1.0); var albedo = Color.random(rng, 0.5, 1.0);
var fuzz = Math.random() * 0.5; var fuzz = rng.nextDouble() * 0.5;
material = new MetallicMaterial(albedo, fuzz); material = new MetallicMaterial(albedo, fuzz);
} else { } else {
// glass // glass

@ -2,6 +2,8 @@ package eu.jonahbauer.raytracing.render;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Random;
public record Color(double r, double g, double b) { 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 BLACK = new Color(0.0, 0.0, 0.0);
public static final @NotNull Color WHITE = new Color(1.0, 1.0, 1.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()); return new Color(a.r() * b.r(), a.g() * b.g(), a.b() * b.b());
} }
public static @NotNull Color random() { public static @NotNull Color random(@NotNull Random random) {
return new Color(Math.random(), Math.random(), Math.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; var span = max - min;
return new Color( return new Color(
Math.fma(Math.random(), span, min), Math.fma(random.nextDouble(), span, min),
Math.fma(Math.random(), span, min), Math.fma(random.nextDouble(), span, min),
Math.fma(Math.random(), span, min) Math.fma(random.nextDouble(), span, min)
); );
} }

Loading…
Cancel
Save