diff --git a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java index 8098af8..775a972 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/Camera.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/Camera.java @@ -21,6 +21,9 @@ public final class Camera { private final @NotNull Vec3 origin; private final @NotNull Vec3 direction; + // antialiasing + private final int samplesPerPixel = 100; + // internal properties private final @NotNull Vec3 pixelU; private final @NotNull Vec3 pixelV; @@ -86,10 +89,20 @@ public final class Camera { var image = new Image(width, height); for (int y = 0; y < height; y++) { - var ray = getRay(x, y); - var color = getColor(scene, ray); - image.set(x, y, color); for (int x = 0; x < width; x++) { + var r = 0d; + var g = 0d; + var b = 0d; + + for (int i = 0; i < samplesPerPixel; i++) { + var ray = getRay(x, y); + var color = getColor(scene, ray); + r += color.r(); + g += color.g(); + b += color.b(); + } + + image.set(x, y, new Color(r / samplesPerPixel, g / samplesPerPixel, b / samplesPerPixel)); } } @@ -103,7 +116,10 @@ public final class Camera { private @NotNull Vec3 getPixel(int x, int y) { Objects.checkIndex(x, width); Objects.checkIndex(y, height); - return pixel00.plus(pixelU.times(x)).plus(pixelV.times(y)); + + double dx = x + Math.random() - 0.5; + double dy = y + Math.random() - 0.5; + return pixel00.plus(pixelU.times(dx)).plus(pixelV.times(dy)); } private @NotNull Color getColor(@NotNull Scene scene, @NotNull Ray ray) {