refactor Shape

main
jbb01 6 months ago
parent 1113b91077
commit 672dc6af8b

@ -2,8 +2,8 @@ package eu.jonahbauer.raytracing;
import eu.jonahbauer.raytracing.render.Camera;
import eu.jonahbauer.raytracing.render.ImageIO;
import eu.jonahbauer.raytracing.shape.Scene;
import eu.jonahbauer.raytracing.shape.Sphere;
import eu.jonahbauer.raytracing.scene.Scene;
import eu.jonahbauer.raytracing.scene.Sphere;
import java.io.IOException;
import java.nio.file.Path;

@ -1,8 +1,9 @@
package eu.jonahbauer.raytracing.render;
import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.math.Vec3;
import eu.jonahbauer.raytracing.shape.Scene;
import eu.jonahbauer.raytracing.scene.Scene;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@ -52,7 +53,7 @@ public record Camera(
var y = pixel.y();
var ray = pixel.ray();
var result = scene.hit(ray);
var result = scene.hit(ray, Range.NON_NEGATIVE);
if (result.isPresent()) {
var normal = result.get().normal();
image.set(x, y, getNormalColor(normal));

@ -1,4 +1,4 @@
package eu.jonahbauer.raytracing.shape;
package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.Vec3;
import org.jetbrains.annotations.NotNull;

@ -1,4 +1,4 @@
package eu.jonahbauer.raytracing.shape;
package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray;
@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public sealed interface Shape permits Sphere {
public interface Hittable {
/**
* {@return the value <code>t</code> such that <code>ray.at(t)</code> is the intersection of this shaped closest to

@ -1,4 +1,4 @@
package eu.jonahbauer.raytracing.shape;
package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray;
@ -8,24 +8,23 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
public record Scene(@NotNull List<@NotNull Shape> shapes) {
public record Scene(@NotNull List<@NotNull Hittable> objects) implements Hittable {
public Scene {
shapes = List.copyOf(shapes);
objects = List.copyOf(objects);
}
public Scene(@NotNull Shape @NotNull ... shapes) {
this(List.of(shapes));
public Scene(@NotNull Hittable @NotNull ... objects) {
this(List.of(objects));
}
public @NotNull Optional<HitResult> hit(@NotNull Ray ray) {
var range = new Range(0, Double.POSITIVE_INFINITY);
public @NotNull Optional<HitResult> hit(@NotNull Ray ray, @NotNull Range range) {
var result = (HitResult) null;
for (var shape : shapes) {
var r = shape.hit(ray, range);
if (r.isPresent() && (result == null || r.get().t() < result.t())) {
for (var object : objects) {
var r = object.hit(ray, range);
if (r.isPresent() && range.surrounds(r.get().t())) {
result = r.get();
range = new Range(0, result.t());
range = new Range(range.min(), result.t());
}
}
return Optional.ofNullable(result);

@ -1,4 +1,4 @@
package eu.jonahbauer.raytracing.shape;
package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray;
@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.Optional;
public record Sphere(@NotNull Vec3 center, double radius) implements Shape {
public record Sphere(@NotNull Vec3 center, double radius) implements Hittable {
public static final @NotNull Sphere UNIT = new Sphere(Vec3.ZERO, 1.0);
public Sphere {

@ -1,4 +1,4 @@
package eu.jonahbauer.raytracing.shape;
package eu.jonahbauer.raytracing.scene;
import eu.jonahbauer.raytracing.math.Range;
import eu.jonahbauer.raytracing.math.Ray;
Loading…
Cancel
Save