In the previous part, we covered diffuse materials and lighting for our spheres. Now we take it a step further by adding a metallic look.
Traits and Reference Counting Pointers
I’m starting to get more comfy with inheritance and shared pointers in Rust. Using a trait called Material, a struct can be created that implements the trait function scatter() to render our spheres with any surface, in the case, metal.
I prefer passing in two parameters and returning one object versus how the Ray Tracing In One Weekend code does it by passing four parameters into a function that mutates two of those parameters. See below for a comparison of the scatter function signatures.
C++ Code (Note: attenuation and scattered are mutable):
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const = 0;
My version in Rust:
fn scatter(&self, ray: Ray, rec: HitRecord) -> MaterialRecord;
Shiny Metal

I think the two outer spheres look like olives with the reflection of the brown center sphere as the pimentos.
Fuzzy Metal

This is a cool effect, but it reminds me of some strange eye test picture the optometrist might show me right before blasting a puff of air at my eyeballs.
Next Up, Glass
We’re making progress! Keep your eye out for the next post as we add a glassy look to our spheres. As always, follow along with my code and the online book, Ray Tracing in One Weekend.
Tags: coding rust software