move render method to Camera
parent
7757b3d573
commit
1113b91077
@ -1,72 +0,0 @@
|
||||
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.HitResult;
|
||||
import eu.jonahbauer.raytracing.shape.Shape;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record Scene(@NotNull List<@NotNull Shape> shapes) {
|
||||
|
||||
public Scene {
|
||||
shapes = List.copyOf(shapes);
|
||||
}
|
||||
|
||||
public Scene(@NotNull Shape @NotNull ... shapes) {
|
||||
this(List.of(shapes));
|
||||
}
|
||||
|
||||
public @NotNull Image render(@NotNull Camera camera) {
|
||||
var image = new Image(camera.width(), camera.height());
|
||||
|
||||
camera.pixels().forEach(pixel -> {
|
||||
var x = pixel.x();
|
||||
var y = pixel.y();
|
||||
var ray = pixel.ray();
|
||||
|
||||
var result = hit(ray);
|
||||
if (result.isPresent()) {
|
||||
var normal = result.get().normal();
|
||||
image.set(x, y, getNormalColor(normal));
|
||||
} else {
|
||||
image.set(x, y, getSkyboxColor(ray));
|
||||
}
|
||||
});
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private @NotNull Optional<HitResult> hit(@NotNull Ray ray) {
|
||||
var range = new Range(0, Double.POSITIVE_INFINITY);
|
||||
var result = (HitResult) null;
|
||||
for (var shape : shapes) {
|
||||
var r = shape.hit(ray, range);
|
||||
if (r.isPresent() && (result == null || r.get().t() < result.t())) {
|
||||
result = r.get();
|
||||
range = new Range(0, result.t());
|
||||
}
|
||||
}
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
private @NotNull Color getNormalColor(@NotNull Vec3 normal) {
|
||||
return new Color(
|
||||
0.5 * (normal.x() + 1),
|
||||
0.5 * (normal.y() + 1),
|
||||
0.5 * (normal.z() + 1)
|
||||
);
|
||||
}
|
||||
|
||||
private @NotNull Color getSkyboxColor(@NotNull Ray ray) {
|
||||
// altitude from -pi/2 to pi/2
|
||||
var alt = Math.copySign(
|
||||
Math.acos(ray.direction().withY(0).unit().times(ray.direction().unit())),
|
||||
ray.direction().y()
|
||||
);
|
||||
return Color.lerp(Color.WHITE, Color.SKY, alt / Math.PI + 0.5);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package eu.jonahbauer.raytracing.shape;
|
||||
|
||||
import eu.jonahbauer.raytracing.math.Range;
|
||||
import eu.jonahbauer.raytracing.math.Ray;
|
||||
import eu.jonahbauer.raytracing.render.Color;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record Scene(@NotNull List<@NotNull Shape> shapes) {
|
||||
|
||||
public Scene {
|
||||
shapes = List.copyOf(shapes);
|
||||
}
|
||||
|
||||
public Scene(@NotNull Shape @NotNull ... shapes) {
|
||||
this(List.of(shapes));
|
||||
}
|
||||
|
||||
public @NotNull Optional<HitResult> hit(@NotNull Ray ray) {
|
||||
var range = new Range(0, Double.POSITIVE_INFINITY);
|
||||
var result = (HitResult) null;
|
||||
for (var shape : shapes) {
|
||||
var r = shape.hit(ray, range);
|
||||
if (r.isPresent() && (result == null || r.get().t() < result.t())) {
|
||||
result = r.get();
|
||||
range = new Range(0, result.t());
|
||||
}
|
||||
}
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
public @NotNull Color getSkyboxColor(@NotNull Ray ray) {
|
||||
// altitude from -pi/2 to pi/2
|
||||
var alt = Math.copySign(
|
||||
Math.acos(ray.direction().withY(0).unit().times(ray.direction().unit())),
|
||||
ray.direction().y()
|
||||
);
|
||||
return Color.lerp(Color.WHITE, Color.SKY, alt / Math.PI + 0.5);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue