|
|
|
@ -10,15 +10,16 @@ import eu.jonahbauer.raytracing.scene.Hittable;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.random.RandomGenerator;
|
|
|
|
|
|
|
|
|
|
public record ConstantMedium(@NotNull Hittable boundary, double density, @NotNull IsotropicMaterial material) implements Hittable {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public @NotNull Optional<HitResult> hit(@NotNull Ray ray, @NotNull Range range) {
|
|
|
|
|
var hit1 = boundary.hit(ray, Range.UNIVERSE);
|
|
|
|
|
public @NotNull Optional<HitResult> hit(@NotNull Ray ray, @NotNull Range range, @NotNull RandomGenerator random) {
|
|
|
|
|
var hit1 = boundary.hit(ray, Range.UNIVERSE, random);
|
|
|
|
|
if (hit1.isEmpty()) return Optional.empty();
|
|
|
|
|
|
|
|
|
|
var hit2 = boundary.hit(ray, new Range(hit1.get().t() + 0.0001, Double.POSITIVE_INFINITY));
|
|
|
|
|
var hit2 = boundary.hit(ray, new Range(hit1.get().t() + 0.0001, Double.POSITIVE_INFINITY), random);
|
|
|
|
|
if (hit2.isEmpty()) return Optional.empty();
|
|
|
|
|
|
|
|
|
|
var tmin = Math.max(range.min(), hit1.get().t());
|
|
|
|
@ -28,7 +29,7 @@ public record ConstantMedium(@NotNull Hittable boundary, double density, @NotNul
|
|
|
|
|
|
|
|
|
|
var length = ray.direction().length();
|
|
|
|
|
var distance = length * (tmax - tmin);
|
|
|
|
|
var hitDistance = - Math.log(Math.random()) / density;
|
|
|
|
|
var hitDistance = - Math.log(random.nextDouble()) / density;
|
|
|
|
|
if (hitDistance > distance) return Optional.empty();
|
|
|
|
|
|
|
|
|
|
var t = tmin + hitDistance / length;
|
|
|
|
|