From ebbf711403a081ccd5f4cd96bf19d39b6dad6284 Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:13:17 +0200 Subject: [PATCH] add cornell box --- .../java/eu/jonahbauer/raytracing/Main.java | 29 +++++++++++++-- .../raytracing/scene/util/Hittables.java | 35 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/jonahbauer/raytracing/scene/util/Hittables.java diff --git a/src/main/java/eu/jonahbauer/raytracing/Main.java b/src/main/java/eu/jonahbauer/raytracing/Main.java index 19bea34..fc1eae0 100644 --- a/src/main/java/eu/jonahbauer/raytracing/Main.java +++ b/src/main/java/eu/jonahbauer/raytracing/Main.java @@ -18,12 +18,11 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; -import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) throws IOException { - var example = getLight(); + var example = getCornellBox(); var scene = example.scene(); var camera = example.camera(); @@ -125,6 +124,32 @@ public class Main { ); } + private static @NotNull Example getCornellBox() { + var red = new LambertianMaterial(new Color(.65, .05, .05)); + var white = new LambertianMaterial(new Color(.73, .73, .73)); + var green = new LambertianMaterial(new Color(.12, .45, .15)); + var light = new DiffuseLight(new Color(15.0, 15.0, 15.0)); + + return new Example( + new Scene( + new Parallelogram(new Vec3(555, 0, 0), new Vec3(0, 555, 0), new Vec3(0, 0, 555), green), + new Parallelogram(new Vec3(0, 0, 0), new Vec3(0, 555, 0), new Vec3(0, 0, 555), red), + new Parallelogram(new Vec3(343, 554, 332), new Vec3(-130, 0, 0), new Vec3(0, 0, -105), light), + new Parallelogram(new Vec3(0, 0, 0), new Vec3(555, 0 ,0), new Vec3(0, 0, 555), white), + new Parallelogram(new Vec3(555, 555, 555), new Vec3(-555, 0 ,0), new Vec3(0, 0, -555), white), + new Parallelogram(new Vec3(0, 0, 555), new Vec3(555, 0 ,0), new Vec3(0, 555, 0), white), + Hittables.box(new Vec3(130, 0, 65), new Vec3(295, 165, 230), white), + Hittables.box(new Vec3(265, 0, 295), new Vec3(430, 330, 460), white) + ), + SimpleCamera.builder() + .withImage(600, 600) + .withFieldOfView(Math.toRadians(40)) + .withPosition(new Vec3(278, 278, -800)) + .withTarget(new Vec3(278, 278, 0)) + .build() + ); + } + private static @NotNull Scene getSimpleScene() { return new Scene( getSkyBox(), diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/util/Hittables.java b/src/main/java/eu/jonahbauer/raytracing/scene/util/Hittables.java new file mode 100644 index 0000000..06c50c6 --- /dev/null +++ b/src/main/java/eu/jonahbauer/raytracing/scene/util/Hittables.java @@ -0,0 +1,35 @@ +package eu.jonahbauer.raytracing.scene.util; + +import eu.jonahbauer.raytracing.math.Vec3; +import eu.jonahbauer.raytracing.render.material.Material; +import eu.jonahbauer.raytracing.scene.Hittable; +import eu.jonahbauer.raytracing.scene.hittable2d.Parallelogram; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; + +public final class Hittables { + private Hittables() { + throw new UnsupportedOperationException(); + } + + public static @NotNull Hittable box(@NotNull Vec3 a, @NotNull Vec3 b, @NotNull Material material) { + var sides = new ArrayList(); + + var min = Vec3.min(a, b); + var max = Vec3.max(a, b); + + var dx = new Vec3(max.x() - min.x(), 0, 0); + var dy = new Vec3(0, max.y() - min.y(), 0); + var dz = new Vec3(0, 0, max.z() - min.z()); + + sides.add(new Parallelogram(new Vec3(min.x(), min.y(), max.z()), dx, dy, material)); // front + sides.add(new Parallelogram(new Vec3(max.x(), min.y(), max.z()), dz.neg(), dy, material)); // right + sides.add(new Parallelogram(new Vec3(max.x(), min.y(), min.z()), dx.neg(), dy, material)); // back + sides.add(new Parallelogram(new Vec3(min.x(), min.y(), min.z()), dz, dy, material)); // left + sides.add(new Parallelogram(new Vec3(min.x(), max.y(), max.z()), dx, dz.neg(), material)); // top + sides.add(new Parallelogram(new Vec3(min.x(), min.y(), min.z()), dx, dz, material)); // bottom + + return new HittableList(sides); + } +}