{"id":20496,"date":"2023-02-01T08:45:46","date_gmt":"2023-02-01T08:45:46","guid":{"rendered":"https:\/\/www.booksofall.com\/?post_type=product&#038;p=20496"},"modified":"2023-02-01T09:02:44","modified_gmt":"2023-02-01T09:02:44","slug":"ray-tracing-in-one-weekend","status":"publish","type":"product","link":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/","title":{"rendered":"Ray Tracing: in One Weekend"},"content":{"rendered":"<div id=\"reading-mode-page-div-4\" class=\"reading-mode-page-div\">\n<h2>2.1 The PPM Image Format<\/h2>\n<p>Whenever you start a renderer, you need a way to see an image. The most straightforward way is to write it to a file. The catch is, there are so many formats. Many of those are complex. I always start with a plain text ppm file.<\/p>\n<p><b>Figure 1:<\/b>\u00a0<a href=\"https:\/\/www.chemaqua.com\/en-us\/Blogs\/what-is-ppm\">PPM<\/a> Example<\/p>\n<p>Let\u2019s make some <a href=\"https:\/\/www.simplilearn.com\/tutorials\/cpp-tutorial\/top-uses-of-c-plus-plus-programming\">C++ code<\/a> to output such a thing:<\/p>\n<p><em>#include &lt;iostream&gt;<\/em><\/p>\n<p><em>int main() {<\/em><\/p>\n<p><em>\/\/ Image<\/em><\/p>\n<p><em>const int image_width = 256;<br \/>\nconst int image_height = 256;<\/em><\/p>\n<p><em>\/\/ Render<\/em><\/p>\n<p><em>std::cout &lt;&lt; &#8220;P3\\n&#8221; &lt;&lt; image_width &lt;&lt; &#8216; &#8216; &lt;&lt; image_height &lt;&lt; &#8220;\\n255\\n&#8221;;<\/em><\/p>\n<p><em>for (int j = image_height-1; j &gt;= 0; &#8211;j) {<br \/>\nfor (int i = 0; i &lt; image_width; ++i) {<br \/>\nauto r = double(i) \/ (image_width-1);<br \/>\nauto g = double(j) \/ (image_height-1);<br \/>\nauto b = 0.25;<\/em><\/p>\n<p><em>int ir = static_cast&lt;int&gt;(255.999 * r);<br \/>\nint ig = static_cast&lt;int&gt;(255.999 * g);<br \/>\nint ib = static_cast&lt;int&gt;(255.999 * b);<\/em><\/p>\n<p><em>std::cout &lt;&lt; ir &lt;&lt; &#8216; &#8216; &lt;&lt; ig &lt;&lt; &#8216; &#8216; &lt;&lt; ib &lt;&lt; &#8216;\\n&#8217;; <\/em><\/p>\n<p><em>}<\/em><em style=\"font-size: 1rem;\">}<\/em><em>}<\/em><\/p>\n<p><b>Listing 1:<\/b>\u00a0[main.cc] Creating your first image<\/p>\n<p>There are some things to note in that code:<\/p>\n<ol>\n<li>The <a href=\"https:\/\/www.techopedia.com\/definition\/24012\/pixel\">pixels<\/a> are written out in rows with pixels left to right.<\/li>\n<li>The rows are written out from top to bottom.<\/li>\n<li>By convention, each of the red\/green\/blue components range from 0.0 to 1.0. We will relax that later when we internally use high dynamic range, but before output we will tone map to the zero to one range, so this code won\u2019t change.<\/li>\n<li>Red goes from fully off (black) to fully on (bright red) from left to right, and green goes from black at the bottom to fully on at the top. Red and green together make yellow so we should expect the upper right corner to be yellow.<\/li>\n<\/ol>\n<\/div>\n<div id=\"reading-mode-page-div-5\" class=\"reading-mode-page-div\">\n<p><b>Creating an Image File\u00a0<\/b><\/p>\n<p>Because the file is written to the program output, you&#8217;ll need to redirect it to an image file. Typically this is done from the command-line by using the &gt; redirection operator, like so:<\/p>\n<p><em>build\\Release\\inOneWeekend.exe &gt; image.ppm<\/em><\/p>\n<p>This is how things would look on Windows. On <a href=\"https:\/\/www.apple.com\/mac\/\">Mac<\/a> or <a href=\"https:\/\/www.linux.org\/\">Linux<\/a>, it would look like this:<\/p>\n<p><em>build\/inOneWeekend &gt; image.ppm<\/em><\/p>\n<p>Opening the output file (in <a href=\"https:\/\/apps.apple.com\/us\/app\/toyviewer\/id414298354?mt=12\">ToyViewer<\/a> on my Mac, but try it in your favorite viewer and Google \u201cppm viewer\u201d if your viewer doesn\u2019t support it) shows this result:<\/p>\n<p>Image 1: First PPM image<\/p>\n<p>Hooray! This is the graphics \u201chello world\u201d. If your image doesn\u2019t look like that, open the\u00a0output file in a text editor and see what it looks like. It should start something like this:<\/p>\n<p><em>P3<\/em><br \/>\n<em><span style=\"font-size: 1rem;\">256 256<br \/>\n<\/span>255 <\/em><br \/>\n<em>0 255 63 <\/em><br \/>\n<em>1 255 63 <\/em><br \/>\n<em>2 255 63<\/em><br \/>\n<em> 3 255 63<\/em><br \/>\n<em> 4 255 63<\/em><br \/>\n<em> 5 255 63<\/em><br \/>\n<em> 6 255 63<\/em><br \/>\n<em> 7 255 63 <\/em><br \/>\n<em>8 255 63<\/em><br \/>\n<em> 9 255 63 &#8230;<\/em><\/p>\n<p><b>Listing 2:<\/b>\u00a0First image output<\/p>\n<p>If it doesn\u2019t, then you probably just have some newlines or something similar that is confusing the image reader.<\/p>\n<p>If you want to produce more image types than PPM, I am a fan of stb_image.h , a header-only image library available on GitHub at <a href=\"https:\/\/github.com\/nothings\/stb\">https:\/\/github.com\/nothings\/stb<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p><iframe style=\"width: 100%; height: 750px; border: none;\" src=\"https:\/\/online.visual-paradigm.com\/share\/book\/creative-commons-license-cc-ray-tracing-in-one-weekend-18r39p7r3t?p=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"featured_media":20499,"template":"","meta":{"_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!"},"product_brand":[],"product_cat":[297],"product_tag":[],"class_list":{"0":"post-20496","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-graphics-programming","8":"first","9":"instock","10":"shipping-taxable","11":"product-type-simple"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Ray Tracing: in One Weekend - BooksOfAll Portuguese<\/title>\n<meta name=\"description\" content=\"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ray Tracing: in One Weekend - BooksOfAll Portuguese\" \/>\n<meta property=\"og:description\" content=\"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/\" \/>\n<meta property=\"og:site_name\" content=\"BooksOfAll Portuguese\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-01T09:02:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\" \/><meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"827\" \/>\n\t<meta property=\"og:image:height\" content=\"1169\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/\",\"url\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/\",\"name\":\"Ray Tracing: in One Weekend - BooksOfAll Portuguese\",\"isPartOf\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\",\"datePublished\":\"2023-02-01T08:45:46+00:00\",\"dateModified\":\"2023-02-01T09:02:44+00:00\",\"description\":\"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage\",\"url\":\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\",\"contentUrl\":\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg\",\"width\":\"827\",\"height\":\"1169\",\"caption\":\"Ray Tracing: in One Weekend\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.booksofall.com\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categories\",\"item\":\"https:\/\/www.booksofall.com\/pt\/categories\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ray Tracing: in One Weekend\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/#website\",\"url\":\"https:\/\/www.booksofall.com\/pt\/\",\"name\":\"BooksOfAll Portuguese\",\"description\":\"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.\",\"publisher\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.booksofall.com\/pt\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/#organization\",\"name\":\"BooksOfAll Portuguese\",\"url\":\"https:\/\/www.booksofall.com\/pt\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/www.booksofall.com\/pt\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2022\/06\/booksofall-logo-2.png\",\"contentUrl\":\"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2022\/06\/booksofall-logo-2.png\",\"width\":166,\"height\":30,\"caption\":\"BooksOfAll Portuguese\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/pt\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ray Tracing: in One Weekend - BooksOfAll Portuguese","description":"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/","og_locale":"pt_PT","og_type":"article","og_title":"Ray Tracing: in One Weekend - BooksOfAll Portuguese","og_description":"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!","og_url":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/","og_site_name":"BooksOfAll Portuguese","article_modified_time":"2023-02-01T09:02:44+00:00","og_image":[{"url":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","type":"","width":"","height":""},{"width":827,"height":1169,"url":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","twitter_misc":{"Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/","url":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/","name":"Ray Tracing: in One Weekend - BooksOfAll Portuguese","isPartOf":{"@id":"https:\/\/www.booksofall.com\/pt\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage"},"image":{"@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage"},"thumbnailUrl":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","datePublished":"2023-02-01T08:45:46+00:00","dateModified":"2023-02-01T09:02:44+00:00","description":"When showing color in digital device, they can also be presented in different codes. Learn more about ray tracing, and try to handle them!","breadcrumb":{"@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/"]}]},{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#primaryimage","url":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","contentUrl":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2023\/02\/Ray-Tracing-in-one-weekend.jpg","width":"827","height":"1169","caption":"Ray Tracing: in One Weekend"},{"@type":"BreadcrumbList","@id":"https:\/\/www.booksofall.com\/pt\/ray-tracing-in-one-weekend\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.booksofall.com\/pt\/"},{"@type":"ListItem","position":2,"name":"Categories","item":"https:\/\/www.booksofall.com\/pt\/categories\/"},{"@type":"ListItem","position":3,"name":"Ray Tracing: in One Weekend"}]},{"@type":"WebSite","@id":"https:\/\/www.booksofall.com\/pt\/#website","url":"https:\/\/www.booksofall.com\/pt\/","name":"BooksOfAll Portuguese","description":"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.","publisher":{"@id":"https:\/\/www.booksofall.com\/pt\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.booksofall.com\/pt\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-PT"},{"@type":"Organization","@id":"https:\/\/www.booksofall.com\/pt\/#organization","name":"BooksOfAll Portuguese","url":"https:\/\/www.booksofall.com\/pt\/","logo":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/www.booksofall.com\/pt\/#\/schema\/logo\/image\/","url":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2022\/06\/booksofall-logo-2.png","contentUrl":"https:\/\/www.booksofall.com\/pt\/wp-content\/uploads\/sites\/8\/2022\/06\/booksofall-logo-2.png","width":166,"height":30,"caption":"BooksOfAll Portuguese"},"image":{"@id":"https:\/\/www.booksofall.com\/pt\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/product\/20496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/media\/20499"}],"wp:attachment":[{"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/media?parent=20496"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/product_brand?post=20496"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/product_cat?post=20496"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.booksofall.com\/pt\/wp-json\/wp\/v2\/product_tag?post=20496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}