add light sources
This commit is contained in:
parent
36de714f46
commit
3be855cffd
@ -12,6 +12,7 @@ import eu.jonahbauer.raytracing.render.renderer.SimpleRenderer;
|
||||
import eu.jonahbauer.raytracing.scene.*;
|
||||
import eu.jonahbauer.raytracing.scene.hittable2d.Parallelogram;
|
||||
import eu.jonahbauer.raytracing.scene.hittable3d.Sphere;
|
||||
import eu.jonahbauer.raytracing.scene.util.Hittables;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -22,12 +23,12 @@ import java.util.Random;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
var example = getSquares();
|
||||
var example = getLight();
|
||||
var scene = example.scene();
|
||||
var camera = example.camera();
|
||||
|
||||
var renderer = SimpleRenderer.builder()
|
||||
.withSamplesPerPixel(500)
|
||||
.withSamplesPerPixel(1000)
|
||||
.withMaxDepth(50)
|
||||
.withIterative(true)
|
||||
.build();
|
||||
@ -107,6 +108,23 @@ public class Main {
|
||||
);
|
||||
}
|
||||
|
||||
private static @NotNull Example getLight() {
|
||||
return new Example(
|
||||
new Scene(
|
||||
new Sphere(new Vec3(0, -1000, 0), 1000, new LambertianMaterial(new Color(0.2, 0.2, 0.2))),
|
||||
new Sphere(new Vec3(0, 2, 0), 2, new LambertianMaterial(new Color(0.2, 0.2, 0.2))),
|
||||
new Parallelogram(new Vec3(3, 1, -2), new Vec3(2, 0, 0), new Vec3(0, 2, 0), new DiffuseLight(new Color(4.0, 4.0, 4.0))),
|
||||
new Sphere(new Vec3(0, 7, 0), 2, new DiffuseLight(new Color(4.0, 4.0, 4.0)))
|
||||
),
|
||||
SimpleCamera.builder()
|
||||
.withImage(400, 225)
|
||||
.withFieldOfView(Math.toRadians(20))
|
||||
.withPosition(new Vec3(26, 3, 6))
|
||||
.withTarget(new Vec3(0, 2, 0))
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
private static @NotNull Scene getSimpleScene() {
|
||||
return new Scene(
|
||||
getSkyBox(),
|
||||
|
@ -25,6 +25,10 @@ public record Color(double r, double g, double b) {
|
||||
return new Color(a.r() * b.r(), a.g() * b.g(), a.b() * b.b());
|
||||
}
|
||||
|
||||
public static @NotNull Color add(@NotNull Color a, @NotNull Color b) {
|
||||
return new Color(a.r() + b.r(), a.g() + b.g(), a.b() + b.b());
|
||||
}
|
||||
|
||||
public static @NotNull Color random(@NotNull Random random) {
|
||||
return new Color(random.nextDouble(), random.nextDouble(), random.nextDouble());
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package eu.jonahbauer.raytracing.render.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.Optional;
|
||||
|
||||
public record DiffuseLight(@NotNull Color emit) implements Material {
|
||||
@Override
|
||||
public @NotNull Optional<ScatterResult> scatter(@NotNull Ray ray, @NotNull HitResult hit) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Color emitted(@NotNull HitResult hit) {
|
||||
return emit;
|
||||
}
|
||||
}
|
@ -12,6 +12,10 @@ public interface Material {
|
||||
|
||||
@NotNull Optional<ScatterResult> scatter(@NotNull Ray ray, @NotNull HitResult hit);
|
||||
|
||||
default @NotNull Color emitted(@NotNull HitResult hit) {
|
||||
return Color.BLACK;
|
||||
}
|
||||
|
||||
record ScatterResult(@NotNull Ray ray, @NotNull Color attenuation) {
|
||||
public ScatterResult {
|
||||
Objects.requireNonNull(ray, "ray");
|
||||
|
@ -92,9 +92,11 @@ public final class SimpleRenderer implements Renderer {
|
||||
if (optional.isPresent()) {
|
||||
var hit = optional.get();
|
||||
var material = hit.material();
|
||||
var emitted = material.emitted(hit);
|
||||
return material.scatter(ray, hit)
|
||||
.map(scatter -> Color.multiply(scatter.attenuation(), getColor0(scene, scatter.ray(), depth - 1)))
|
||||
.orElse(Color.BLACK);
|
||||
.map(color -> Color.add(color, emitted))
|
||||
.orElse(emitted);
|
||||
} else {
|
||||
return scene.getBackgroundColor(ray);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user