The joy of building a ray tracer, for fun, in Rust

  • Was pleasantly surprised to learn that the author of this blog post was none other than the CTO of Stripe! Glad to see a CTO who's still hands-on and even willingly blogs about making so-called 'beginner' mistakes with the float/integer behaviour in Rust. Big props.

  • Tangent: Nvidia also wrote a tutorial translating the C++ from the original Ray Tracing in One Weekend to CUDA code that runs on the GPU: https://developer.nvidia.com/blog/accelerated-ray-tracing-cu...

  • > (python does the same FWIW)

    FWIW, python 3 doesn't do the same anymore:

      $ python2.7 -c "print(800 / 600)"
      1
      $ python3 -c "print(800 / 600)"
      1.3333333333333333

  • Oh, I did something like this a while back as well! Aside from ray tracing, my renderer also supports ray marching, so it can render some cool fractals[1]. Writing path tracers is so much fun, love the write-up!

    [1]: https://github.com/slightknack/keikan#readme

  • Hi folks, article author here. I just wanted to stop by to say I'm delighted so many folks enjoyed the piece. I really did have a lot of fun :-)

  • That raytracer is impressive. For a gentle introduction to raytracing, I recommend this book: https://www.amazon.com/gp/product/B07Q84TQ91/

  • This article hit home... I took a similar approach and spent bit of time over the summer to familiarize myself with the language by writing a ray tracer. (Super simple, not as fancy as the one in the article.)

    https://github.com/mschaef/rust-rt

    After spending as much time lately as I have in Scala and Python, my immediate reaction to Rust was quite positive. Python has performance issues from 1985 and the build ecosystem seems borderline chaotic. (Made me seriously miss Maven, etc.) Scala seems a lot like C++ - an amazing intellectual accomplishment, a great place to spend all of your time, but not so good as a part time language. (I spend a bunch of my time in Scala mentally expanding out shorthand notation the way I might be mentally macroexpanding in a Lisp.)

    Rust, in contrast, seems to have struck a nice balance between expressive power and runtime performance. Expressively, it has a lot of what I like about Scala with a syntax that makes more sense to my C-style upbringing. Performance seems to be everything I'd expect from the fully compiled language that it is. (In terms of performance, there's no way Python would've let me get away with some of what I got away with in my Rust ray tracer.)

    Given that the language gave such a positive initial impression, the questions I still have are more about what it feels like in the large. ie: Working with a significantly sized team jointly on a codebase that might last 1, 5, 10 or more years. (Even then, I'm pretty optimistic.)

  • Awesome! Danger here is that once you get started down this path, one becomes really tempted to build a full commercial renderer ;)

    Next step: add global illumination. Rayon is the key here, and having access to more cpu cores.

    Ray Tracing And Global Illumination (2003)

    https://digitalcommons.unf.edu/cgi/viewcontent.cgi?article=1...

  • I guess this thread is the best place to ask for this:

    Do you know of good open source real time (preferably gpu based) ray tracing engines out there? That can be used for games? I see a bunch of lists but want to get your expert opinion.

    E.g. https://awesomeopensource.com/projects/raytracing/real-time

  • Every time I read another one of these articles I have to ask myself what kind of a programmer am I if I've never written even a basic ray tracer. Seems like a fun hobby project as you can iterate for eternity. I even initially got into programming thru playing with Povray for school art projects.

  • Love me some shiny balls. Great approach to the topic, wonderful blog design as well.

  • Nice article. Not everything is an expression in rust as the author mentioned though.

  • Cool! Just to clarify the idea, it's not rendering it on screen (I was first curious how you'd do that from Rust in a simple example), but rather saves image frames.

  • The earth rendering looks funky, my guess is that there is some projection error because everything is too wide (across the equator). Nice post :)

  • Very good and encouraging intro. Does anyone know of a good raytracer using Rust that leverages GPUs on Metal or Vulkan?

  • Rayon really does feel like magic.

  • Does the 3d games today still use ray tracing or some other algorithm?

  • pub trait Hittable { fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>; }

    pub trait Scatterable { fn scatter(&self, ray: &Ray, hit_record: &HitRecord) -> Option<(Option<Ray>, Srgb)>; }

    I don't know rust but familiar with the Option type. Why would you not create a type that composes a Ray and a Srgb value, something like this?

    struct RayColour/RayWithSrgb/etc { Ray, Srgb }

    Seems like it would be a nicer API.

  • are there stats for the rendering ? that would be quite useful to compare similar implementations in other languages etc.

  • It's also a undergrad course lab here at SJTU, also in Rust: https://github.com/aik2mlj/raytracer-tutorial

  • undefined

  • undefined

  • Finally a good book recommendation about Rust. The official Rust book is poorly written and messy.