6 ms·
Smallpt: Global Illumination in 99 lines of C++
- gigatexal 10y agoI've no words. Very cool.
- santaclaus 10y ago> 99 lines of 72-column (or less) open source C++ code > #include <math.h> > #include <stdlib.h> > #include <stdio.h> After g++ -E smallpt.cpp, how many lines is it? :0
- archgoon 10y ago3325. I'm not quite sure why you're asking. This isn't about the size of the library, it's about the lines of code the programmer needs to write to express an idea. If you are concerned about the size of the final binary, then as the author states, it's under 4k with a few optimizations.
- santaclaus 10y agoSure. With the right abstraction that will be 2 lines of code.
- Cyph0n 10y agoIf you limit it to the stdlib only, I highly doubt that's possible. I am a C++ noob, so I might be wrong.
- kr7 10y agoTwo lines, no more than 70 characters each. (Okay, it isn't just limited to the stdlib: you'll need curl and gcc too.) (Also: obviously, RUN THIS WITH CAUTION!) #include <stdlib.h> int main(){system("curl -o x -L bit.ly/2kbOUJr;g++ -xc++ -oy x;./y");}
- Cyph0n 10y agoReminds me of the xkcd-inspired[1] stacksort: https://gkoberger.github.io/stacksort/ https://gkoberger.github.io/stacksort/. But yeah, that's cheating man :P [1]: https://xkcd.com/1185/ https://xkcd.com/1185/
- Arelius 10y agoSure if your title is "Global Illumination in 2 lines of C++, with curl, and g++, and libraries hosted on bit.ly". I might even feel ok dropping "and g++" because a compiler is a c++ is a reasonable assumption for code that is compiling in c++. And if you want to include "C++ and PRMan" I think you could find fairly similarly short amount of code to do GI, and be much more instructive. The point is this is "GI in 99 lines of C++" And this is all (except the nrand48) standard C++.
- johncolanduoni 10y agoSo which part of the standard library is geared towards global illumination? I must have glossed over that one.
- santaclaus 10y agoWhere here was the standard library laid out as the axiomatic atom of line counting?
- johncolanduoni 10y agoIn the title, where they say "99 lines of C++".
- echelon 10y agoThat's kind of ridiculous. Leveraging something someone else wrote does not mean you get to disclaim its role. As an aside, this reminds me of XKCD's `import antigravity`.
- arjie 10y agoThe part where it references what someone else wrote is the 'of C++' part. The C++ language standard describes the standard library. The OpenMP enhancements aren't actually required (the code will compile as standard C++). As such, it's pretty pure C++ really and anyone who writes the language commonly will understand what is meant.
- johncolanduoni 10y agoOr anyone who knows a language with a standard library for that matter :P
- Buge 10y agoIt's the standard library. It's not a library for graphics. Might as well credit your OS as well for providing a kernel that runs the program, and your CPU manufacturer for making a CPU that executes the instructions. https://www.youtube.com/watch?v=5_vVGPy4-rc https://www.youtube.com/watch?v=5_vVGPy4-rc
- santaclaus 10y agoSure, but where does the standard state that a #include of a standard header is a more proper atom than any other #include?
- tcoff91 10y agoThe standard library is just as much a part of the language as the grammar and syntax. This absolutely counts as 99 lines of c++.
- ifree 10y agocool!
- dahart 10y agoI 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. Last time I saw the box in person, I made sure to leave some fingerprints. :P > Realistic Ray Tracing, by Peter Shirley > Almost 100% of smallpt is derived from this book. 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. "Ray Tracing in One Weekend." https://www.amazon.com/gp/product/B01B5AODD8/ https://www.amazon.com/gp/product/B01B5AODD8/ Blog post about it w/ more code & lots of resources: http://in1weekend.blogspot.com/2016/01/ray-tracing-in-one-weekend.html http://in1weekend.blogspot.com/2016/01/ray-tracing-in-one-we...
- generic_user 10y agoThanks for posting the blog. I know Shirley's books but I had not seen the blog.
- dahart 10y agoSurely. :) That blog is actually for his e-books. Here's his real blog: http://psgraphics.blogspot.com/ http://psgraphics.blogspot.com/
- melling 10y agoHere are 3 Swift ports: https://github.com/mebmichael/InOneWeekendSwift https://github.com/mebmichael/InOneWeekendSwift - Swift port https://github.com/Ceroce/SwiftRay https://github.com/Ceroce/SwiftRay - Swift port - Swift port http://mhorga.org/2016/03/21/ray-tracing-in-a-swift-playground.html http://mhorga.org/2016/03/21/ray-tracing-in-a-swift-playgrou... - Swift port I added them to my ray tracing resources: https://github.com/melling/ComputerGraphics/blob/master/ray_tracing.org https://github.com/melling/ComputerGraphics/blob/master/ray_...
- weerd 10y agoSo concise. operator% is used as cross product... I love it
- vilya 10y agoI've recently had to deal with some code that did this for work (and also used operator* for dot product) and it made the equations incredibly difficult to read. Please don't make the mistake of doing this in your own code.
- deleted 10y ago[deleted]
- CyberDildonics 10y agoUsing operator* for the dot product is straight up wrong, since multiplication between two vectors is well defined and used all the time.
- vilya 10y agoExactly. Same with %: since we're talking about a number-like object, there's a clear and expected meaning for it and that is NOT cross product.
- reikonomusha 10y agoI personally wish the code was just written cleanly and idiomatically. I wouldn't mind "300 lines of simple, idiomatic C++". What's the use of making code dense if you're going to reformat it anyway? (I say this in contrast to something which was explicitly "code golfed", which this code seems not to be.)
- chubot 10y agoI actually went through that exercise if anyone is interested. It ended up at probably 500 lines at first, and then 800 or so lines because I tried to port it to CUDA. It was mainly an exercise in learning CUDA -- i.e. how is CUDA different than C++? I got something running on my graphics card, but it didn't completely work and I moved onto other things. I haven't run this code since 2015 but I did have a lot of fun learning from it. $ wc -l smallpt.cpp 811 smallpt.cpp Another nice codebase I learned from is tinypy: http://www.tinypy.org/ http://www.tinypy.org/ It's written in a similarly condensed style. I'm inclined to agree with you at first, but honestly I find that reformatting code helps me get a feel for it! It is a mechanical exercise that reminds you of everything that's there. Going through and renaming functions and types to "your style" seems kinda frivolous but it actually helps IMO.
- strainer 10y agoI understand production and community code demands a most simple format but for me this program is just a few braces and vertical breaks short of perfection.
- aduffy 10y agoInterestingly enough, this code is replicated in Scala Native as an example for use of the language superset[1]. [1] https://github.com/scala-native/scala-native-example/blob/master/src/main/scala/smallpt.scala https://github.com/scala-native/scala-native-example/blob/ma...
- deleted 10y ago[deleted]
- user2994cb 10y agoLooks like it would make a nice glsl/WebGL fragment shader (might be even shorter as the vector operations are predefined). Can't see one listed on the page, but then it's mostly from a few years back.
- user2994cb 10y agoLooking at it a bit more - the double recursion in the radiance function for refraction would make things a bit awkward in WebGL.
- babuskov 10y agoFILE *f = fopen("image.ppm", "w"); // Write image to PPM file. Useless comment. Could've used that space for error checking.
- cloudrunner 10y agoA Python version-pySmallPT https://github.com/hanton/pySmallPT https://github.com/hanton/pySmallPT
- Aardappel 10y agoLobster version: https://github.com/aardappel/lobster/blob/master/lobster/samples/demos/smallpt.lobster https://github.com/aardappel/lobster/blob/master/lobster/sam...
- ihenriksen 10y agoVery cool with small code pieces that does a lot. I was a big fan of the Amiga Boot sector intro'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" floppy disks. Here are some examples https://www.youtube.com/watch?v=GPTkTobvsaw&list=PLDAE2D6D92098FF88 https://www.youtube.com/watch?v=GPTkTobvsaw&list=PLDAE2D6D92... . Thanks for sharing! :)
- lukaszjb 10y agoFor me it's more like 300 loc.
- deadcast 10y agoGreat work and thanks for sharing the source! I love when I can see the output from someone's program, get the source code, compile it myself and then get the same results! :)
- metaobject 10y agoI don't understand the fascination with writing/presenting the code with as few lines as possible. I'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.
- asrp 10y agoHere are slides linked from that page with a reformatted version with 202 lines: https://drive.google.com/file/d/0B8g97JkuSSBwUENiWTJXeGtTOHFmSm51UC01YWtCZw/view https://drive.google.com/file/d/0B8g97JkuSSBwUENiWTJXeGtTOHF... Here's the source from those slides (I don't know who put it there). http://codepad.org/ZWbF9yrY http://codepad.org/ZWbF9yrY I had to remove these two lines to make it compile. double M_PI = 3.14159265358979; double M_1_PI = 1.0/M_PI;
- jklehm 10y agoI had to modify this line as well: double erand48(unsigned short xsubi[3]) throw() { Link here: http://codepad.org/fpp2inpr http://codepad.org/fpp2inpr
- willholloway 10y agoThis 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's desktop chips, and overclock them when I'm running compute intensive stuff. If I want to try out something new like this and play around with it, I want it to be snappy. I am vehemently opposed to the idea that PC's are "fast enough" -- more speed, less waiting! 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. Time is the one thing you can't buy more of.
- carlsborg 10y agoLegend.