cdriehuys / raytracer

A Rust implementation of "The Ray Tracer Challenge"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

raytracer-rs

Test

A ray tracer written in Rust, built by following The Ray Tracer Challenge by Jamis Buck.

Playground

At the end of each chapter, the new capabilities of the ray tracer are utilized in the main program. It can be executed with:

# Using the release profile instead of the default debug profile has a massive
# effect on performance.
cargo run --release

Current Behavior (Ch. 9)

We have our first alternate shape type: planes. Planes allow us to replace our hacky floors/walls made of deformed spheres with proper infinite, flat planes. This isn't a huge visual change, but there was a significant amount of refactoring to make it easier to share logic between shapes.

Scene with spheres and a plane

History

Expand the section below to see a progression of the project. These milestones generally correspond to the exercises at the end of each chapter in the book which utilize the new functionality added in the chapter.

Expand to see progression

Canvas Usage (Ch. 2)

This was the first visual output from the project. It exercised basic tuple math to compute the trajectory of a projectile and used the new canvas to plot the projectile's position over time.

projectile motion plot

Matrix Transforms (Ch. 4)

The next major addition was matrix transformations which allows us to use a consistent operation (matrix multiplication) to perform arbitrary transforms of objects. For example, the hour markers on a clock can be drawn by applying rotations to a single hour marker as shown in the image:

raytracer clock

Casting Rays (Ch. 5)

The next step was actually casting rays to produce the silhouette of a sphere. The sphere is a unit-sphere located at the origin. Rays are cast from a point in space towards a "wall" behind the sphere. Locations on the wall are colored red if the ray hits the sphere. This produces an image like:

Ray traced sphere silhouette

Light and Shading (Ch. 6)

Objects now have an associated material. The material utilizes ambient, diffuse, and specular reflections to produce a specific color at each position on the sphere. When rendered with a light source, the sphere looks like:

Ray traced sphere

Making a Scene (Ch. 7)

The ray tracer now has abstractions for groups of objects that make up a scene as well as an abstraction for positioning the camera. This makes it much easier to render scenes with multiple objects, like the following:

Scene with multiple objects

Shadows (Ch. 8)

Shadows have now been added. For each rendered pixel of an object, we do an additional ray cast towards the light source to determine if there is direct light cast at that particular point. If not, then the location is in shadow, and only the material's ambient reflection affects the final color.

Scene with multiple objects casting shadows

License

This project is licensed under the MIT License.

About

A Rust implementation of "The Ray Tracer Challenge"

License:MIT License


Languages

Language:Rust 100.0%