In the last post, we added the ability to position our camera. This time we’ll add code for focus, or rather the ability to blur objects that are not in focus.
Here we go
In order to handle “depth of field”, we need to pass two more parameters into our Camera constructor:
- aperture – the size of the camera lens.
- focus_dist – the distance between the projection point and the plane of perfect focus.
We also need to add a new function to handle the actual blurring effect called random_in_unit_disk()
After adjusting a few of the camera calculations, we get the following:

The Final Step
Now that we’ve addressed depth of field, we’re done adding features to our ray tracer. In the next post, we’ll create a world with a bunch of different spheres that fully exercise the code we’ve written. Until then, take a peek at the code I’ve written and follow along with the online book, Ray Tracing in One Weekend.
Tags: coding rust software