TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Smallpt: Global Illumination in 99 lines of C++

238 pointsby Ivoahover 8 years ago

16 comments

dahartover 8 years ago
I like the walls made of spheres! The real Cornell box is about 2 feet cubed IIRC... that would make the units of this scene file something close to centimeters... which would mean the walls are spheres with a diameter of about 1.2 miles.<p>Last time I saw the box in person, I made sure to leave some fingerprints. :P<p>&gt; Realistic Ray Tracing, by Peter Shirley &gt; Almost 100% of smallpt is derived from this book.<p>FWIW, Peter Shirley distilled all the fun parts of Realistic Ray Tracing into an easy quick e-book available for $3, or free with the trial. &quot;Ray Tracing in One Weekend.&quot;<p><a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;B01B5AODD8&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;B01B5AODD8&#x2F;</a><p>Blog post about it w&#x2F; more code &amp; lots of resources: <a href="http:&#x2F;&#x2F;in1weekend.blogspot.com&#x2F;2016&#x2F;01&#x2F;ray-tracing-in-one-weekend.html" rel="nofollow">http:&#x2F;&#x2F;in1weekend.blogspot.com&#x2F;2016&#x2F;01&#x2F;ray-tracing-in-one-we...</a>
评论 #13566806 未加载
评论 #13565807 未加载
metaobjectover 8 years ago
I don&#x27;t understand the fascination with writing&#x2F;presenting the code with as few lines as possible. I&#x27;d be just as impressed if they said the code was 120 lines and formatted so it was just a bit more pleasant to read.
评论 #13567785 未加载
aduffyover 8 years ago
Interestingly enough, this code is replicated in Scala Native as an example for use of the language superset[1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;scala-native&#x2F;scala-native-example&#x2F;blob&#x2F;master&#x2F;src&#x2F;main&#x2F;scala&#x2F;smallpt.scala" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;scala-native&#x2F;scala-native-example&#x2F;blob&#x2F;ma...</a>
babuskovover 8 years ago
<p><pre><code> FILE *f = fopen(&quot;image.ppm&quot;, &quot;w&quot;); &#x2F;&#x2F; Write image to PPM file. </code></pre> Useless comment. Could&#x27;ve used that space for error checking.
weerdover 8 years ago
So concise. operator% is used as cross product... I love it
评论 #13566270 未加载
willhollowayover 8 years ago
This a good example of the kind of computational tasks that have me staying on the top of the line of the latest generation of intel&#x27;s desktop chips, and overclock them when I&#x27;m running compute intensive stuff.<p>If I want to try out something new like this and play around with it, I want it to be snappy.<p>I am vehemently opposed to the idea that PC&#x27;s are &quot;fast enough&quot; -- more speed, less waiting!<p>The only compromise I make is avoiding the extreme versions of their chips, as they can almost $1,000 more. But in the future I might change my mind on that.<p>Time is the one thing you can&#x27;t buy more of.
ihenriksenover 8 years ago
Very cool with small code pieces that does a lot. I was a big fan of the Amiga Boot sector intro&#x27;s back in the 80-90s where the programmers had to make entertaining pieces of digital art where compiled code, music, and graphics all had to fit inside 1 KB boot sector of a 3.5&quot; floppy disks. Here are some examples <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=GPTkTobvsaw&amp;list=PLDAE2D6D92098FF88" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=GPTkTobvsaw&amp;list=PLDAE2D6D92...</a> . Thanks for sharing! :)
reikonomushaover 8 years ago
I personally wish the code was just written cleanly and idiomatically. I wouldn&#x27;t mind &quot;300 lines of simple, idiomatic C++&quot;. What&#x27;s the use of making code dense if you&#x27;re going to reformat it anyway?<p>(I say this in contrast to something which was explicitly &quot;code golfed&quot;, which this code seems not to be.)
评论 #13565804 未加载
评论 #13566094 未加载
评论 #13566552 未加载
评论 #13566555 未加载
cloudrunnerover 8 years ago
A Python version-pySmallPT <a href="https:&#x2F;&#x2F;github.com&#x2F;hanton&#x2F;pySmallPT" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hanton&#x2F;pySmallPT</a>
评论 #13573502 未加载
user2994cbover 8 years ago
Looks like it would make a nice glsl&#x2F;WebGL fragment shader (might be even shorter as the vector operations are predefined). Can&#x27;t see one listed on the page, but then it&#x27;s mostly from a few years back.
评论 #13566644 未加载
deadcastover 8 years ago
Great work and thanks for sharing the source! I love when I can see the output from someone&#x27;s program, get the source code, compile it myself and then get the same results! :)
lukaszjbover 8 years ago
For me it&#x27;s more like 300 loc.
carlsborgover 8 years ago
Legend.
ifreeover 8 years ago
cool!
gigatexalover 8 years ago
I&#x27;ve no words. Very cool.
santaclausover 8 years ago
&gt; 99 lines of 72-column (or less) open source C++ code<p>&gt; #include &lt;math.h&gt;<p>&gt; #include &lt;stdlib.h&gt;<p>&gt; #include &lt;stdio.h&gt;<p>After g++ -E smallpt.cpp, how many lines is it? :0
评论 #13565731 未加载
评论 #13565964 未加载