From a41b14da48804c943eab4bc309b6eb477613ecfc Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:28:43 +0200 Subject: [PATCH] add metal --- .../java/eu/jonahbauer/raytracing/Main.java | 7 ++++-- .../raytracing/material/MetallicMaterial.java | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/jonahbauer/raytracing/material/MetallicMaterial.java diff --git a/src/main/java/eu/jonahbauer/raytracing/Main.java b/src/main/java/eu/jonahbauer/raytracing/Main.java index 725228a..32d72ba 100644 --- a/src/main/java/eu/jonahbauer/raytracing/Main.java +++ b/src/main/java/eu/jonahbauer/raytracing/Main.java @@ -1,6 +1,7 @@ package eu.jonahbauer.raytracing; import eu.jonahbauer.raytracing.material.LambertianMaterial; +import eu.jonahbauer.raytracing.material.MetallicMaterial; import eu.jonahbauer.raytracing.render.Camera; import eu.jonahbauer.raytracing.render.Color; import eu.jonahbauer.raytracing.render.ImageFormat; @@ -13,8 +14,10 @@ import java.nio.file.Path; public class Main { public static void main(String[] args) throws IOException { var scene = new Scene( - new Sphere(0, 0, 1, 0.5, new LambertianMaterial(Color.RED)), - new Sphere(0, -100.5, 1, 100, new LambertianMaterial(Color.BLUE)) + 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 MetallicMaterial(new Color(0.8, 0.8, 0.8))), + new Sphere(1, 0, 1, 0.5, new MetallicMaterial(new Color(0.8, 0.6, 0.2))) ); var camera = new Camera(512, 2, 16 / 9d); diff --git a/src/main/java/eu/jonahbauer/raytracing/material/MetallicMaterial.java b/src/main/java/eu/jonahbauer/raytracing/material/MetallicMaterial.java new file mode 100644 index 0000000..f2fd97c --- /dev/null +++ b/src/main/java/eu/jonahbauer/raytracing/material/MetallicMaterial.java @@ -0,0 +1,23 @@ +package eu.jonahbauer.raytracing.material; + +import eu.jonahbauer.raytracing.math.Ray; +import eu.jonahbauer.raytracing.render.Color; +import eu.jonahbauer.raytracing.scene.HitResult; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.Optional; + +public record MetallicMaterial(@NotNull Color albedo) implements Material { + public MetallicMaterial { + Objects.requireNonNull(albedo, "albedo"); + } + + @Override + public @NotNull Optional scatter(@NotNull Ray ray, @NotNull HitResult hit) { + var direction = ray.direction(); + var normal = hit.normal(); + var newDirection = direction.minus(normal.times(2 * normal.times(direction))); + return Optional.of(new ScatterResult(new Ray(hit.position(), newDirection), albedo)); + } +}