It's time for my yearly ray tracer implementation, and this time, we're going with Rust!
I have this rather weird habit of implementing anything I've learned previously multiple times in spaced intervals of time. In this case, it's my tiny little ray tracer, called tracy (omg, so cute, right?).
I've implemented tracy in multiple languages ever since I've come across the book Ray Tracing in One Weekend. C, C++, Python, JS, etc... I've lost count. It must be the simple wording and excellent explanations of the concept in the book which made it so easy to follow yet so interesting to tinker with. This time, I'm planning to rewrite this project, in crab language, i.e, Rust.
I've been using Rust for the past couple of months and I enjoy writing programs on it quite a lot. My 6502 emulator went suprisingly great, hence I'm planning to continue with the language (I haven't touched lifetimes yet, but nevertheless!).
Even more, in this implementation, I'm planning to take it a bit further: Better lighting, Optimization, BVHs, Animations, Progressive Rendering, and many more (given that I don't lose interest after a few days). There's something I'm not mentioning here, but I want to see if I can make it and only then I'll mention it (hehe).
Quite a verbose intro, I understand. But I want to state where I'm going with this blog: This is not a guide to writing your own ray tracer, but a list of things I came across and found interesting, so I can review this the next time I write another ray tracer in another language (I'll eventually hand down this tradition to my kids).
Here we go.
A good vec3 class is worth a thousand component-wise operations scattered across the codebase
This is something the book gets done with early on. I was first very hesitant to implement it again, but I finally took the time to do it and oh my god, it was totally worth it. I don't want to deal with working on vectors component-wise mid a complex algorithm, and the abstraction is totally worth it.
Vector subtraction is super cool
Let's say we have two vectors, A and B. if we want a vector that points from A to B, then we do B - A. The resulting vector, when origin tail set to at head of vector A, points to vector B. The geometrical interpretation is very important to understand why the author chose to do certain things in code. Having vectors when working with setting up viewport and sending rays made it very easy for me to visualise a ray going from camera center to the scene through a pixel in viewport.
This is the part where I got wine drunk and forgot that I was writing. Nice.
Spheres are great, but add planes are simple
The book only implements ray-sphere intersection, but ray-plane intersections are simpler and gives you the dopamine boost from being able to implement something on your own. It won't take much time once the hittable and hitrecord abstraction is in place.
Visualising normals are a great way to debug inconsistencies in code
Using the normal coordinates as color for objects in scene, or similar visual debugging methods are a great way to make sure our calculations are right. This helped me tremendously when I was implementing it in C++ for the first time.
Front faces versus Back faces
The book makes a design decision whether normals should always point out or not. This is important for things like internal reflection. If a ray where to hit on the surface and reflect off, in case of a sphere, the dot product of the direction of ray an d the outward normal will be less than zero. If the outward normal and ray are in the same direction, that means the ray hit the back face. If we were to only store the normal, we would've lost the information on whether they ray hit back or front face. It's a good practice to store a boolean field called "front face", which stores whether it was front or back. then we can store the normal to be against the direction of the ray. Read this section a few times as this is very useful when it comes to understanding materials.
HittableList is just another Hittable
I found this particularly beautiful. HittableList is just another Hittable, which outputs a HitRecord with necessary information to paint a ray.
NOT FINISHED YET! I'M JUST BUSY WITH MY STUPID LIFE.