5 ms·
This article is very good! It covers many beautiful techniques and has many beautiful images. However, I would quibble with the title; if you want to know how
by kragen 5y ago
This article is very good! It covers many beautiful techniques and has many beautiful images. However, I would quibble with the title; if you want to know how much boilerplate code you need to write a ray-tracer, you will not find the answer in it.
I wrote the following programs in an attempt to get a better answer to that question.
http://canonical.org/~kragen/sw/aspmisc/my-very-first-raytracer.html http://canonical.org/~kragen/sw/aspmisc/my-very-first-raytra... four pages of C
http://canonical.org/~kragen/sw/dev3/raytracer1k.clj http://canonical.org/~kragen/sw/dev3/raytracer1k.clj 1K of Clojure, condensed down from
http://canonical.org/~kragen/sw/dev3/circle.clj http://canonical.org/~kragen/sw/dev3/circle.clj a page of unfactored but not obfuscated Clojure, translated into
http://canonical.org/~kragen/sw/dev3/circle.js http://canonical.org/~kragen/sw/dev3/circle.js a page of JS in node.js
https://gitlab.com/kragen/bubbleos/blob/master/yeso/sdf.lua https://gitlab.com/kragen/bubbleos/blob/master/yeso/sdf.lua a page of Lua using signed distance function raymarching that runs in real time at a lousy framerate; to run it, git clone the repo, install the prerequisites listed in the README, and run `make` in the yeso directory, before running sdf.lua.
But none of these can load triangle meshes, render caustics, render area light sources, average multiple samples, do bidirectional rendering, etc.
Reznik says, "A lot of projects using something like PPM file format, which is very simple, but in the end, you need to write ... extra code for this simplicity (like tone mapping, converting from floating point values to uint8, etc.)," but the amount of extra code required is really very minimal:
/* PPM P6 file format; see <http://netpbm.sourceforge.net/doc/ppm.html> */
static void
output_header(int ww, int hh)
{ printf("P6\n%d %d\n255\n", ww, hh); }
static unsigned char
byte(double dd) { return dd > 1 ? 255 : dd < 0 ? 0 : dd * 255 + 0.5; }
static void
encode_color(color co)
{ putchar(byte(co.x)); putchar(byte(co.y)); putchar(byte(co.z)); }
But on some platforms you don't need even that much; my Clojure raytracer[s] just use[s] javax.imageio.ImageIO/write to encode a JPEG. (Admittedly, that one in particular cheats pretty shamelessly on tone mapping!)
- meetups323 5y agoYou may be happy to learn that in modern Node, the 3200x2400 example which used to take 6.8 seconds now runs in 0.25 seconds. I unfortunately don't have a clojure setupt to compare against.
- kragen 5y agoThat's astounding! Maybe V8 is catching up to LuaJIT's performance?
- capableweb 5y agoFor me (on AMD Ryzen 9 5950X, Arch Linux, Clojure 1.10.3 [OpenJDK 17.0.1] and NodeJS 17.4.0), circle.js takes ~0.15 seconds and the circle.clj takes ~0.19 seconds. After running circle.clj 100 times in a running JVM though, it takes the time down to ~0.092 seconds for each run.
- meetups323 5y agoWell of course I now have to ask how long it takes in node after running the script a hundred times
- capableweb 5y agoDoes NodeJS do any on-the-fly optimization like the JVM does?
- meetups323 5y agoYes, if you dig around V8 performance profiles you'll see some massive "Optimizing Code" blocks that appear at the bottom of a frequently-hit routine once, then subsequent calls to that same routine will run faster. That being said, while I haven't investigated this super thoroughly, in my experience the delta is usually small.
- meetups323 5y agoTrying again with timers internal to the code (previously used `time node ray.js`), initial go is 190ms, then 170ms, 160ms, and after that hovers around 140-150ms for the remaining 97 runs. If I remove the FS operations it's instead 160ms initially, then drops to 130ms by the third go and hovers around there.
- foxfluff 5y ago7 lines of K: http://www.nsl.com/k/ray/raya.k http://www.nsl.com/k/ray/raya.k
- kragen 5y agoNice! APL-family languages are really great at this kind of thing; I should see how small I can squinch a raytracer with just NumPy. Stevan Apter's programs (like that one) constantly make me wish I knew K: even if I don't want my code to look like that, it seems like it would be great to be able to type it in that way. That also leads to a one-page OCaml version and a two-page C++ version by Jon D. Harrop (the Flying Frog guy), both also using PPM output and rendering only spheres, much like most of my examples: https://web.archive.org/web/20070605173709/http://www.ffconsultancy.com/languages/ray_tracer/comparison.html https://web.archive.org/web/20070605173709/http://www.ffcons... He also wrote an SML version https://web.archive.org/web/20070522030632/http://www.ffconsultancy.com/languages/ray_tracer/comparison_cpp_vs_sml.html https://web.archive.org/web/20070522030632/http://www.ffcons... and a Java version, and others ported it to Scheme and Common Lisp https://web.archive.org/web/20070515185350/http://www.ffconsultancy.com/languages/ray_tracer/benchmark.html https://web.archive.org/web/20070515185350/http://www.ffcons..., those these seem to be lost now. Still others ported it to Io http://mike-austin.com/io/ray.io http://mike-austin.com/io/ray.io and Factor https://github.com/factor/factor/blob/master/extra/benchmark/raytracer/raytracer.factor https://github.com/factor/factor/blob/master/extra/benchmark... (3 pages). In https://www.pouet.net/prod.php?which=83222 https://www.pouet.net/prod.php?which=83222 Holtsetio wrote a raytracer in two pages of MySQL SQL, but it's sort of obfuscated. It emits the output in BMP format (almost as simple as PPM) and uses lots of imperative MySQL extensions to SQL, and it ended up as 10 KiB instead of the 1 KiB of my Clojure version. But it handles triangles, not just spheres, so it can render the Stanford bunny. https://github.com/chunky/sqlraytracer/blob/master/raytracer.sql https://github.com/chunky/sqlraytracer/blob/master/raytracer... is instead about three pages of SQL, using recursive CTEs instead of imperative assignments, and supporting only spheres but with different materials, like my C version. He's using recursive CTEs as implemented in Postgres: compliant with ANSI SQL but not implemented in most other SQL engines. In particular, he started out with SQLite but it wasn't strong enough.
- 5y ago
- dekhn 5y agoThe underlying algorithms of ray tracing, ignoring the details of the graphics system and IO, really are very simple (Snell's law). The 1K of clojure example is pretty much that. All the rest is pretty uninteresting, in my opinion- the various tricks to make better rendering in shorter times is mostly grad-level math and some computer engineering, while the asset loading is a long solved problem with ultra-mature libraries.
- kragen 5y agoNone of my examples actually implement Snell's law, just diffuse and sometimes specular reflection, but I agree that adding Snell's law (and thus refraction) does not add much complexity. Recently it's become fashionable to use the Fresnel equations and "more filmic" tone mapping to get less plasticky images, too. I think my interests are probably pretty different from yours, because I find the details of the graphics system and I/O pretty interesting, in particular how they can be improved. I also think the various tricks for better rendering in shorter time are super interesting, and even the question of how to do liability loading has some very interesting new answers, like FlatBuffers. Probably the liability-loading strategies that we came up with in the previous millennium for spinning rust that transferred 40 megabytes per second after an 8 000 000 nanosecond seek time are not optimal for SSDs that transfer 4000 megabytes per second with a 1000 nanosecond "seek time". Another thing worth mentioning is the question of how you model the 3-D shapes you want to render in the first place, which includes considerations of HCI, sometimes physics simulation, and so on. One of the most interesting things about SDFs is the tempting possibility of an efficiently renderable representation that naturally supports operations like CSG and filleting (though not usually both at once!) That is to say, I think the stuff these minimal raytracers leave out is actually very interesting indeed. But I also think it's very interesting to see what's left over when you do leave it out: a relatively small amount of math and code that can still produce visually arresting images.
- dekhn 5y agoI'm confused- don't all ray tracers use snell's law or is there a different equation for reflection (https://en.wikipedia.org/wiki/Specular_reflection https://en.wikipedia.org/wiki/Specular_reflection)? Nearly everybody I've worked with who's implemented a ray tracer says there's only one thing that matters, and that's implementing the reflection equation (I mean for first order ray tracing, not interesting stuff). As for asset loading, like I said, I think it's a solved problem. When I start Cyperpunk 2077 on my machine (fresh, no cache), it takes less than 30 seconds to get to a playable state and my SSD is basically streaming data into RAM as fast as it can.
- a_e_k 5y ago> like tone mapping You can can even add a fairly basic kind of Reinhard tone mapping, plus a 2.2 gamma and output dithering if you change the second function to: static unsigned char byte(double dd) { return pow(dd / (1.0 + dd), 1.0/2.2) * 255.0 + drand48(); } Having spent over a decade working on one of the major commercial renderers, I'll say that there can be a lot of complexity involved in a production renderer, but you can still get pretty far with a surprisingly small amount of code.
- kragen 5y agoThis is awesome! I should have put something like that in there, but I wouldn't even have thought of the dithering, and since I don't know much about graphics I didn't know about Reinhard tone mapping. There's an interesting thing that happens where sometimes if you constrain yourself to 1% or 0.1% of the code you would normally use, you end up getting interesting uncontrolled effects. cf. http://canonical.org/~kragen/bytebeat http://canonical.org/~kragen/bytebeat
- pjmlp 5y agoOn the PPM side, a very easy alternative is to use stb_image (even though I abhor header only libs), or OS specific ones like GDI+ / CoreImage, if using something like C++. On .NET side, I used ImageSharp. Thanks for sharing the Clojure version of a RayTracer.
- kragen 5y agoI'm glad you enjoyed it!