From b3f073e2be607d62241ad196270a936d3baf7ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Kapri=C5=A1?= Date: Tue, 20 Dec 2022 13:49:14 +0100 Subject: [PATCH] Add shadows --- .../gfxlab/graphics3d/raytracers/RayTracerSimple.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/xyz/marsavic/gfxlab/graphics3d/raytracers/RayTracerSimple.java b/src/xyz/marsavic/gfxlab/graphics3d/raytracers/RayTracerSimple.java index 9e65de6..c279d2c 100644 --- a/src/xyz/marsavic/gfxlab/graphics3d/raytracers/RayTracerSimple.java +++ b/src/xyz/marsavic/gfxlab/graphics3d/raytracers/RayTracerSimple.java @@ -24,7 +24,12 @@ public class RayTracerSimple extends RayTracer { Color lightDiffuse = Color.BLACK; // The sum of diffuse contributions from all the lights for (Light light : scene.lights()) { - Vec3 l = light.p().sub(p); // Vector from p to the light; + Vec3 l = light.p().sub(p); // Vector from p to the light; + Ray lRay = new Ray(light.p(), l.inverse()); // Project a Ray from light source to object + Hit lHit = scene.solid().firstHit(lRay); + double epsilon = 0.000001; + if (lRay.at(Hit.t(lHit)).sub(p).lengthSquared() >= epsilon) + continue; // if it strikes an object before our point, a shadow is cast, no light is added double lLSqr = l.lengthSquared(); // Distance from p to the light squared double lL = Math.sqrt(lLSqr); // Distance from p to the light double cosLN = n_.dot(l) / lL; // Cosine of the angle between l and n_