From 77c1a87e4ff5060b394e36d43b5df90e4db05b91 Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Thu, 8 Aug 2024 19:31:57 +0200 Subject: [PATCH] fix a possible cause of NaNs --- .../raytracing/render/renderer/SimpleRenderer.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/jonahbauer/raytracing/render/renderer/SimpleRenderer.java b/src/main/java/eu/jonahbauer/raytracing/render/renderer/SimpleRenderer.java index e3c6a1e..15a8ecc 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/renderer/SimpleRenderer.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/renderer/SimpleRenderer.java @@ -180,7 +180,13 @@ public final class SimpleRenderer implements Renderer { } else { var mixed = new MixtureProbabilityDensityFunction(new TargetingProbabilityDensityFunction(hit.position(), scene.getLights()), pdf, 0.5); var direction = mixed.generate(random); - var factor = pdf.value(direction) / mixed.value(direction); + + var idealPdf = pdf.value(direction); + var actualPdf = mixed.value(direction); + if (actualPdf == 0) break; // when actualPdf is 0, the ray should have never been generated by mixed.generate + + var factor = idealPdf / actualPdf; + attenuation = Color.multiply(attenuation, Color.multiply(a, factor)); ray = new Ray(hit.position(), direction);