Hacker News new | past | comments | ask | show | jobs | submit login
Deciphering the postcard-sized raytracer (fabiensanglard.net)
221 points by Impossible 8 months ago | hide | past | web | favorite | 24 comments

Funny story! I'm an undergrad Computer Science student who received this postcard raytracer from my Pixar recruiter back in November when they visited my school (CMU). To try and woo my recruiter, I changed the "PIXAR" text to say "HIRE ME" and sent it back to them. I also called fork() twice before the render loop to use 4 cores (plus a spooky mmap call to share the memory).

Here's the gist of my modified code (clang-formatted with comments): https://gist.github.com/Flafla2/d2f44c9e5e5a0ff13e6071fb782d...

And here's what it renders: https://i.imgur.com/Yhqt0Le.png

I didn't end up moving forward with my offer at Pixar, but I had a ton of fun with this! Super awesome idea, Pixar sure knows how to get computer graphics programmers thinking.

If anyone is interested in diving deeper into distance field ray marching and using it in a production game engine, check out my article on the topic here: http://flafla2.github.io/2016/10/01/raymarching.html

Totally no idea. What is the logic behind all these? Is the word 3D model?

The words are defined using Signed Distance Field functions -- a function that determines the distance to object's surface. It is quite simple to write down an SDF for primitive objects like cube and sphere, cylinder, torus, then using negation and min/max you can create unions, intersections and differences of shapes defined by SDFs.

Inigo Quilez has in-depth explanations and plenty of examples for these: https://iquilezles.org/www/articles/distfunctions/distfuncti...

Incidentally, he also works at Pixar --- and I'm not sure if he was the one who popularised using SDFs there, because I don't remember seeing this stuff before he first put up those webpages explaining SDFs.

Inigo Quilez works at Facebook (after Oculus story studio was shut down) and is the creator/primary developer of the VR art tool Quill (https://quill.fb.com). He quit Pixar in 2014.

There are no vertices in the code. Everything is done with CSG functions. If you are unfamiliar with them, let's just say that they are functions describing if a coordinate is inside or outside an object.

I believe this technique was first popularised in the demoscene, because it makes for some extremely concise procedural descriptions of 3D scenes:


The POVray ray tracer uses CSG in its scene descriptions as well. CSG is a really neat way to visualize objects mentally, too.

The other interesting raytracer is http://www.kevinbeason.com/smallpt/ which is a path tracer in 100 lines of 80 column width C.

For anyone else who read the article and is itching for more, the International Obfuscated C Code Contest[0] (IOCCC) is the place for this kind of compact, obfuscated trick code. Stuff like [1] reminds me how far I still have to go with programming.

[0]: https://www.ioccc.org/years-spoiler.html [1hint]: https://www.ioccc.org/2015/mills1/hint.html [1code]: https://www.ioccc.org/2015/mills1/prog.c

Fabrice Bellard has an entry there too!


I am not surprised what he chose to do. It may actually be the only entry that uses patented technology.[1]

[1] https://news.ycombinator.com/item?id=17587684

Oh, and of course the 2KB flight simulator: https://news.ycombinator.com/item?id=15058723

This is awesome!

There’s a bug in the deobfuscated version — sizeof(letters) should be 60 or sizeof(letters)-1 (that version reads past the end of the buffer otherwise).

This reminded me of these concise REBOL examples:


Is the new 'ray tracing' support from Nvidia ray tracing or is it path tracing?

It's hardware accelerated support for tracing rays efficiently. That can be used to implement a variety of rendering approaches that make use of tracing rays, including traditional Whitted ray tracing, path tracing and all sorts of hybrid approaches.

To be more precise: RTX (like OptiX before it) makes no particular assumptions about the applications. It concerns itself mostly with fast ray/scene intersections while user code in the form of very specialized shaders form the actual rendering algorithms around that core. That makes sense because constructing acceleration structures and the intersection tests themselves tend to dominate the runtime for these algorithms unless some particularly fancy shading is applied to the surfaces.

> main() is the one symbol that cannot be obfuscated since it is called by libc's _start function

Who needs libc ;)

> Who needs libc

This program does, for multiple things, regardless of how the entry point works.

No program needs libc. Most could use it, though.

This program uses for example printf, which is provided by libc, so it needs some form of libc to run.

It’s easy to swap out the limited use of printed in this program, which just prints strings and integers, with a short loop and a syscall. Like I said, nobody needs libc; it’s just convenient to use.

This program, as it is written, needs libc to run. It would not run without a libc.

If you wrote a different program, that didn't use libc, then it would not need libc, that is correct. But would that program fit on a post card?

That’s my argument: it probably could; I think it would be a dozen or so more characters.

Even the AMD64 instruction names alone for fabsf, sqrtf, powf, fmodf, will add up to more than a dozen characters. That's before you implement rand and printf. I'm skeptical.

Applications are open for YC Winter 2020

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact