|
|
@ -2,6 +2,8 @@ package eu.jonahbauer.raytracing.math;
|
|
|
|
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
|
|
public record Vec3(double x, double y, double z) {
|
|
|
|
public record Vec3(double x, double y, double z) {
|
|
|
|
public static final Vec3 ZERO = new Vec3(0, 0, 0);
|
|
|
|
public static final Vec3 ZERO = new Vec3(0, 0, 0);
|
|
|
|
public static final Vec3 UNIT_X = new Vec3(1, 0, 0);
|
|
|
|
public static final Vec3 UNIT_X = new Vec3(1, 0, 0);
|
|
|
@ -31,12 +33,15 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
return vec.minus(normal.times(2 * normal.times(vec)));
|
|
|
|
return vec.minus(normal.times(2 * normal.times(vec)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double index) {
|
|
|
|
public static @NotNull Optional<Vec3> refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) {
|
|
|
|
vec = vec.unit();
|
|
|
|
vec = vec.unit();
|
|
|
|
var cosTheta = Math.min(- vec.times(normal), 1.0);
|
|
|
|
var cosTheta = Math.min(- vec.times(normal), 1.0);
|
|
|
|
var rOutPerp = vec.plus(normal.times(cosTheta)).times(index);
|
|
|
|
var sinTheta = Math.sqrt(1 - cosTheta * cosTheta);
|
|
|
|
|
|
|
|
if (ri * sinTheta > 1) return Optional.empty();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var rOutPerp = vec.plus(normal.times(cosTheta)).times(ri);
|
|
|
|
var rOutParallel = normal.times(- Math.sqrt(Math.abs(1 - rOutPerp.squared())));
|
|
|
|
var rOutParallel = normal.times(- Math.sqrt(Math.abs(1 - rOutPerp.squared())));
|
|
|
|
return rOutPerp.plus(rOutParallel);
|
|
|
|
return Optional.of(rOutPerp.plus(rOutParallel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 plus(@NotNull Vec3 b) {
|
|
|
|
public @NotNull Vec3 plus(@NotNull Vec3 b) {
|
|
|
|