<!DOCTYPE html><htmllang="en-US"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><title>(Task 2) Intersections - </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 2) Intersections</title><metaname="generator"content="Jekyll v4.2.0"/><metaproperty="og:title"content="(Task 2) Intersections"/><metaproperty="og:locale"content="en_US"/><metaname="twitter:card"content="summary"/><metaproperty="twitter:title"content="(Task 2) Intersections"/><script type="application/ld+json">{"headline":"(Task 2) Intersections","@type":"WebPage","url":"/pathtracer/intersecting_objects","@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 active"><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 active">(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 "><ahref="/pathtracer/shadow_rays"class="nav-list-link">(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 2) Intersections</span></li></ol></nav><divid="main-content"class="main-content"role="main"><h1id="task-2-intersecting-objects"><ahref="#task-2-intersecting-objects"class="anchor-heading"aria-labelledby="task-2-intersecting-objects"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> (Task 2) Intersecting Objects </h1><p>Now that your ray tracer generates camera rays, we need to be able to answer the core query in ray tracing: “does this ray hit this object?” Here, you will start by implementing ray-object intersection routines against the two types of objects in the starter code: triangles and spheres. Later, we will use a BVH to accelerate these queries, but for now we consider an intersection test against a single object.</p><p>First, take a look at the <codeclass="language-plaintext highlighter-rouge">rays/object.h</code> for the interface of <codeclass="language-plaintext highlighter-rouge">Object</code> class. An <codeclass="language-plaintext highlighter-rouge">Object</code> can be <strong>either</strong> a <codeclass="language-plaintext highlighter-rouge">Tri_Mesh</code>, a <codeclass="language-plaintext highlighter-rouge">Shape</code>, a BVH(which you will implement in Task 3), or a list of <codeclass="language-plaintext highlighter-rouge">Objects</code>. Right now, we are only dealing with <codeclass="language-plaintext highlighter-rouge">Tri_Mesh</code>’s case and <codeclass="language-plaintext highlighter-rouge">Shape</code>’s case, and their interfaces are in <codeclass="language-plaintext highlighter-rouge">rays/tri_mesh.h</code> and <codeclass="language-plaintext highlighter-rouge">rays/shapes.h</code>, respectively. <codeclass="language-plaintext highlighter-rouge">Tri_Mesh</code> contains a BVH of <codeclass="language-plaintext highlighter-rouge">Triangle</code>, and in this task you will be working with the <codeclass="language-plaintext highlighter-rouge">Triangle</code> class. For <codeclass="language-plaintext highlighter-rouge">Shape</code>, you are going to work with <codeclass="language-plaintext highlighter-rouge">Sphere</code>s, which is the major type of <codeclass="language-plaintext highlighter-rouge">Shape</code> in Scotty 3D.</p><p>Now, you need to implement the <codeclass="language-plaintext highlighter-rouge">hit</code> routine for both <codeclass="language-plaintext highlighter-rouge">Triangle</code> and <codeclass="language-plaintext highlighter-rouge">Sphere</code>. <codeclass="language-plaintext highlighter-rouge">hit</code> takes in a ray, and returns a <codeclass="language-plaintext highlighter-rouge">Trace</code> structure, which contains information on whether the ray hits the object and if hits, the information describing the surface at the point of the hit. See <codeclass="language-plaintext highlighter-rouge">rays/trace.h</code> for the definition of <codeclass="language-plaintext highlighter-rouge">Trace</code>.</p><p>In order to correctly implement <codeclass="language-plaintext highlighter-rouge">hit</code> you need to understand some of the fields in the Ray structure defined in <codeclass="language-plaintext highlighter-rouge">lib/ray.h</code>.</p><ul><li><codeclass="language-plaintext highlighter-rouge">point</code>: represents the 3D point of origin of the ray</li><li><codeclass="language-plaintext highlighter-rouge">dir</code>: represents the 3D direction of the ray (this direction will be normalized)</li><li><codeclass="language-plaintext highlighter-rouge">time_bounds</code>: correspond to the minimum and maximum points on the ray with its x-component as the lower bound and y-component as the upper bound. That is, intersections that lie outside the [<codeclass="language-plaintext highlighter-rouge">ray.time_bounds.x</code>, <codeclass="language-plaintext highlighter-rouge">ray.time_bounds.y</code>] range should not be considered valid intersections with the primitive.</li></ul><p>One important detail of the Ray structure is that <codeclass="language-plaintext highlighter-rouge">time_bounds</code> is a mutable field of the Ray. This means that this fields can be modified by constant member functions such as <codeclass="language-plaintext highlighter-rouge">Triangle::hit</code>. When finding the first intersection of a ray and the scene, you almost certainly want to update the ray’s <codeclass="language-plaintext highlighter-rouge">time_bounds</code> value after finding each hit with scene geometry. By bounding the ray as tightly as possible, your ray tracer will be able to avoid unnecessary tests with scene geometry that is known to not be able to result in a closest hit, resulting in higher performance.</p><hr/><h3id="step-1-intersecting-triangles"><ahref="#step-1-intersecting-triangles"class="anchor-heading"aria-labelledby="step-1-intersecting-triangles"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a><strong>Step 1: Intersecting Triangles</strong></h3><p>The first intersect routine that the <codeclass="language-plaintext highlighter-rouge">hit</code> routines for the triangle mesh in <codeclass="language-plaintext highlighter-rouge">student/tri_mesh.cpp</code>.</p><p>While faster implementations are possible, we recommend you implement ray-triangle intersection using the method described in the <ahref="http://15462.courses.cs.cmu.edu/fall2017/lecture/acceleratingqueries">lecture slides</a>. Further details of implementing this method efficiently are given in <ahref="ray_triangle_intersection.md">these notes</a>.</p><p>There are two important details you should be aware of about intersection:</p><ul><li><p>When finding the first-hit intersection with a triangle, you need to fill in the <codeclass="language-plaintext highlighter-rouge">Trace</code> structure with details of the hit. The structure should be initialized with:</p><ul><li><codeclass="language-plaintext highlighter-rouge">hit</code>: a boolean representing if there is a hit or not</li><li><codeclass="language-plaintext highlighter-rouge">time</code>: the ray’s <em>t</em>-value of the hit point</li><li><codeclass="language-plaintext highlighter-rouge">position</code>: the exact position of the hit point. This can be easily computed by the <codeclass="language-plaintext highlighter-rouge">time</code> above as with the ray’s <codeclass="language-plaintext highlighter-rouge">point</code> and <codeclass="language-plaintext highlighter-rouge">dir</code>.</li><li><codeclass="language-plaintext highlighter-rouge">normal</code>: the normal of the surface at the hit point. This normal should be the interpolated normal (obtained via interpolation of the per-vertex normals according to the barycentric coordinates of the hit point)</li></ul></li></ul><!-- * When intersection occurs with the back-face of a triangle (the side of the triangle opposite the direction of the normal) you should flip the returned normal to point in that direction. That is, always return a normal pointing in the direction the ray came from! --><p>Once you’ve successfully implemented triangle intersection, you will be able to render many of the scenes in the media directory. However, your ray tracer will be very slow!</p><p>While you are working with <codeclass="language-plaintext highlighter-rouge">student/tri_mesh.cpp</code>, you should implement <codeclass="language-plaintext highlighter-rouge">Triangle::bbox</code> as well, which are important for task 3.</p><h3id="step-2-intersecting-spheres"><ahref="#step-2-intersecting-spheres"class="anchor-heading"aria-labelledby="step-2-intersecting-spheres"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a><strong>Step 2: Intersecting Spheres</strong></h3><p>You also need to implement the <codeclass="language-plaintext highlighter-rouge">hit</code> routines for the <codeclass="language-plaintext highlighter-rouge">Sphere</code> class in <codeclass="language-plaintext highlighter-rouge">student/sphapes.cpp</code>. Remember that your intersection tests should respect the ray’s <codeclass="language-plaintext highlighter-rouge">time_bound</code>. Because spheres always represent closed surfaces, you should not flip back-facing normals you did with triangles.</p><p>Note: take care <strong>not</strong> to use the <codeclass="language-plaintext highlighter-rouge">Vec3::normalize()</code> method when computing your normal vector. You should instead use <codeclass="language-plaintext highlighter-rouge">Vec3::unit()</code>, since <codeclass="language-plaintext highlighter-rouge">Vec3::normalize()</code> will actually change the <codeclass="language-plaintext highlighter-rouge">Vec3</code> object passed in rather than returning a normalized version.</p><hr/><p><ahref="visualization_of_normals.md">Visualization of normals</a> might be very helpful with debugging.</p></div></div><divclass="search-overlay"></div></div></body></html>