<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Matthieu Brucher&#039;s blog &#187; Interactive RayTracer</title> <atom:link href="http://matt.eifelle.com/category/cpp/interactive-raytracer/feed/" rel="self" type="application/rss+xml" /><link>http://matt.eifelle.com</link> <description></description> <lastBuildDate>Tue, 27 Jul 2010 07:04:23 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Interactive Raytracer 4: Reflection rays</title><link>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/</link> <comments>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/#comments</comments> <pubDate>Tue, 29 Sep 2009 08:13:40 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=763</guid> <description><![CDATA[Now, I will show the implementation of reflection (from the Whitted approach). It is basically using the reflection law and recurse the ray cast.
Reflection rays
Each object can reflect a ray more or less from a different object. A mirror would reflect the light totally, and a matte object would reflect nothing. Each new reflection is [...]]]></description> <content:encoded><![CDATA[<p>Now, I will show the implementation of reflection (from the Whitted approach). It is basically using the reflection law and recurse the ray cast.</p><h4>Reflection rays</h4><p>Each object can reflect a ray more or less from a different object. A mirror would reflect the light totally, and a matte object would reflect nothing. Each new reflection is a new ray tracing call, so it can be costly. The number of recursion levels will be fixed, even if an object reflects nothing: this will be implemented through shaders in the future.<br
/> <span
id="more-763"></span><br
/> Knowing the normal to the intersection point, the reflected ray can be computing: it&#8217;s just the symmetric ray of the incident ray.</p><p>Some elements will be stored in a material structure that will be used to compute the reflected color:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=primitives.h">primitives.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p7634"><td
class="code" id="p763code4"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">/// Caracteristics of the primitive at a given point</span>
<span style="color: #0000ff;">struct</span> MaterialPoint
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">/// Normale at the intersection point</span>
  Normal3df normal<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>In the past, I&#8217;ve used this structure to contain diffuse or reflection colors, but usually, an object has not mirror property in red and matte in green, so this was deleted.</p><p>Now, I need to update the raytracer:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p7635"><td
class="code" id="p763code5"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, Color<span style="color: #000040;">&amp;</span> color, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> level<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> index <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFirstCollision<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>index <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
  Primitive<span style="color: #000040;">*</span> primitive <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getPrimitive<span style="color: #008000;">&#40;</span>index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  MaterialPoint caracteristics<span style="color: #008080;">;</span>
  primitive<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColorNormal<span style="color: #008000;">&#40;</span>ray, dist, caracteristics<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  color <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColor<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist <span style="color: #000040;">*</span> ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, caracteristics, primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>level <span style="color: #000080;">&lt;</span> levels<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Ray ray_sec<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist <span style="color: #000040;">*</span> ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> caracteristics.<span style="color: #007788;">normal</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">*</span> caracteristics.<span style="color: #007788;">normal</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ray_sec.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>.<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>norm2<span style="color: #008000;">&#40;</span>ray_sec.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Color color_sec<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    computeColor<span style="color: #008000;">&#40;</span>ray_sec, color_sec, level<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    color <span style="color: #000040;">+</span><span style="color: #000080;">=</span> mult<span style="color: #008000;">&#40;</span>color_sec, caracteristics.<span style="color: #007788;">reflect</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I need to update the original collision method. Contrary to <a
href="http://matt.eifelle.com/2009/09/08/interactive-raytracer-3-lights-and-shadows/">the last post</a>, it isn&#8217;t enough to test if one primitive is hit by a ray, but I still need to test if the retrieved intersection isn&#8217;t too close to the original object. Indeed, I could be testing the intersection with the current object because of numerical instabilities. So I&#8217;ve added a minimum distance test:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=763&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p7636"><td
class="code" id="p763code6"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">long</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">getFirstCollision</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">float</span> min_dist <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">float</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">long</span> min_primitive <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">bool</span> test <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>intersect<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>test <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0001f</span> <span style="color: #000080;">&lt;</span> dist<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>dist <span style="color: #000080;">&lt;</span> min_dist<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      min_primitive <span style="color: #000080;">=</span> it <span style="color: #000040;">-</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      min_dist <span style="color: #000080;">=</span> dist<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>min_primitive <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">else</span>
  <span style="color: #008000;">&#123;</span>
    dist <span style="color: #000080;">=</span> min_dist<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> min_primitive<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, when a ray hits a primitive, a material structure is used to get the normal and the other parameters at the intersection point.</p><p>The <strong>level</strong> parameter indicates the recursion level we are at. If the level isn&#8217;t high enough, the symmetric ray is cast and the reflected color is used to add the color of the object. I do not test if the contribution is too small, it is possible in some implementations, but not here. Also, adding more recursion is not always costly, as after a while, all rays point outside the scene.</p><h4>Result</h4><p>Now, this is the result with 3 recursion levels and 3 lights:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_reflections.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_reflections-300x226.png" alt="IRT with reflection rays" title="IRT with reflection rays" width="300" height="226" class="aligncenter size-medium wp-image-770" /></a></center></p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/29/interactive-raytracer-4-reflection-rays/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Parallel Studio: Using Advisor Lite</title><link>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/</link> <comments>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/#comments</comments> <pubDate>Tue, 22 Sep 2009 08:02:28 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Distributed Computing]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Advisor]]></category> <category><![CDATA[Intel]]></category> <category><![CDATA[Parallel Studio]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=647</guid> <description><![CDATA[After reviewing Parallel Studio, I&#8217;ve decided to look after Advisor Lite. Intel offers it for free, before the actual Advisor is released with a future Parallel Studio version. It aims at steering multithreaded development with Parallel Studio.I&#8217;ve started with the Starting Guide, and in fact, it is the best way to know how to use [...]]]></description> <content:encoded><![CDATA[<p>After reviewing <a
href="http://matt.eifelle.com/2009/07/07/review-of-intel-parallel-studio/">Parallel Studio</a>, I&#8217;ve decided to look after Advisor Lite. Intel offers it for free, before the actual Advisor is released with a future Parallel Studio version. It aims at steering multithreaded development with Parallel Studio.<br
/> <span
id="more-647"></span><br
/> I&#8217;ve started with the Starting Guide, and in fact, it is the best way to know how to use this plugin. Advisor offers four steps, two of them being short-cuts to the online help, and the two others link to some Parallel Studio actions (namely hotspot in Amplifier and the threaded memory check with Inspector).<br
/> The online help is interesting, but once you know how you can parallelize an application and what to look for, the two Parallel Studio actions with the help of some macros presented in the Starting Guide are the only thing you need.</p><h4>Test on parallelizing a custom library</h4><p>I&#8217;ve decided to test Advisor Lite on my <a
href="http://matt.eifelle.com/category/cpp/interactive-raytracer/">Interactive Raytracer</a>. This is a test to verify if Advisor Lite finds the adequate parallelization and the memory sharing issues. It is a simple raytracer, so it can be parallelized for each pixel in the image. The only memory sharing issue that I know of is in the kd-tree ray traversal.</p><h4>Profiling the library</h4><p>First, I will profile the library. For the complete Advisor Lite workflow, I have to use Intel Compiler, and as it is faster than Microsoft&#8217;s compiler, I will use the <strong>timeit_image.py</strong> script instead of the <strong>measure_image.py</strong> I&#8217;ve used when profiling with <a
href="http://matt.eifelle.com/2009/04/07/profiling-with-valgrind/">Valgrind</a> or <a
href="http://matt.eifelle.com/2009/08/18/profiling-with-visual-studio-performance-tool/">Visual Studio</a>.</p><p>Amplifier can show the results in a bottom-up or in a top-down manner. Unfortunately, you only have the exclusive timing that is displayed. In my case, when displaying bottom-up results, the method <strong>getEntryExitDistances()</strong> is the most costly one. In the top-down view, unfortunately, I can&#8217;t have a simple tree, as it can be seen in the following view:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-advisor.png"><img
class="aligncenter size-medium wp-image-727" title="IRT: Amplifier profile (call-tree view)" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-advisor-300x187.png" alt="IRT: Amplifier profile (call-tree view)" width="300" height="187" /></a></p><p>In Visual Studio, I have the same results &#8211; more or less -, but with a correct top-down call-tree:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-msvc.png"><img
class="aligncenter size-medium wp-image-695" title="Profile returned by Visual Studio Performance Tool (call-tree)" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-profile-msvc-300x187.png" alt="Profile returned by Visual Studio Performance Tool (call-tree)" width="300" height="187" /></a></p><p>The method <strong>getEntryExitDistances()</strong> cannot be parallelized: it is recursively called, several times per pixel, which would lead to a lot of memory contention. The simpler task is thus to parallelized the pixel rendering, a perfect data-parallel problem.</p><h4>Annotation of the code</h4><p>OK, now I can annotate my code. I had to dig inside the help for this, as it was not mentionned in the Starting Guide that Intel provides a header, <strong>annotate.h</strong>, which mimics the issues you may encounter in a multithreaded application.</p><p>So you need to read at least once the online help so that you know the available annotation macros, how you can get them and how they will retrieve what you need. Once the code is annotated, it must be recompiled and then the sharing issues can be detected.</p><h4>Detection of sharing issues</h4><p>As expected, Inspector detected errors in the kd-tree traversal:</p><p><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-advisor-annotate-correctness.png"><img
class="aligncenter size-medium wp-image-696" title="Memory sharing issues detected by Inspector" src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt-advisor-annotate-correctness-300x187.png" alt="Memory sharing issues detected by Inspector" width="300" height="187" /></a><br
/> The solution in this case is to have a ray-traversal stack per thread, which will have to be implemented in whichever parallel library will be chosen, or simply to put the stack in the actual traversal algorithm and not in the instance.</p><h4>Using TBB</h4><p>I&#8217;ve decided to go for Thread Building Blocks, as it was already used for game development. This seemed to me a good idea, as it is a Open Source solution. So now, I will split the screen in 2D pieces, and add a thread-specific storage in the kd-tree class. Of course, I will have to add a flag to disable this paralellization if TBB is not available.</p><p>The actual parallelization will be in a future post in the Interactive Raytracer category. It is pretty straightforward once I had the different elements Parallel Studio gave me.</p><h4>Conclusion</h4><p>In fact Advisor is mainly the <strong>annotate.h</strong> header, as you have to know your program to put the macros at correct locations. The parallelization must be done by hand, as well as correcting the memory sharing issues.</p><p>The only problem I had is that <strong>annotate.h</strong> includes <strong>window.h</strong>. This header is not C++ compliant and declares some macros as <strong>max()</strong> (in fact I got the same issue with TBB headers!). As I use a <strong>max()</strong> function declared in <strong>std::numerical_limits</strong>,  I had to explicitely undefine this macro.</p><p>Safe from this, Advisor Lite is a good plugin, and I&#8217;m looking forward to seeing Advisor in a next Parallel Studio release.</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/22/parallel-studio-using-advisor-lite/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Interactive Raytracer 3: Lights and shadows</title><link>http://matt.eifelle.com/2009/09/08/interactive-raytracer-3-lights-and-shadows/</link> <comments>http://matt.eifelle.com/2009/09/08/interactive-raytracer-3-lights-and-shadows/#comments</comments> <pubDate>Tue, 08 Sep 2009 08:44:34 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=742</guid> <description><![CDATA[Adding the lights is the first step toward a Whitted raytracer. For each ray that hits an object, several rays can be cast, reflection, refraction and shadow. The last one is the one created with lights.
Lights and shadow rays
Without light, objects are bland, seem to have no depth, &#8230; Light on a scene can be [...]]]></description> <content:encoded><![CDATA[<p>Adding the lights is the first step toward a Whitted raytracer. For each ray that hits an object, several rays can be cast, reflection, refraction and shadow. The last one is the one created with lights.</p><h4>Lights and shadow rays</h4><p>Without light, objects are bland, seem to have no depth, &#8230; Light on a scene can be cast by several light sources. When a ray of the camera hits an object, the intersection point can be illuminated by one or several of those sources. Each of them contributes to the color of the object. If the light source direction is parallel with the normal of the object at the intersection point, the contribution will be maximum. If it is orthogonal, the contribution will be zero. The scalar product is the tool used to compute this quantity.<br
/> <span
id="more-742"></span><br
/> Each light source will be tested one after another. If there is no object between the light source and the intersection point, the light contribution will be added.<br
/> First, in the raytracer, the main method will be modified to get the normal through the <strong>MaterialPoint</strong> structure.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=742&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p74210"><td
class="code" id="p742code10"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, Color<span style="color: #000040;">&amp;</span> color, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> level<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
      Primitive<span style="color: #000040;">*</span> primitive <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFirstCollision<span style="color: #008000;">&#40;</span>ray, dist, tnear, tfar<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>primitive <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
      MaterialPoint caracteristics<span style="color: #008080;">;</span>
      primitive<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColorNormal<span style="color: #008000;">&#40;</span>ray, dist, caracteristics<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      color <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColor<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist <span style="color: #000040;">*</span> ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, caracteristics, primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, I add in the primitive class a property named <strong>diffuse</strong>, which indicates how much light is diffused by the object. Then, the scene can be modified:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=742&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p74211"><td
class="code" id="p742code11"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">const</span> Color SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Point3df<span style="color: #000040;">&amp;</span> center, <span style="color: #0000ff;">const</span> MaterialPoint<span style="color: #000040;">&amp;</span> caracteristics<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Color t_color<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Light<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> lights.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> lights.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      Vector3df path <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getCenter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> center<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">float</span> pathSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>norm2<span style="color: #008000;">&#40;</span>path<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      path.<span style="color: #007788;">normalize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      Ray ray<span style="color: #008000;">&#40;</span>center, path<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>testCollision<span style="color: #008000;">&#40;</span>ray, pathSize<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">float</span> cosphi <span style="color: #000080;">=</span> path <span style="color: #000040;">*</span> caracteristics.<span style="color: #007788;">normal</span> <span style="color: #000040;">*</span> primitive<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getDiffuse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cosphi <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
      t_color <span style="color: #000040;">+</span><span style="color: #000080;">=</span> mult<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>caracteristics.<span style="color: #007788;">color</span> <span style="color: #000040;">*</span> cosphi<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColor<span style="color: #008000;">&#40;</span>ray, pathSize<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">bool</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">testCollision</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span> dist<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">float</span> t_dist<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">bool</span> test <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>intersect<span style="color: #008000;">&#40;</span>ray, t_dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>test <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0001f</span> <span style="color: #000080;">&lt;</span> t_dist<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>t_dist <span style="color: #000080;">&lt;</span> dist<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, there are several ways of computing the light color. You can just say that a light broadcasts the same color everywhere, or you can use a more physical equation, where the color diminishes when the object is far from the light. As the light emits in every direction, the decrease is proportional to the square of the distance. This is what I used at first (in the newest versions, I&#8217;ve used the non-physical equation for some comparisons tests).</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=742&amp;download=light.cpp">light.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p74212"><td
class="code" id="p742code12"><pre class="cpp" style="font-family:monospace;">  Color Light<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span> dist<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> color <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>. <span style="color: #000040;">/</span> <span style="color: #008000;">&#40;</span>dist <span style="color: #000040;">*</span> dist<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><h4>Result</h4><p>Here is the result of this computation:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_lights.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/irt_lights-300x176.png" alt="Result with 3 lights" title="Result with 3 lights" width="300" height="176" class="aligncenter size-medium wp-image-761" /></a></center></p><p>One constant is hardcoded: <strong>0.0001f</strong>. This is the reason why there are some artefacts in the result image (on the red and the green spheres). If this number is raised to 0.001, the image will be more accurate. But then, if the spheres are nearer, then the shadows may be inaccurate&#8230;</p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/09/08/interactive-raytracer-3-lights-and-shadows/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Profiling with Visual Studio Performance Tool</title><link>http://matt.eifelle.com/2009/08/18/profiling-with-visual-studio-performance-tool/</link> <comments>http://matt.eifelle.com/2009/08/18/profiling-with-visual-studio-performance-tool/#comments</comments> <pubDate>Tue, 18 Aug 2009 08:14:39 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[General]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Microsoft]]></category> <category><![CDATA[Profiling]]></category> <category><![CDATA[Visual Studio]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=689</guid> <description><![CDATA[After presenting Valgrind as an emulation profiler, I will present Microsoft solution, Visual Studio Performance Tool. It is available in the Team Suite editions, and offers a sampling- and an instrumentation-based profiler. Of course, it is embedded in Visual Studio IDE and accessible from a solution.Using the profiler
First of all, the code must be compiled [...]]]></description> <content:encoded><![CDATA[<p>After presenting <a
href="http://matt.eifelle.com/2009/04/07/profiling-with-valgrind/">Valgrind</a> as an emulation profiler, I will present Microsoft solution, Visual Studio Performance Tool. It is available in the Team Suite editions, and offers a sampling- and an instrumentation-based profiler. Of course, it is embedded in Visual Studio IDE and accessible from a solution.<br
/> <span
id="more-689"></span></p><h4>Using the profiler</h4><p>First of all, the code must be compiled with <strong>/Zi</strong> or <strong>/Z7</strong>. Then, the link edition must be done with <strong>/DEBUG</strong>. Without these options, it won&#8217;t be possible to measure anything.</p><p>Performance Tool must be used in a Visual Studio Solution, which I don&#8217;t have in the case of the Interactive Raytracer. I will have to create an empty project, and I will create a bogus project, just to be able to browse the code in the IDE.</p><p>Once in Performance Tool, I create a new target which will be my generated library:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-ajouter-binaire-01.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-ajouter-binaire-01-300x187.png" alt="Adding a new target to a Performance session" title="Adding a new target to a Performance session" width="300" height="187" class="aligncenter size-medium wp-image-704" /></a></center></p><p>Then, I have to add it to the targets to profile:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-proprietes-02.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-proprietes-02-300x187.png" alt="Selected targets to profile" title="Selected targets to profile" width="300" height="187" class="aligncenter size-medium wp-image-703" /></a></center></p><p>Then I modify the launch properties to launch python with a sample script that is supposed to be descriptive enough. If you profile a Visual Studio project, you can use the debugging project properties (there are more environment parameters that you may modify).<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-ajouter-binaire-02.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-ajouter-binaire-02-300x187.png" alt="Modify launch properties" title="Modify launch properties" width="300" height="187" class="aligncenter size-medium wp-image-702" /></a></center></p><h4>Sampling</h4><p>To use the sampling profiler, you have to select it in the session parameters. Once done, you can launch the profiler.</p><p>This is the results I got from the IRT:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-01.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-01-300x187.png" alt="Sampling: Main result page" title="Sampling: Main result page" width="300" height="187" class="aligncenter size-medium wp-image-707" /></a></center><br
/> Only the most relevant are shown in the first panel. The first table is the most important inclusive function. The cost of a function is its cost as well as the functions it has called. Then the second table is the most important exclusive functions: only the cost of the function, without the cost of the called functions, is used.</p><p>This view helps finding the function to optimize. It display the current function at the middle of the panel, and then the caller functions at the top and the callee/called functions at the bottom. The displayed costs are inclusive and exclusive costs.<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-03.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-03-300x187.png" alt="Sampling: Function callers and callees" title="Sampling: Function callers and callees" width="300" height="187" class="aligncenter size-medium wp-image-708" /></a></center></p><p>A more general view, the call-tree, can help finding the hotspot as well. The caller can obviously not be displayed.<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-04.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-sampling-04-300x187.png" alt="Sampling: Call-tree" title="Sampling: Call-tree" width="300" height="187" class="aligncenter size-medium wp-image-709" /></a></center></p><p>It is easy to get access to the code by a left click, which is one of the reasons I&#8217;ve created a bigus Visual Studio project.</p><h4>Instrumentation</h4><p>Instrumentation-based profiling is somewhat different than sampling-based profiling. Here, the executable is modified to count cycles or counters, whereas in sampling-based profile, the program is sampled at regular intervals to get where the program is, the counters, &#8230;</p><p>Instrumentation results are quite like the sampling ones<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-01.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-01-300x187.png" alt="Instrumentation: Main result page" title="Instrumentation: Main result page" width="300" height="187" class="aligncenter size-medium wp-image-710" /></a></center></p><p><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-03.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-03-300x187.png" alt="Instrumentation: Callers and callees" title="Instrumentation: Callers and callees" width="300" height="187" class="aligncenter size-medium wp-image-711" /></a></center></p><p><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-04.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/msvc-instrumentation-04-300x187.png" alt="Instrumentation: Call-tree" title="Instrumentation: Call-tree" width="300" height="187" class="aligncenter size-medium wp-image-712" /></a></center></p><h4>Conclusion</h4><p>According to the different results I got, I know that I have some work to do on the math library. Well, this was some months ago, so now this code optimized as well. Based on Performance Tool profile, a lot has been optimized, as not creating rays inside the loop, &#8230;</p><p>I cannot really compare Performance Tool to Valgrind, as they are not the same kind of profilers. They each give more or less precise information on some parts of your program.<br
/> Compared to KCacheGrind, the callee map is missing, but Microsoft did a great job with the UI. The information may not be visualized as a 2D image, but it is still easy to extract it from the different views.</p><p>As a conclusion, I&#8217;d simply say that Performance Tool is an excellent tool Mixrosoft provides in its high-end Visual Studio versions. To bad it is not acceissble in the lower versions.</p>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/08/18/profiling-with-visual-studio-performance-tool/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Interactive RayTracer 2: Wrapping with SWIG</title><link>http://matt.eifelle.com/2009/08/11/interactive-raytracer-2-wrapping-with-swig/</link> <comments>http://matt.eifelle.com/2009/08/11/interactive-raytracer-2-wrapping-with-swig/#comments</comments> <pubDate>Tue, 11 Aug 2009 08:44:24 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[SWIG]]></category> <category><![CDATA[Software Construction]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=718</guid> <description><![CDATA[To ease profiling and testing, I have wrapped the library with SWIG.Creation of a wrapper
The module&#8217;s name will be IRT. For the vctor, I&#8217;ll use Numpy and its C API.
Here is the main file:?Download IRT.i%&#123;
#define SWIG_FILE_WITH_INIT
#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
%&#125;
%include &#34;numpy.i&#34;
%init %&#123;
import_array&#40;&#41;;
%&#125;
&#160;
%include &#34;constraints.i&#34;
&#160;
%module&#40;package=&#34;IRT&#34;, docstring=&#34;Python interface to the Interactive RayTracer&#34;&#41; IRT
&#160;
%nodefaultdtor;
%nodefaultctor;
&#160;
%include &#34;primitives.i&#34;
%include &#34;simple_scene.i&#34;
%include &#34;raytracer.i&#34;Nothing out of the ordinary, [...]]]></description> <content:encoded><![CDATA[<p>To ease profiling and testing, I have wrapped the library with SWIG.<br
/> <span
id="more-718"></span></p><h4>Creation of a wrapper</h4><p>The module&#8217;s name will be IRT. For the vctor, I&#8217;ll use Numpy and its C API.</p><p>Here is the main file:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=IRT.i">IRT.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71820"><td
class="code" id="p718code20"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#define SWIG_FILE_WITH_INIT</span>
<span style="color: #339900;">#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;numpy.i&quot;</span>
<span style="color: #000040;">%</span>init <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
import_array<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;constraints.i&quot;</span>
&nbsp;
<span style="color: #000040;">%</span>module<span style="color: #008000;">&#40;</span>package<span style="color: #000080;">=</span><span style="color: #FF0000;">&quot;IRT&quot;</span>, docstring<span style="color: #000080;">=</span><span style="color: #FF0000;">&quot;Python interface to the Interactive RayTracer&quot;</span><span style="color: #008000;">&#41;</span> IRT
&nbsp;
<span style="color: #000040;">%</span>nodefaultdtor<span style="color: #008080;">;</span>
<span style="color: #000040;">%</span>nodefaultctor<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;primitives.i&quot;</span>
<span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;simple_scene.i&quot;</span>
<span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;raytracer.i&quot;</span></pre></td></tr></table></div><p>Nothing out of the ordinary, the only trick is not to generate a default constructor or destructor so that nothing bad can happen.</p><p>Handling primitives (and lights in the future) is tricky: when creating an object, Python will handle its life, but in IRT&#8217;s case, the life time is the scene responsibility. Without special instructions, if the Python object is deleted, so is the undelying C++ object and then we look at memory corruption, double frees, &#8230;. I&#8217;ll use a SWIG function to split this.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=primitives.i">primitives.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71821"><td
class="code" id="p718code21"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#include &quot;primitives.h&quot;</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Vector3df</span><span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>PyArrayObject<span style="color: #000040;">*</span> array<span style="color: #000080;">=</span><span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">int</span> is_new_object<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  array <span style="color: #000080;">=</span> obj_to_array_contiguous_allow_conversion<span style="color: #008000;">&#40;</span>$input, NPY_FLOAT, <span style="color: #000040;">&amp;</span>is_new_object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>array <span style="color: #000040;">||</span> <span style="color: #000040;">!</span>require_dimensions<span style="color: #008000;">&#40;</span>array, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>array<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dimensions<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> SWIG_fail<span style="color: #008080;">;</span>
&nbsp;
  $<span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> IRT<span style="color: #008080;">::</span><span style="color: #007788;">Vector3df</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> array<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>freearg<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Vector3df</span><span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>is_new_object$argnum <span style="color: #000040;">&amp;&amp;</span> array$argnum<span style="color: #008000;">&#41;</span> Py_DECREF<span style="color: #008000;">&#40;</span>array$argnum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Color</span><span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>PyArrayObject<span style="color: #000040;">*</span> array<span style="color: #000080;">=</span><span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">int</span> is_new_object<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  array <span style="color: #000080;">=</span> obj_to_array_contiguous_allow_conversion<span style="color: #008000;">&#40;</span>$input, NPY_FLOAT, <span style="color: #000040;">&amp;</span>is_new_object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>array <span style="color: #000040;">||</span> <span style="color: #000040;">!</span>require_dimensions<span style="color: #008000;">&#40;</span>array, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>array<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dimensions<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> IRT<span style="color: #008080;">::</span><span style="color: #007788;">nbColors</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> SWIG_fail<span style="color: #008080;">;</span>
&nbsp;
  $<span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> IRT<span style="color: #008080;">::</span><span style="color: #007788;">Color</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> array<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>freearg<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Color</span><span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>is_new_object$argnum <span style="color: #000040;">&amp;&amp;</span> array$argnum<span style="color: #008000;">&#41;</span> Py_DECREF<span style="color: #008000;">&#40;</span>array$argnum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I use here the constant <strong>IRT::nbColors</strong>. This indicates the number of colors (which means that I could use more that RGB, or it can be useful in other contexts, as acoustics). These typemaps are freely inspired by Numpy&#8217;s.</p><p>The following typemaps handle giving or taking life responsibility from Python to C++ when interacting with the scene.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=primitives.i">primitives.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71822"><td
class="code" id="p718code22"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span> IRT<span style="color: #008080;">::</span><span style="color: #007788;">Primitive</span><span style="color: #000040;">*</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>SWIG_ConvertPtr<span style="color: #008000;">&#40;</span>$input,<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>$<span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>,$<span style="color: #0000dd;">1</span>_descriptor, SWIG_POINTER_EXCEPTION <span style="color: #000040;">|</span> SWIG_POINTER_DISOWN<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> SWIG_fail<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>out<span style="color: #008000;">&#41;</span> IRT<span style="color: #008080;">::</span><span style="color: #007788;">Primitive</span><span style="color: #000040;">*</span>
<span style="color: #008000;">&#123;</span>
  $result <span style="color: #000080;">=</span> SWIG_NewPointerObj<span style="color: #008000;">&#40;</span>$<span style="color: #0000dd;">1</span>, $<span style="color: #0000dd;">1</span>_descriptor, SWIG_POINTER_OWN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> IRT
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">class</span> Primitive
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    ~Primitive<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">class</span> Sphere<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Primitive
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    Sphere<span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Vector3df</span><span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span> dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ~Sphere<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">void</span> setColor<span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Color</span><span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>I didn&#8217;t expose the whole interface to Python. I don&#8217;t want everyone to do fancy stuff with the raytracer&#8217;s inner code.</p><p>Now, for the scene, there is not much to say:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=simple_scene.i">simple_scene.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71823"><td
class="code" id="p718code23"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#include &quot;simple_scene.h&quot;</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>apply Pointer NONNULL<span style="color: #008000;">&#123;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Primitive</span><span style="color: #000040;">*</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> IRT
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">class</span> SimpleScene
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    SimpleScene<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ~SimpleScene<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    IRT<span style="color: #008080;">::</span><span style="color: #007788;">Primitive</span><span style="color: #000040;">*</span> removePrimitive<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">bool</span> addPrimitive<span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">Primitive</span><span style="color: #000040;">*</span> primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>If a NULL pointer is passed to the function, SWIG throws itself an exception, I don&#8217;t have to handle this case myself. This is done in <strong>constraints.i</strong>.</p><p>Finally, this is the raytracer wrapper:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=raytracer.i">raytracer.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71824"><td
class="code" id="p718code24"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#include &quot;raytracer.h&quot;</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>typecheck<span style="color: #008000;">&#40;</span>SWIG_TYPECHECK_DOUBLE_ARRAY<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> INPLACE_ARRAY<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  $<span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> is_array<span style="color: #008000;">&#40;</span>$input<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> PyArray_EquivTypenums<span style="color: #008000;">&#40;</span>array_type<span style="color: #008000;">&#40;</span>$input<span style="color: #008000;">&#41;</span>,NPY_FLOAT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> INPLACE_ARRAY<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#40;</span>PyArrayObject<span style="color: #000040;">*</span> array<span style="color: #000080;">=</span><span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">int</span> is_new_object<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  array <span style="color: #000080;">=</span> obj_to_array_contiguous_allow_conversion<span style="color: #008000;">&#40;</span>$input, NPY_FLOAT, <span style="color: #000040;">&amp;</span>is_new_object<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  $<span style="color: #0000dd;">1</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>$<span style="color: #0000dd;">1</span>_ltype<span style="color: #008000;">&#41;</span> array<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> IRT
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">class</span> Raytracer
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    Raytracer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight, <span style="color: #0000ff;">float</span> width, <span style="color: #0000ff;">float</span> height, <span style="color: #0000ff;">float</span> depth<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ~Raytracer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> draw<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> INPLACE_ARRAY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">void</span> setResolution<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">void</span> setScene<span style="color: #008000;">&#40;</span>IRT<span style="color: #008080;">::</span><span style="color: #007788;">SimpleScene</span><span style="color: #000040;">*</span> scene<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The raytracer wrapper must be able to set its scene, draw it and change the actual resolution. The main issue here is the image where the scene will be drawn on, implemented here as a Numpy array in Python.</p><h4>Python scene and display</h4><p>To generate profiles, I&#8217;ve created a small sample scene:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=sample.i">sample.i</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71825"><td
class="code" id="p718code25"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> IRT
<span style="color: #ff7700;font-weight:bold;">import</span> numpy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Sample<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">raytracer</span> = IRT.<span style="color: black;">Raytracer</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">800</span>, <span style="color: #ff4500;">600</span>, <span style="color: #ff4500;">8</span>., <span style="color: #ff4500;">6</span>., <span style="color: #ff4500;">40</span>.<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">scene</span> = IRT.<span style="color: black;">SimpleScene</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    sphere = IRT.<span style="color: black;">Sphere</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">80</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">2</span>.<span style="color: black;">&#41;</span>
    sphere.<span style="color: black;">setColor</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>., <span style="color: #ff4500;">0</span>., <span style="color: #ff4500;">1</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">scene</span>.<span style="color: black;">addPrimitive</span><span style="color: black;">&#40;</span>sphere<span style="color: black;">&#41;</span>
    sphere = IRT.<span style="color: black;">Sphere</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>., <span style="color: #ff4500;">1</span>., <span style="color: #ff4500;">60</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span>.<span style="color: black;">&#41;</span>
    sphere.<span style="color: black;">setColor</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>., <span style="color: #ff4500;">0</span>., <span style="color: #ff4500;">0</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">scene</span>.<span style="color: black;">addPrimitive</span><span style="color: black;">&#40;</span>sphere<span style="color: black;">&#41;</span>
    sphere = IRT.<span style="color: black;">Sphere</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">2</span>., -<span style="color: #ff4500;">1</span>., <span style="color: #ff4500;">60</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span>.<span style="color: black;">&#41;</span>
    sphere.<span style="color: black;">setColor</span><span style="color: black;">&#40;</span>numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>., <span style="color: #ff4500;">1</span>., <span style="color: #ff4500;">0</span>.<span style="color: black;">&#41;</span>, dtype=numpy.<span style="color: black;">float32</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">scene</span>.<span style="color: black;">addPrimitive</span><span style="color: black;">&#40;</span>sphere<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #008000;">self</span>.<span style="color: black;">raytracer</span>.<span style="color: black;">setScene</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">scene</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, screen<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">raytracer</span>.<span style="color: black;">draw</span><span style="color: black;">&#40;</span>screen<span style="color: black;">&#41;</span></pre></td></tr></table></div><p>The screen has a size of (800,600) with a resolution of 0.1. Three spheres will be drawn, one blue, one green and one red. <strong>__call__()</strong> is where the time can be measured or the profile is relevent.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=718&amp;download=timeit_image.py">timeit_image.py</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p71826"><td
class="code" id="p718code26"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">timeit</span>
&nbsp;
setup=<span style="color: #483d8b;">&quot;&quot;&quot;import numpy
import sample
import pylab
&nbsp;
s = sample.Sample()
&nbsp;
screen = numpy.zeros((600, 800, 3), dtype=numpy.float32)
&quot;&quot;&quot;</span>
&nbsp;
timer = <span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'s(screen)'</span>, setup=setup<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> timer.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span></pre></td></tr></table></div><p>Here is the result with Matplotlib:<br
/><center><a
href="http://matt.eifelle.com/wp-content/uploads/2009/08/scene.01.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/08/scene.01-300x226.png" alt="A simple scene with 3 spheres (Matplotlib)" title="A simple scene with 3 spheres (Matplotlib)" width="300" height="226" class="aligncenter size-medium wp-image-722" /></a></center></p><p>The next post on IRT will expose lights and shadows.</p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/08/11/interactive-raytracer-2-wrapping-with-swig/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Interactive RayTracer 1: A barebone raytracer</title><link>http://matt.eifelle.com/2009/08/04/interactive-raytracer-1-a-barebone-raytracer/</link> <comments>http://matt.eifelle.com/2009/08/04/interactive-raytracer-1-a-barebone-raytracer/#comments</comments> <pubDate>Tue, 04 Aug 2009 08:35:14 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Raytracing]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=643</guid> <description><![CDATA[At last, I&#8217;m starting with my first post on my attempt on Interactive RayTracing. This first one will only be on the generic global implementation.
A matrix library must be used, the same basis class will be used for each element, point or color, but with a different size (if needed). I will use a typedef [...]]]></description> <content:encoded><![CDATA[<p>At last, I&#8217;m starting with my first post on my attempt on Interactive RayTracing. This first one will only be on the generic global implementation.</p><p>A matrix library must be used, the same basis class will be used for each element, point or color, but with a different size (if needed). I will use a <strong>typedef</strong> for defining each of them. This will help explaining what is going on. I will not explain the code of the library, althoug the optimization of the raytracer will surely have to be done in this part of the code as well.</p><p>So a vector will be named (for the moment) <strong>Vector3df</strong>, a point <strong>Point3df</strong>, a normal <strong>Normal3df</strong> and a color <strong>Color</strong>. All elements will live in the <strong>IRT</strong> namespace.<br
/> <span
id="more-643"></span></p><h4>Basic classes</h4><p>These basis classes, with the ray class that will be shown later, are what makes the raytracer work. The primitives describe what objects are in the scene, which color they have, if they are transprent, if they reflect light, &#8230; The scene itself is a smart container for the primitives and the lights which can allow for fast ray traversal (if implemeted). The raytracer traces rays through the scene and saves the result in an image.</p><p>First the <strong>Ray</strong> class is only an aggregation of two data, a vector and a starting point.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=ray.h">ray.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64342"><td
class="code" id="p643code42"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">class</span> Ray
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    Point3df origin_<span style="color: #008080;">;</span>
    Vector3df direction_<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    _export_tools Ray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Point3df<span style="color: #000040;">&amp;</span> origin, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools ~Ray<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">const</span> Point3df<span style="color: #000040;">&amp;</span> origin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> origin_<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    Point3df<span style="color: #000040;">&amp;</span> origin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> origin_<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> direction_<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> direction_<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>The <strong>_export_tools</strong> macro is used for Windows shared libraries, where you have to explicitely tell which function is to be exported (or imported). This will change in the future when the library will be completely template.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=ray.cpp">ray.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64343"><td
class="code" id="p643code43"><pre class="cpp" style="font-family:monospace;">  Ray<span style="color: #008080;">::</span><span style="color: #007788;">Ray</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Point3df<span style="color: #000040;">&amp;</span> origin, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span>origin_<span style="color: #008000;">&#40;</span>origin<span style="color: #008000;">&#41;</span>, direction_<span style="color: #008000;">&#40;</span>direction<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  Ray<span style="color: #008080;">::</span>~Ray<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><h4>Primitives</h4><p>A primitive is an object in the scene. For the moment, it will be a pure virtual class (an interface) with some methods, like the intersection as well as the color computation.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=primitives.h">primitives.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64344"><td
class="code" id="p643code44"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">class</span> Primitive
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> ~Primitive<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">bool</span> intersect<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> computeColorNormal<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span> dist, Color<span style="color: #000040;">&amp;</span> color, Vector3df<span style="color: #000040;">&amp;</span> normal<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>My first primitive will be a Sphere with a uniform color. The most complex computation here is the intersection between a ray and the sphere:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=primitives.cpp">primitives.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64345"><td
class="code" id="p643code45"><pre class="cpp" style="font-family:monospace;">  Sphere<span style="color: #008080;">::</span><span style="color: #007788;">Sphere</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Point3df<span style="color: #000040;">&amp;</span> center, <span style="color: #0000ff;">float</span> radius<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span>center<span style="color: #008000;">&#40;</span>center<span style="color: #008000;">&#41;</span>, radius<span style="color: #008000;">&#40;</span>radius<span style="color: #008000;">&#41;</span>, color<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> Sphere<span style="color: #008080;">::</span><span style="color: #007788;">setColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Color<span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color <span style="color: #000080;">=</span> color<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">bool</span> Sphere<span style="color: #008080;">::</span><span style="color: #007788;">intersect</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> A <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>.<span style="color: #007788;">f</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> B <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> center<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> C <span style="color: #000080;">=</span> norm2<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> center<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> radius <span style="color: #000040;">*</span> radius<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">float</span> delta <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>B <span style="color: #000040;">*</span> B <span style="color: #000040;">-</span> A <span style="color: #000040;">*</span> C<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>delta <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> disc <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>delta<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>dist <span style="color: #000080;">=</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>B <span style="color: #000040;">+</span> disc<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span>
      dist <span style="color: #000080;">=</span> <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span>B <span style="color: #000040;">-</span> disc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The idea is to consider a straight line parametered with <strong>dist</strong>. With this equation, we add to the system the equation of the sphere. This way, a second degree polynomial must be solved, and the smallest positive root is the solution (if it exists). To simplify computation, the direction vector is considered unitary.</p><p>Now, the color can be computed, as well as the normal (very easy to do with a sphere). In fact the normal is not used here, but will be used in the future.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=primitives.cpp">primitives.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64346"><td
class="code" id="p643code46"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">void</span> Sphere<span style="color: #008080;">::</span><span style="color: #007788;">computeColorNormal</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span> dist, Color<span style="color: #000040;">&amp;</span> color, Normal3df<span style="color: #000040;">&amp;</span> normal<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Vector3df collide<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">origin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> dist<span style="color: #000040;">*</span>ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    normal <span style="color: #000080;">=</span> collide <span style="color: #000040;">-</span> center<span style="color: #008080;">;</span>
    normal <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>.<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>norm2<span style="color: #008000;">&#40;</span>normal<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    color <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><h4>The scene</h4><p>The scene stores the primitives. Primitives may be added, deleted or accessed. BEsides, the scene is in charge of finding objects hit by a ray with the method <strong>getFirstCollision()</strong>.<br
/> simple_scene.h</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=simple_scene.h">simple_scene.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64347"><td
class="code" id="p643code47"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">class</span> SimpleScene
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> primitives<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    _export_tools SimpleScene<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools ~SimpleScene<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools Primitive<span style="color: #000040;">*</span> getPrimitive<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools Primitive<span style="color: #000040;">*</span> removePrimitive<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">long</span> getFirstCollision<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">bool</span> addPrimitive<span style="color: #008000;">&#40;</span>Primitive<span style="color: #000040;">*</span> primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>The scene is also in charge of deleting the primitives it stores. When a new primitive is added to the scene, it is charge of its life time.<br
/> Here is the actual code:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64348"><td
class="code" id="p643code48"><pre class="cpp" style="font-family:monospace;">  SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">SimpleScene</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span>primitives<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  SimpleScene<span style="color: #008080;">::</span>~SimpleScene<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
      <span style="color: #0000dd;">delete</span> <span style="color: #000040;">*</span>it<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  Primitive<span style="color: #000040;">*</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">getPrimitive</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> index<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> primitives<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Wen a primitive is removed from the scene, it is returned to the user:</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64349"><td
class="code" id="p643code49"><pre class="cpp" style="font-family:monospace;">  Primitive<span style="color: #000040;">*</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">removePrimitive</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> index<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">advance</span><span style="color: #008000;">&#40;</span>it, index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Primitive<span style="color: #000040;">*</span> primitive <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>it<span style="color: #008080;">;</span>
    primitives.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> primitive<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>Now, this is the main kernel. In this case, each primitive is tested and if the ray hits the primitive, the distance from the ray to the primitive is updated in <strong>dist</strong> and the primitive index is returned.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=simple_scene.cpp">simple_scene.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64350"><td
class="code" id="p643code50"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">long</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">getFirstCollision</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, <span style="color: #0000ff;">float</span><span style="color: #000040;">&amp;</span> dist<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> min_dist <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">float</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> min_primitive <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>Primitive<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it <span style="color: #000080;">=</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">bool</span> test <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>intersect<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>test<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        min_primitive <span style="color: #000080;">=</span> it <span style="color: #000040;">-</span> primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        min_dist <span style="color: #000080;">=</span> dist<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>min_primitive <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
    <span style="color: #008000;">&#123;</span>
      dist <span style="color: #000080;">=</span> min_dist<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">return</span> min_primitive<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">bool</span> SimpleScene<span style="color: #008080;">::</span><span style="color: #007788;">addPrimitive</span><span style="color: #008000;">&#40;</span>Primitive<span style="color: #000040;">*</span> primitive<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>primitives.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, primitive<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primitives.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
    primitives.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>primitive<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>A primitive cannot be added twice to the the scene. If the insertion fails, <strong>false</strong> is returned by <strong>addPrimitive()</strong>.</p><h4>The raytracer</h4><p>A raytracer can use any scene from the moment it is possible to get the primitive hit by a ray.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.h">raytracer.h</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64351"><td
class="code" id="p643code51"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">class</span> Raytracer
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">void</span> generateRay<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> x, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> y, Ray<span style="color: #000040;">&amp;</span> ray<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> computeColor<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, Color<span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> updateParameters<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    _export_tools Raytracer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight, <span style="color: #0000ff;">float</span> width, <span style="color: #0000ff;">float</span> height, <span style="color: #0000ff;">float</span> depth<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools ~Raytracer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">void</span> draw<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> screen<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">void</span> setViewer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> width, <span style="color: #0000ff;">float</span> height, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> origin, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">void</span> setResolution<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    _export_tools <span style="color: #0000ff;">void</span> setScene<span style="color: #008000;">&#40;</span>SimpleScene<span style="color: #000040;">*</span> scene<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    Point3df origin<span style="color: #008080;">;</span>
    Vector3df direction<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> width<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> height<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> depth<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">float</span> precompWidth<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> precompHeight<span style="color: #008080;">;</span>
&nbsp;
    SimpleScene<span style="color: #000040;">*</span> scene<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div><p>Besides the key raytracer methods, this class provides an interface to set the camera (even if the code does not do this at the moment).</p><p>First, a screen has a height and a width with a number of pixels for those. The screen is at some distance from the camera (the depth).</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64352"><td
class="code" id="p643code52"><pre class="cpp" style="font-family:monospace;">  Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">Raytracer</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight, <span style="color: #0000ff;">float</span> width, <span style="color: #0000ff;">float</span> height, <span style="color: #0000ff;">float</span> depth<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span>origin<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span>, direction<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span>, pixelWidth<span style="color: #008000;">&#40;</span>pixelWidth<span style="color: #008000;">&#41;</span>, pixelHeight<span style="color: #008000;">&#40;</span>pixelHeight<span style="color: #008000;">&#41;</span>, width<span style="color: #008000;">&#40;</span>width<span style="color: #008000;">&#41;</span>, height<span style="color: #008000;">&#40;</span>height<span style="color: #008000;">&#41;</span>, depth<span style="color: #008000;">&#40;</span>depth<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    direction<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    updateParameters<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  Raytracer<span style="color: #008080;">::</span>~Raytracer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>With these elements, some additional parameters are precomputed.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64353"><td
class="code" id="p643code53"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">generateRay</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> x, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> y, Ray<span style="color: #000040;">&amp;</span> ray<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
  <span style="color: #008000;">&#123;</span>
    ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> precompWidth <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>x <span style="color: #000040;">-</span> pixelWidth <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> precompHeight <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>y <span style="color: #000040;">-</span> pixelHeight <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> depth<span style="color: #008080;">;</span>
&nbsp;
    ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>.<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>norm2<span style="color: #008000;">&#40;</span>ray.<span style="color: #007788;">direction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>This method builds an unitary ray in place. Based on the position of the camera, a ray to each pixel is generated.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64354"><td
class="code" id="p643code54"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">draw</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #000040;">*</span> screen<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
  <span style="color: #008000;">&#123;</span>
    Ray ray<span style="color: #008000;">&#40;</span>origin, direction<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> pixelHeight<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>j<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> pixelWidth<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        generateRay<span style="color: #008000;">&#40;</span>i, j, ray<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        Color color<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>.<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        computeColor<span style="color: #008000;">&#40;</span>ray, color<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> k <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> k <span style="color: #000080;">&lt;</span> nbColors<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>k<span style="color: #008000;">&#41;</span>
          screen<span style="color: #008000;">&#91;</span>nbColors <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>j<span style="color: #000040;">*</span> pixelWidth <span style="color: #000040;">+</span> i<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> k<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> color<span style="color: #008000;">&#40;</span>k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>This was the main method to draw an image from the scene. It works by computing the color for each ray.</p><p>To get this color, the following method searches the scene for the nearest hit object. If one is found, the color is generated by <strong>computeColorNormal()</strong>. This method is also the one that will be in charge of the secondary rays and shadows, but this is for another post.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64355"><td
class="code" id="p643code55"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">computeColor</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Ray<span style="color: #000040;">&amp;</span> ray, Color<span style="color: #000040;">&amp;</span> color<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> dist<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">long</span> index <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getFirstCollision<span style="color: #008000;">&#40;</span>ray, dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>index <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
    Normal3df normal<span style="color: #008080;">;</span>
    Primitive<span style="color: #000040;">*</span> primitive <span style="color: #000080;">=</span> scene<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>getPrimitive<span style="color: #008000;">&#40;</span>index<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    primitive<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>computeColorNormal<span style="color: #008000;">&#40;</span>ray, dist, color, normal<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><p>The next methods help updating the raytracer.</p><div
class="wp_codebox_msgheader"><span
class="right"><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left2">Download <a
href="http://matt.eifelle.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=643&amp;download=raytracer.cpp">raytracer.cpp</a></span><div
class="codebox_clear"></div></div><div
class="wp_codebox"><table><tr
id="p64356"><td
class="code" id="p643code56"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">setScene</span><span style="color: #008000;">&#40;</span>SimpleScene<span style="color: #000040;">*</span> scene<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>scene <span style="color: #000080;">=</span> scene<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">setResolution</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelWidth, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> pixelHeight<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>pixelWidth <span style="color: #000080;">=</span> pixelWidth<span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>pixelHeight <span style="color: #000080;">=</span> pixelHeight<span style="color: #008080;">;</span>
&nbsp;
    updateParameters<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">setViewer</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> width, <span style="color: #0000ff;">float</span> height, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> origin, <span style="color: #0000ff;">const</span> Vector3df<span style="color: #000040;">&amp;</span> direction<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>width <span style="color: #000080;">=</span> width<span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>height <span style="color: #000080;">=</span> height<span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>origin <span style="color: #000080;">=</span> origin<span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>direction <span style="color: #000080;">=</span> direction<span style="color: #008080;">;</span>
&nbsp;
    updateParameters<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> Raytracer<span style="color: #008080;">::</span><span style="color: #007788;">updateParameters</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    precompWidth <span style="color: #000080;">=</span> width <span style="color: #000040;">/</span> pixelWidth<span style="color: #008080;">;</span>
    precompHeight <span style="color: #000080;">=</span> height <span style="color: #000040;">/</span> pixelHeight<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div><h4>Coming next</h4><p>It is now possible to use the library for a basic rendering in C++, but I&#8217;ve decided to add a small Python wrapper to allow for more easy testing. This will be the main topic of my next post on raytracing.</p><p><a
href="http://www.launchpad.net/irt">Link to Launchpad</a>.</p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/08/04/interactive-raytracer-1-a-barebone-raytracer/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Interactive RayTracer</title><link>http://matt.eifelle.com/2009/05/19/interactive-raytracer/</link> <comments>http://matt.eifelle.com/2009/05/19/interactive-raytracer/#comments</comments> <pubDate>Tue, 19 May 2009 08:28:02 +0000</pubDate> <dc:creator>Matt</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[General]]></category> <category><![CDATA[Interactive RayTracer]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Graphics]]></category> <category><![CDATA[multithread]]></category> <category><![CDATA[Multithreaded applications]]></category> <category><![CDATA[Parallel computing]]></category> <category><![CDATA[Profiling]]></category> <category><![CDATA[Raytracing]]></category> <category><![CDATA[SIMD]]></category><guid
isPermaLink="false">http://matt.eifelle.com/?p=248</guid> <description><![CDATA[Some months ago, I&#8217;ve decided to dig into raytracing, and more exactly interactive raytracing. So I&#8217;ve started writting my own library, based on several publications.
nVidia announced recently its own framework, Intel wants also to do raytracing on Larrabee, it is the current trend.First, raytracing is a tool to draw a picture from a scene. It&#8217;s [...]]]></description> <content:encoded><![CDATA[<p>Some months ago, I&#8217;ve decided to dig into raytracing, and more exactly interactive raytracing. So I&#8217;ve started writting my own library, based on several publications.<br
/> nVidia announced recently its own framework, Intel wants also to do raytracing on Larrabee, it is the current trend.<br
/> <span
id="more-248"></span><br
/> First, raytracing is a tool to draw a picture from a scene. It&#8217;s like a camera observing the scene, and to know the content of each pixel on the film, a ray is cast through the scene. Depending on the objects, rays will propagate to other objects or lights, and then the actual color of the camera pixel can then be computed.</p><p>A raytracer is in fact really easy to write. The math formulae are simple for a version that will return satisfactory results. On the contrary, an interactive or &#8220;real-time&#8221; one is more complicated. This is why I started my small project and why I will blog from time to time about it.</p><p>Interactive or real-time means that the drawing must be fast, really fast. This also means that realistic drawing is out of the question. I&#8217;ve started with a simple raytracer, with some optimizations, and then I&#8217;ve added some algorithmic optimizations. I will talk about all this in several posts.</p><h4>How does a raytracer actually work?</h4><p>In physical vision, light rays travel around the world from the sources, hitting objects, then reflected or transmitted and finally arriving at the eye (or camera). The principle of raytracing is to do it backwards, from the camera to the light sources. The camera film, or the retina, is symbolized by a screen.</p><p><center><div
id="attachment_483" class="wp-caption aligncenter" style="width: 310px"><a
href="http://matt.eifelle.com/wp-content/uploads/2009/05/irt.png"><img
src="http://matt.eifelle.com/wp-content/uploads/2009/05/irt-300x180.png" alt="How a raytracer works" title="Interaction of a ray with the scene" width="300" height="180" class="size-medium wp-image-483" /></a><p
class="wp-caption-text">How a raytracer works</p></div></center></p><p>In this case, the emitted ray hits object 1 with some angle to the normal (the normal vector is the vector orthogonal to the tangent plane of the object). Object 1 gets some light and so the color ray depends on this light, the object and the angle between the normal and the direction of the light.<br
/> The ray is then reflected to object 2 and hits it with another angle. As for object 2, it gets some light and the reflected ray carries a specific color. This color is then blended with the color of the primary ray, and this final color is the color of the pixel on the screen.</p><h4>Coming next</h4><p>I&#8217;ve written my library in C++, tested it with several compilers on different platforms. So as to test the code, to profile it and to create a scene in an elegant manner, I&#8217;ve added a small Python wrapper. This will be the subject of my next post.</p><p>Then, I&#8217;ll go through secondary and shadow rays, a GUI for the raytracer, acceleration structures, &#8230;</p><p>The code is released under the LGPL on <a
href="https://launchpad.net/irt">Launchpad</a>.</p><h4>Additional links</h4><p><a
href="http://software.intel.com/en-us/articles/interactive-ray-tracing">Interactive Raytracer on Intel website</a></p><p><a
href="http://software.intel.com/en-us/articles/architecture-of-a-real-time-ray-tracer-1">An Intel article on real-time raytracer architecture</a></p><p><a
href="http://developer.nvidia.com/object/nvision08-IRT.html">nVidia presentation on raytracing</a></p><form
action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input
type="hidden" name="cmd" value="_xclick" /> <input
type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input
type="hidden" name="item_name" value="Buy Me a Coffee!" /><input
type="hidden" name="currency_code" value="USD" /><span
style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br
/><br
/><select
id="amount" name="amount" class=""><option
value="3">Capuccino - 3$</option><option
value="6">Frappuccino - 6$</option><option
value="10">Hot Chocolate - 10$</option><option
value="20">Expensive Coffee - 20$</option><option
value="50">Alien Coffee - 50$</option></select><br
/><br
/><strong>Other Amount:</strong><br
/><br
/><input
type="text" name="amount" size="10" title="Other donate" value="" /><br
/><br
/><strong> Your Email Address :</strong><input
type="hidden" name="on0" value="Reference" /><br
/><br
/><input
type="text" name="os0" maxlength="60" /> <br
/><br
/> <input
type="hidden" name="no_shipping" value="2" /> <input
type="hidden" name="no_note" value="1" /> <input
type="hidden" name="mrb" value="3FWGC6LFTMTUG" /> <input
type="hidden" name="bn" value="IC_Sample" /> <input
type="hidden" name="return" value="http://matt.eifelle.com" /><input
type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form>]]></content:encoded> <wfw:commentRss>http://matt.eifelle.com/2009/05/19/interactive-raytracer/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: matt.eifelle.com @ 2010-07-30 08:16:42 by W3 Total Cache -->