<!DOCTYPE html><htmllang="en-US"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><title>(Task 4) Shadow Rays - </title><linkrel="shortcut icon"href="/favicon.ico"type="image/x-icon"><linkrel="stylesheet"href="/assets/css/just-the-docs-default.css"><script type="text/javascript"src="/assets/js/vendor/lunr.min.js"></script><script type="text/javascript"src="/assets/js/just-the-docs.js"></script><metaname="viewport"content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.7.1 --><title>(Task 4) Shadow Rays</title><metaname="generator"content="Jekyll v4.2.0"/><metaproperty="og:title"content="(Task 4) Shadow Rays"/><metaproperty="og:locale"content="en_US"/><metaname="twitter:card"content="summary"/><metaproperty="twitter:title"content="(Task 4) Shadow Rays"/><script type="application/ld+json">{"headline":"(Task 4) Shadow Rays","@type":"WebPage","url":"/pathtracer/shadow_rays","@context":"https://schema.org"}</script><!-- End Jekyll SEO tag --></head><body><svgxmlns="http://www.w3.org/2000/svg"style="display: none;"><symbolid="svg-link"viewBox="0 0 24 24"><title>Link</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-link"><pathd="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><pathd="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></symbol><symbolid="svg-search"viewBox="0 0 24 24"><title>Search</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-search"><circlecx="11"cy="11"r="8"></circle><linex1="21"y1="21"x2="16.65"y2="16.65"></line></svg></symbol><symbolid="svg-menu"viewBox="0 0 24 24"><title>Menu</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-menu"><linex1="3"y1="12"x2="21"y2="12"></line><linex1="3"y1="6"x2="21"y2="6"></line><linex1="3"y1="18"x2="21"y2="18"></line></svg></symbol><symbolid="svg-arrow-right"viewBox="0 0 24 24"><title>Expand</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-chevron-right"><polylinepoints="9 18 15 12 9 6"></polyline></svg></symbol><symbolid="svg-doc"viewBox="0 0 24 24"><title>Document</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-file"><pathd="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polylinepoints="13 2 13 9 20 9"></polyline></svg></symbol></svg><divclass="side-bar"><divclass="site-header"><ahref="/"class="site-title lh-tight"></a><ahref="#"id="menu-button"class="site-button"><svgviewBox="0 0 24 24"class="icon"><usexlink:href="#svg-menu"></use></svg></a></div><navrole="navigation"aria-label="Main"id="site-nav"class="site-nav"><ulclass="nav-list"><liclass="nav-list-item"><ahref="/"class="nav-list-link">Home</a></li><liclass="nav-list-item"><ahref="/git/"class="nav-list-link">GitHub Setup</a></li><liclass="nav-list-item"><ahref="/build/"class="nav-list-link">Building Scotty3D</a></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/guide/"class="nav-list-link">User Guide</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/guide/animate_mode/"class="nav-list-link">Animate</a></li><liclass="nav-list-item "><ahref="/guide/layout_mode/"class="nav-list-link">Layout</a></li><liclass="nav-list-item "><ahref="/guide/model_mode/"class="nav-list-link">Model</a></li><liclass="nav-list-item "><ahref="/guide/render_mode/"class="nav-list-link">Render</a></li><liclass="nav-list-item "><ahref="/guide/rigging_mode/"class="nav-list-link">Rig</a></li><liclass="nav-list-item "><ahref="/guide/simulate_mode/"class="nav-list-link">Simulate</a></li></ul></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/meshedit/"class="nav-list-link">A2: MeshEdit</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/meshedit/halfedge"class="nav-list-link">Halfedge Mesh</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/meshedit/local/"class="nav-list-link">Local Operations</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/meshedit/local/edge_flip"class="nav-list-link">Edge Flip Tutorial</a></li><liclass="nav-list-item "><ahref="/meshedit/local/bevel/"class="nav-list-link">Bevelling</a></li></ul></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/meshedit/global/"class="nav-list-link">Global Operations</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/meshedit/global/catmull/"class="nav-list-link">Catmull-Clark Subdivision</a></li><liclass="nav-list-item "><ahref="/meshedit/global/remesh/"class="nav-list-link">Isotropic Remeshing</a></li><liclass="nav-list-item "><ahref="/meshedit/global/linear/"class="nav-list-link">Linear Subdivision</a></li><liclass="nav-list-item "><ahref="/meshedit/global/loop/"class="nav-list-link">Loop Subdivision</a></li><liclass="nav-list-item "><ahref="/meshedit/global/simplify/"class="nav-list-link">Simplification</a></li><liclass="nav-list-item "><ahref="/meshedit/global/triangulate/"class="nav-list-link">Triangulation</a></li></ul></li></ul></li><liclass="nav-list-item active"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/pathtracer/"class="nav-list-link">A3: Pathtracer</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/pathtracer/camera_rays"class="nav-list-link">(Task 1) Camera Rays</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/pathtracer/intersecting_objects"class="nav-list-link">(Task 2) Intersections</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/pathtracer/ray_triangle_intersection"class="nav-list-link">Ray Triangle Intersection</a></li><liclass="nav-list-item "><ahref="/pathtracer/ray_sphere_intersection"class="nav-list-link">Ray Sphere Intersection</a></li></ul></li><liclass="nav-list-item "><ahref="/pathtracer/bounding_volume_hierarchy"class="nav-list-link">(Task 3) BVH</a></li><liclass="nav-list-item active"><ahref="/pathtracer/shadow_rays"class="nav-list-link active">(Task 4) Shadow Rays</a></li><liclass="nav-list-item "><ahref="/pathtracer/path_tracing"class="nav-list-link">(Task 5) Path Tracing</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/pathtracer/materials"class="nav-list-link">(Task 6) Materials</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/pathtracer/dielectrics_and_transmission"class="nav-list-link">Dielectrics and Transmission</a></li></ul></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/pathtracer/environment_lighting"class="nav-list-link">(Task 7) Environment Lighting</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/pathtracer/importance_sampling"class="nav-list-link">Environment Light Importance Sampling</a></li></ul></li><liclass="nav-list-item "><ahref="/pathtracer/visualization_of_normals"class="nav-list-link">Visualization of normals</a></li></ul></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/animation/"class="nav-list-link">A4: Animation</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/animation/splines"class="nav-list-link">Splines</a></li><liclass="nav-list-item "><ahref="/animation/skeleton_kinematics"class="nav-list-link">Skeleton Kinematics</a></li><liclass="nav-list-item "><ahref="/animation/skinning"class="nav-list-link">Skinning</a></li><liclass="nav-list-item "><ahref="/animation/particles"class="nav-list-link">Particles</a></li></ul></li></ul></nav><footerclass="site-footer"> This site uses <ahref="https://github.com/pmarsceill/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll. </footer></div><divclass="main"id="top"><divid="main-header"class="main-header"><divclass="search"><divclass="search-input-wrap"><inputtype="text"id="search-input"class="search-input"tabindex="0"placeholder="Search "aria-label="Search "autocomplete="off"><labelfor="search-input"class="search-label"><svgviewBox="0 0 24 24"class="search-icon"><usexlink:href="#svg-search"></use></svg></label></div><divid="search-results"class="search-results"></div></div></div><divid="main-content-wrap"class="main-content-wrap"><navaria-label="Breadcrumb"class="breadcrumb-nav"><olclass="breadcrumb-nav-list"><liclass="breadcrumb-nav-list-item"><ahref="/pathtracer/">A3: Pathtracer</a></li><liclass="breadcrumb-nav-list-item"><span>(Task 4) Shadow Rays</span></li></ol></nav><divid="main-content"class="main-content"role="main"><h1id="task-4-shadow-rays"><ahref="#task-4-shadow-rays"class="anchor-heading"aria-labelledby="task-4-shadow-rays"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> (Task 4) Shadow Rays </h1><p>In this task you will modify <codeclass="language-plaintext highlighter-rouge">Pathtracer::trace_ray</code> to implement accurate shadows.</p><p>Currently <codeclass="language-plaintext highlighter-rouge">Pathtracer::trace_ray</code> computes the following:</p><ul><li>It computes the intersection of ray <codeclass="language-plaintext highlighter-rouge">r</code> with the scene.</li><li>It computes the amount of light arriving at the hit point <codeclass="language-plaintext highlighter-rouge">hit.position</code> (the irradiance at the hit point) by integrating radiance from all scene light sources.</li><li>It computes the radiance reflected from the hit point in the direction of -<codeclass="language-plaintext highlighter-rouge">r</code>. (The amount of reflected light is based on the BSDF of the surface at the hit point.)</li></ul><p>Shadows occur when another scene object blocks light emitted from scene light sources towards the hit point. Fortunately, determining whether or not a ray of light from a light source to the hit point is occluded by another object is easy given a working ray tracer (which you have at this point!). <strong>You simply want to know whether a ray originating from the hit point (<codeclass="language-plaintext highlighter-rouge">hit.position</code>), and traveling towards the light source (direction to light) hits any scene geometry before reaching the light.</strong> (Note that you need to consider light’s distance from the hit point is given, more on this in the notes below.)</p><p>Your job is to implement the logic needed to compute whether hit point is in shadow with respect to the current light source sample. Below are a few notes:</p><ul><li><p>In the starter code, when we call <codeclass="language-plaintext highlighter-rouge">light.sample(hit.position)</code>, it returns us a <codeclass="language-plaintext highlighter-rouge">Light_sample sample</code> at the hit point . (You might want to take a look at <codeclass="language-plaintext highlighter-rouge">rays/light.h</code> for the definition of <codeclass="language-plaintext highlighter-rouge">struct Light_sample</code> and <codeclass="language-plaintext highlighter-rouge">class light</code>.) A <codeclass="language-plaintext highlighter-rouge">Light_sample</code> contains fields <codeclass="language-plaintext highlighter-rouge">radiance</code>, <codeclass="language-plaintext highlighter-rouge">pdf</code>, <codeclass="language-plaintext highlighter-rouge">direction</code>, and <codeclass="language-plaintext highlighter-rouge">distance</code>. In particular, <codeclass="language-plaintext highlighter-rouge">sample.direction</code> is the direction from the hit point to the light source, and <codeclass="language-plaintext highlighter-rouge">sample.distance</code> is the distance from the hit point to the light source.</p></li><li><p>A common ray tracing pitfall is for the “shadow ray” shot into the scene to accidentally hit the same objecr as <codeclass="language-plaintext highlighter-rouge">r</code> (the surface is erroneously determined to be occluded because the shadow ray is determined to hit the surface!). We recommend that you make sure the origin of the shadow ray is offset from the surface to avoid these erroneous “self-intersections”. For example, consider setting the origin of the shadow ray to be <codeclass="language-plaintext highlighter-rouge">hit.position + epsilon * sample.direction</code> instead of simply <codeclass="language-plaintext highlighter-rouge">hit.position</code>. <codeclass="language-plaintext highlighter-rouge">EPS_F</code> is defined in for this purpose(see <codeclass="language-plaintext highlighter-rouge">lib/mathlib.h</code>).</p></li><li>Another common pitfall is forgetting that it doesn’t matter if the shadow ray hits any scene geometry after reaching the light. Note that the light’s distance from the hit point is given by <codeclass="language-plaintext highlighter-rouge">sample.distance</code>. Also note that <codeclass="language-plaintext highlighter-rouge">Ray</code> has a member called <codeclass="language-plaintext highlighter-rouge">time_bound</code>…</li><li>You will find it useful to debug your shadow code using the <codeclass="language-plaintext highlighter-rouge">DirectionalLight</code> since it produces hard shadows that are easy to reason about.</li><li>You would want to comment out the line <codeclass="language-plaintext highlighter-rouge">Spectrum radiance_out = Spectrum(0.5f);</code> and initialize the <codeclass="language-plaintext highlighter-rouge">radiance_out</code> to a more reasonable value. Hint: is there supposed to have any amount of light before we even start considering each light sample?</li></ul><p>At this point you should be able to render very striking images.</p><h2id="sample-results"><ahref="#sample-results"class="anchor-heading"aria-labelledby="sample-results"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> Sample results: </h2><p>At this point, you can add all kinds of lights among the options you have when you create “New Light” in Layout mode, except for Sphere Light and Environment Map which you will implement in task 7 (Note that you can still fill in <codeclass="language-plaintext highlighter-rouge">Sphere::Uniform::sample</code> in <codeclass="language-plaintext highlighter-rouge">Samplers.cpp</code> now to view the result of a mesh under Sphere Light).</p><p>The head of Peter Schröder rendered with hemishphere lighting.</p><center><imgsrc="new_results/shadow_peter.png"style="height:240px"/></center><p>A sphere and a cube with hemishphere lighting</p><center><imgsrc="new_results/cube_sphere_hemisphere.png"style="height:240px"/></center><p>Hex and cube under directional lighting</p><center><imgsrc="new_results/ref1.png"style="height:240px"/></center><p>Bunny on a plane under point light</p><center><imgsrc="new_results/ref2.png"style="height:400px"/></center><p>Spot on a sphere under diretional lighting</p><center><imgsrc="new_results/ref3.png"style="height:240px"/></center><p>Spot on a sphere under hemisphere lighting</p><center><imgsrc="new_results/ref4.png"style="height:240px"/></center></div></div><divclass="search-overlay"></div></div></body></html>