aboutsummaryrefslogtreecommitdiff
path: root/src/illuminate/shadow.cpp
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-12-07 16:23:20 -0500
committersotech117 <michael_foiani@brown.edu>2023-12-07 16:23:20 -0500
commitcaa765bff49d54217b75aaf0e7acf4e5392a11e4 (patch)
tree9b92914dfb88b99599e8e60e4512e9e9ea9a25db /src/illuminate/shadow.cpp
parenta9274459443f1d560d7580a162deb581549980cb (diff)
upload base code
Diffstat (limited to 'src/illuminate/shadow.cpp')
-rw-r--r--src/illuminate/shadow.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/illuminate/shadow.cpp b/src/illuminate/shadow.cpp
new file mode 100644
index 0000000..99e2b29
--- /dev/null
+++ b/src/illuminate/shadow.cpp
@@ -0,0 +1,58 @@
+#include "raytracer/raytracer.h"
+
+bool RayTracer::isShadowed(
+ glm::vec4 lightPosition,
+ float distanceToLight,
+ glm::vec4 directionFromIntersectionToLight,
+ glm::vec4 intersectionWorld,
+ const RayTraceScene &scene)
+{
+ // normalize direction
+ directionFromIntersectionToLight = glm::normalize(directionFromIntersectionToLight);
+
+ // acceleration causes "bad jaggies" so we disable it for now
+ if (m_config.enableAcceleration)
+ {
+ RenderShapeData shapeData;
+ auto pBias = intersectionWorld + .001f * directionFromIntersectionToLight;
+ float t = traverseBVH(pBias, directionFromIntersectionToLight, shapeData, scene.m_bvh);
+ return t != FINF;
+ }
+
+ for (const RenderShapeData &s: scene.getShapes()) {
+ // convert this world ray to object space
+ glm::vec4 dObject = glm::normalize(
+ s.inverseCTM * directionFromIntersectionToLight);
+ glm::vec4 pObject = s.inverseCTM * intersectionWorld;
+
+ // see if there is an intersection
+ glm::vec4 newIntersectionObj = findIntersection(pObject, dObject, s);
+
+ if (newIntersectionObj.w == 1.f) // hit!
+ {
+ // check if the intersection is the same as the pObject
+ if (floatEquals(glm::distance(newIntersectionObj, pObject), 0.f, 0.001f))
+ {
+ // don't consider self-intersections
+ continue;
+ }
+
+ // check if this intersection is closer than the direction to the light
+ auto newIntersectionWorld = s.ctm * newIntersectionObj;
+ if (distanceToLight == FINF)
+ {
+ // if the light is infinitely far away light, then any non-self intersection is valid
+ return true;
+ }
+
+ float newDist = glm::distance(newIntersectionWorld, lightPosition);
+ if (newDist < distanceToLight - 0.001f)
+ {
+ // an object in front of the camera is the way -> shadow
+ return true;
+ }
+ }
+ }
+
+ return false;
+} \ No newline at end of file