In Part 6, we drew two spheres on the screen, but the edges of the images were rough and jagged. In this part, we’ll smooth out the edges with some antialiasing.
Zoom In
It might be hard to notice the jagged edges from the image we rendered in Part 6, so let’s zoom in and take a look:

As you can see, the image on right looks more blury, but also has smoother edges.
Zoom Out
If we zoom out from the image after applying antialiasing, we see this:

Slower
The two main coding accomplishments in this chapter were:
- Abstract some code out of main.rs into a Camera struct.
- Add a third nested loop to our main rendering loop to apply the antialiasing effect.
I noticed the program execution takes a bit longer now – 18 seconds vs 1 second. I’m not sure if this a bug on my part or how antialising is supposed to work. From what I can tell, this is a result of using a sample size of 100. For each pixel, I’m drawing an additional 100 rays and averaging the color. If I reduce the sample size to 10, the execution time drops to 3 seconds. At some point I’ll go back and see if there’s a more optimal sample size or a more efficient way to handle antialiasing. For now, I’ll leave things as-is and move on.
A Little More Realistic
Up next, we’ll start making our spheres look a little more realistic by applying a matte material to their surfaces. Until then, follow the code on GitHub and check out the online book I’m using for this project.
Tags: coding rust software