Brandon Eleuterio

Articles

Learning Rust – Part 12: Depth of Field

Brandon Eleuterio

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:

  1. aperture – the size of the camera lens.
  2. 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:

Depth-of-field
Depth-of-field

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:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top