Chapter 3 of Peter Shirley's book starts us of with Rays, a simple camera and background. We have to make some additions to our vec3 class, to implement operator overloading. Rust allows for limited operator overloading, where only certain operators can be overloaded.
Moving and Copying
In chapter 3 we also implement the unit_vector function, which takes a Vec3 vector as input and finds it's unit length. Unfortunately this moves the original Vec3 vector out of scope, and it can not be used anymore. There are two ways to avoid this problem, copying or borrowing. Copying allows to perform the operator overloading and returning of values without the original pointer going out of scope. Implementing copying for Vec3 is super simple and is done by adding the line
#[derive(Copy,Clone)] where Vec3 is defined.
Another way to solve the moving problem is to not move at all, but rather use references. Thus saving on memory but making the code harder to read. Borrowing is performed by sending the address of a variable using the & symbol. See slide 31 from the 2015 Boston Rust Meetup
Now that we understand copying, moving, and borrowing we can implement the raytracer for generating our background.
The above code uses a ray defined by the function
p(t) = A + t*B where p is the 3D ray, A is its origin and B is the ray direction. The t parameter moves the point along the ray. Positive t will take you to parts in from of A, and negative t will take you to points behind A. The task of the ray tracer is to send rays through pixels and compute the color seen in the direction of those rays. The code above will traverse the screen from the lower left hand corner and use two offset vectors along the screen sides to move the ray endpoint across the screen. The
color(r: &ray::Ray) function linearly blends white and blue depending on the y coordinate and gives us the background you see below.
That's it for chapter 3! If you want to see the source code discussed in this post, go to github.