6 ms·
How much boilerplate code you need to write a ray-tracer?
- zwieback 5y agoNice resource but the headline really brings up the question about what language we should use to "quantify" code. I'd go with something like C or even Fortran: expressive enough to do this kind of work but low-level enough that we can easily imaging the assembly code behind it.
- kragen 5y agoThis 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.
- 5y ago
- otaviogood 5y agoThis article talks about a pretty full-featured ray tracer. If you just want to play around with some ideas and have fun, you can make different tradeoffs and still get very nice and fast renderings. Here's a ray tracer that I made that's GPU-accelerated and can make some nice looking images quickly in 1000 lines of code. https://www.shadertoy.com/view/4ddcRn https://www.shadertoy.com/view/4ddcRn The tradeoff is that it's all procedural graphics. So there are no triangle meshes and it would also be a bit tricky to implement a bidirectional ray tracer like this.
- sillysaurusx 5y agoWhoaaa. If you look from the bottom up through the glass floor, it looks crazy awesome. I think that's the most impressive little demo I've seen in ... quite some time. I can think of more impressive ones, but they're all extraordinarily complicated in comparison. How'd you do the glass if there are no meshes? I'm about to pass out, else I'd dig in. I'm especially curious how you're getting the refraction right. I forget my basic physics, but is it as simple as deflecting the ray slightly? But then how does it work on a curved surface? I guess it happens per pixel, so it shouldn't be too surprising, but... still, I was expecting a lot more aliasing than this: https://i.imgur.com/fdDVcCT.jpg https://i.imgur.com/fdDVcCT.jpg I guess I've never seen a real time raytracer that happened to trace through ripply glass, which is why it feels so neat.
- elihu 5y agoNot the person you're replying to, but in general refraction is pretty easy to do in a ray tracer. When the ray hits the object you use the normal vector, the incident ray direction, and the index of refraction of the material (or materials, if you're transitioning from one substance to another with different index of refractions) to calculate the new ray direction and continue from there. Usually there's some amount of reflection, too (the amount varying based on the angle of the incoming ray with respect to the surface), so you might spawn two rays: one continuing through the object, and the other bouncing off. What's much harder to do is simulating the light that's refracted or reflected off of objects (called "caustics"), like the light on the bottom of a swimming pool. To do that in a physically correct way generally requires falling back on some kind of global illumination technique like path tracing or photon mapping.
- choppaface 5y agoOr you can fit one on a business card: https://www.kevinbeason.com/smallpt/ https://www.kevinbeason.com/smallpt/
- EvanAnderson 5y agoAlso what I was thinking of. Fabien Sanglard did a nice article[0] about a business card raytracer Andrew Kensler made awhile back, too (HN discussion[1]). [0] https://fabiensanglard.net/rayTracing_back_of_business_card/ https://fabiensanglard.net/rayTracing_back_of_business_card/ [1] https://news.ycombinator.com/item?id=6425714 https://news.ycombinator.com/item?id=6425714
- bmitc 5y agoThat's a big business card. :)
- deleted 5y ago[deleted]
- rswier 5y agoAre there any good resources for learning how to render dichroic thin films with multiple internal reflections and refraction indexes? Basically, I would love to render something like this[1] in real time. [1] https://www.youtube.com/watch?v=BVQ34NlTi-E?mute=1 https://www.youtube.com/watch?v=BVQ34NlTi-E?mute=1
- capableweb 5y agoI'm just an amateur who played around with rendering in various ways, raytraced and rasterized. In order to get the results similar to the video (without hacks), you're gonna need caustics, refraction and reflections, all together with realistic lightning. You're not gonna be able to get that looking nice without a physically accurate render engine, which will most likely be using raytracing. And raytracing (compared to rasterization) is not gonna be able to run anywhere near real-time (more like at least seconds per frame, if you really ramp down the samples [image will be more noisy]).
- a_e_k 5y agoLook into spectral rendering. For a very simple form, you can sample a specific wavelength with each ray, trace it using wavelength-dependent refraction indices (e.g., from the Sellmeier equation) whenever you compute refraction or internal reflection. Then tint the ray by the color and visible intensity of the wavelength when averaging it into the image pixel. For a lot more detail, see e.g., "Efficient Spectral Rendering on the GPU for Predictive Rendering" (https://hal.inria.fr/hal-03331619/document https://hal.inria.fr/hal-03331619/document).
- alkonaut 5y agoA naive spectral path tracer can be less complex (code wise) than a "normal" one. You just trace each photon with its wavelength instead of tracing RGB (or some other full spectrum) per path. Since this abstraction is closer to physics, it makes a lot of code easier to reason about. Wavelength dependent refraction is straightforward, you just use the index of refraction for the photon you are tracing. I find this code base extremely easy to read despite not being a C++ (It has that Carmackian quality). Pretty easy to use to port to the language of your choice. https://github.com/TomCrypto/Lambda https://github.com/TomCrypto/Lambda To get the kind of effect you see in the youtube video you'd render diamond or something with extreme refraction. This is far from real time though of course. It will run an order of magnitude slower than anything you can do with full spectrum rays. So doing it in realtime would probably be just like subsurface scattering is done in real time: you just have to cheat. Perhaps there can be some inspiration from the real thing though.
- Const-me 5y ago> you could write your own scheduler or use a library I never wrote ray tracers, but in my experience the best way to parallelize things is usually “none of the above”. C++ runtime has a built-in scheduler, OpenMP https://www.openmp.org/ https://www.openmp.org/ If the algorithm needs to dynamically spawn tasks (graph search, flood fill or similar), Windows kernel has another one, see SubmitThreadpoolWork API. These things tend to work better than third-party libraries, and are way easier to consume than developing custom schedulers.
- chrisseaton 5y ago> C++ runtime has a built-in scheduler, OpenMP Is OpenMP part of the C++ standard these days?
- d110af5ccf 5y agoI don't think so. I believe OpenMP qualifies as a (very mature and widely supported) library as well as language extension. Assuming it meets your needs it has (AFAIK) support from all the major compilers (Intel, MS, GCC, LLVM) as well as being able to abstract over GPUs and other sorts of hardware.
- Const-me 5y agoNo, but it’s supported by all modern compilers. In practice, that’s almost as good as being a part of the language standard. The level of support varies, but OpenMP 2.0 standard is supported by all 4 of them.
- mirceal 5y agoread this book: http://raytracerchallenge.com/ http://raytracerchallenge.com/ I'm halfway through and at 1531 lines in Elixir including the tests via doctest :)
- systemBuilder 5y agoReally annoying that he never gives a single answer to the question posed in the title of the article. Such a troll for a title . .
- qwerty456127 5y agoI often feel sorry people are shy about inventing new languages and using them in production nowadays. I'd rather have a reasonably different language for every class of problems which would eliminate boilerplate code.
- stavros 5y agoSounds like it'd be great for job security, too.
- jhgb 5y ago> Now you want to actually trace something. There is at least three ways to it: > * write all intersection and BVH code by yourself; > * build and use open-source third-party library; > * use something like Intel Embree. How many more useful ways are there? Pretty much the only better thing that comes to my mind is "study something like Embree and then write a shader compiler that produces tracing code on-the-fly", and even that perhaps might not be actually better (although it's my understanding that OSL may actually be doing something like this).