{"id":11846,"date":"2021-01-27T09:19:51","date_gmt":"2021-01-27T09:19:51","guid":{"rendered":"https:\/\/www.thecoderschool.com\/blog\/?p=11846"},"modified":"2022-10-13T20:49:52","modified_gmt":"2022-10-13T20:49:52","slug":"concepts-of-ray-tracing","status":"publish","type":"post","link":"https:\/\/www.thecoderschool.com\/blog\/concepts-of-ray-tracing\/","title":{"rendered":"Concepts of Ray Tracing"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-11843 aligncenter\" src=\"https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/01\/Ray-Tracing-300x195.png\" alt=\"Ray Tracing\" width=\"520\" height=\"338\" srcset=\"https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/01\/Ray-Tracing-300x195.png 300w, https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/01\/Ray-Tracing.png 606w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/figure><p class=\"p1\" style=\"text-align: center;\">&nbsp; &nbsp; <\/p>\n&nbsp;\n<h2><span style=\"font-weight: 400;\">Sun and Lights<\/span><\/h2>\n<\/div>\n<\/div><\/div>\n\n\n\n<p><strong>By Daniel K., Age 14<\/strong><\/p>\n\n\n\n<div class=\"\">\n<br><p><span style=\"font-weight: 400;\">In real life, the sun and lights emit rays of light that travel in (kind of) a straight line. When these rays of light hit an object, some of them are absorbed, some of them pass through (if the object is clear), and some of them are reflected off. The rays of light continue bouncing around until they reach your eye (or a camera).<\/span><\/p>\n<br><p><span style=\"font-weight: 400;\">Ray tracing is supposed to simulate these rays of light and their complex interactions and is used to render realistic CGI scenes, usually in movies and more recently in video games. However, raytracing is incredibly hard to do. You have to simulate trillions of rays of light bouncing around the environment. In movie studios, raytracing one frame of CGI can take up to a day.<\/span><\/p>\n<br><p><span style=\"font-weight: 400;\">However, we can do some optimization. Since the rays of light that don\u2019t reach the camera don\u2019t affect the picture, we can simply not simulate those rays. But how do we know that the rays of light don\u2019t reach the camera without simulating them? Simple. Assuming light travels the same way when reversed (aka. Camera to light source), we can simulate the rays of light bouncing from the camera to a light source. Since the camera is now emitting the rays of light rather than the light sources, it means some rays of light never reach the light sources. Although this method still simulates extra unneeded rays, this way of simulating light is still far superior to the other method (light source \u2192 camera).<\/span><\/p>\n<br><h2>Math<\/h2>\n<br><p><span style=\"font-weight: 400;\">\u201cWell then, what is the math involved?\u201d, you may ask. A lot of very complex math goes on when calculating the path of a ray of light, but I\u2019ll give you the basic rundown:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">A ray is cast from a point P, or the position of the camera.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">The vector of the ray is determined by combining the pixel the ray represents on the screen, the field of view and the angle of a camera.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">The vector is then multiplied by the camera\u2019s view distance (longer means more accurate but takes longer to compute.)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Uses an equation to calculate whether the ray intersects with a triangle using <\/span><a href=\"http:\/\/geomalgorithms.com\/a06-_intersect-2.html\"><span style=\"font-weight: 400;\">this<\/span><\/a><span style=\"font-weight: 400;\"> equation.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Repeat for all visible triangles (more efficient visibility calculation means less triangles to calculate intersections for)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If an intersection is not found, either discard the ray or act as if the ray has hit an emissive material, set its color to a sky color, and skip to step 10.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If an intersection is found, calculate the triangle\u2019s surface normal (optionally use an algorithm to calculate smooth normal for smooth materials)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">According to the triangle\u2019s material, refract the ray through the material or reflect it off of the material, applying the material\u2019s color multiplicatively.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If the material is not emissive (does not give off light), calculate the ray\u2019s reflection angle and repeat steps 3-10. (limiting # of bounces to prevent infinite looping)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">If the material is emissive, mix the light ray color with the corresponding pixel\u2019s color based on how many rays of light have already been calculated for that pixel.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Do that (8294400 \u00d7 amount of rays calculated per pixel) more times and voila! You have just raytraced a single 4k image!<\/span><\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; &nbsp; Sun and Lights By Daniel K., Age 14 In real life, the sun and lights emit rays of light that travel in (kind of) a straight line. When these rays of light hit an object, some of them are absorbed, some of them pass through (if the object is clear), and some &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.thecoderschool.com\/blog\/concepts-of-ray-tracing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Concepts of Ray Tracing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[69,65],"class_list":["post-11846","post","type-post","status-publish","format-standard","hentry","category-coder-blog","tag-student-showcase","tag-tips-tricks","entry"],"_links":{"self":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/comments?post=11846"}],"version-history":[{"count":9,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11846\/revisions"}],"predecessor-version":[{"id":11865,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11846\/revisions\/11865"}],"wp:attachment":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/media?parent=11846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/categories?post=11846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/tags?post=11846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}