From 94231f6a5b515b6c586a49491fb50c638f506b50 Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:36:52 +0200 Subject: [PATCH] play with camera settings and fix handedness of camera --- src/main/java/eu/jonahbauer/raytracing/Main.java | 16 +++++++++++----- .../java/eu/jonahbauer/raytracing/math/Vec3.java | 10 ++++++++++ .../eu/jonahbauer/raytracing/render/Camera.java | 6 +++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/jonahbauer/raytracing/Main.java b/src/main/java/eu/jonahbauer/raytracing/Main.java index 0cbaae6..3056b10 100644 --- a/src/main/java/eu/jonahbauer/raytracing/Main.java +++ b/src/main/java/eu/jonahbauer/raytracing/Main.java @@ -3,6 +3,7 @@ package eu.jonahbauer.raytracing; import eu.jonahbauer.raytracing.material.DielectricMaterial; import eu.jonahbauer.raytracing.material.LambertianMaterial; import eu.jonahbauer.raytracing.material.MetallicMaterial; +import eu.jonahbauer.raytracing.math.Vec3; import eu.jonahbauer.raytracing.render.Camera; import eu.jonahbauer.raytracing.render.Color; import eu.jonahbauer.raytracing.render.ImageFormat; @@ -15,12 +16,17 @@ import java.nio.file.Path; public class Main { public static void main(String[] args) throws IOException { var scene = new Scene( - new Sphere(0, -100.5, 1, 100, new LambertianMaterial(new Color(0.8, 0.8, 0.0))), - new Sphere(0, 0, 1.2, 0.5, new LambertianMaterial(new Color(0.1, 0.2, 0.5))), - new Sphere(-1, 0, 1, 0.5, new DielectricMaterial(0.75)), - new Sphere(1, 0, 1, 0.5, new MetallicMaterial(new Color(0.8, 0.6, 0.2), 1.0)) + new Sphere(0, -100.5, - 1, 100, new LambertianMaterial(new Color(0.8, 0.8, 0.0))), + new Sphere(0, 0, - 1.2, 0.5, new LambertianMaterial(new Color(0.1, 0.2, 0.5))), + new Sphere(-1, 0, - 1, 0.5, new DielectricMaterial(1.5)), + new Sphere(-1, 0, - 1, 0.4, new DielectricMaterial(1 / 1.5)), + new Sphere(1, 0, - 1, 0.5, new MetallicMaterial(new Color(0.8, 0.6, 0.2), 1.0)) + ); + var camera = new Camera( + 800, 450, + 16d / 9 * 2, 2d, + Vec3.ZERO.plus(Vec3.UNIT_Z).plus(Vec3.UNIT_Y).minus(Vec3.UNIT_X), new Vec3(0.25, -0.5, - 1) ); - var camera = new Camera(512, 2, 16 / 9d); var image = camera.render(scene); ImageFormat.PNG.write(image, Path.of("scene-" + System.currentTimeMillis() + ".png")); diff --git a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java index 8dafe48..4646246 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java @@ -44,6 +44,12 @@ public record Vec3(double x, double y, double z) { return Optional.of(rOutPerp.plus(rOutParallel)); } + public static @NotNull Vec3 rotate(@NotNull Vec3 vec, @NotNull Vec3 axis, double angle) { + Vec3 vxp = axis.cross(vec); + Vec3 vxvxp = axis.cross(vxp); + return vec.plus(vxp.times(Math.sin(angle))).plus(vxvxp.times(1 - Math.cos(angle))); + } + public @NotNull Vec3 plus(@NotNull Vec3 b) { return new Vec3(this.x + b.x, this.y + b.y, this.z + b.z); } @@ -60,6 +66,10 @@ public record Vec3(double x, double y, double z) { return new Vec3(this.x * b, this.y * b, this.z * b); } + public @NotNull Vec3 neg() { + return new Vec3(-x, -y, -z); + } + public @NotNull Vec3 cross(@NotNull Vec3 b) { return new Vec3( this.y() * b.z() - b.y() * this.z(), diff --git a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java index 93f6d7f..58e9e40 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java @@ -49,7 +49,7 @@ public final class Camera { * @param viewportHeight the viewport height */ public Camera(int width, int height, double viewportWidth, double viewportHeight) { - this(width, height, viewportWidth, viewportHeight, Vec3.ZERO, Vec3.UNIT_Z); + this(width, height, viewportWidth, viewportHeight, Vec3.ZERO, Vec3.UNIT_Z.neg()); } public Camera( @@ -73,11 +73,11 @@ public final class Camera { var d = direction.unit(); var dXZ = direction.withY(0).unit(); - var viewportU = new Vec3(dXZ.z(), 0, - dXZ.x()); // perpendicular to dXZ in xz-plane + var viewportU = new Vec3(- dXZ.z(), 0, dXZ.x()); // perpendicular to dXZ in xz-plane var viewportV = d.cross(viewportU); // perpendicular to viewportU and direction viewportU = viewportU.times(viewportWidth); // vector along the width of the viewport - viewportV = viewportV.times(- viewportHeight); // vector along the height of the viewport + viewportV = viewportV.times(viewportHeight); // vector along the height of the viewport this.pixelU = viewportU.div(width); this.pixelV = viewportV.div(height);