Stochastic ray tracer based on The Ray Tracer Challenge book by Jamis Buck.
It includes most of the features present in the book. The book only gives you the unit tests that your code must pass and some math formulas (apart from the theoretical explanation of how a raytracer works of course), so the actual implementation really reflects my own style of coding. It was also my choice to add some optimizations to this project, such as multi-threaded rendering.
To run this project you need to have cargo installed.
Refer to the examples directory to find examples on how to create the scenes. You can run an example with a command such as:
cargo run --release --examples checkered_walls_metallic_sphere # runs the `examples/checkered_walls_metallic_sphere.rs` file
This creates an image.png
file with the generated image.
Remember to run in RELEASE MODE.
This ray tracer uses the CPU to perform all the computations instead of the GPU, which usually would result in better performance due to the nature of how GPU cores work for number-crunching.
Complex scenes might require a lot of CPU power as the number of ray intersections increases. This can happen because of the following reasons:
-
Increased image resolution. At least one ray is cast for every pixel in the resulting image.
-
Increased number of objects in the scene. The more objects there are in a scene, the more intersections have to be checked each time a ray is casted, even if that object is nowhere near the ray. In scenes with too many objects, adding the objects to a group and then dividing that group might allow you to take advantage of bounding volumes hierarchy and speed up your rendering time by reducing the number of unnecessary checks for intersections. You can check an example of how this is done in this example.
-
Objects with reflective and/or refractive materials. When intersecting a material with these properties, multiple rays are cast recursively to determine the color at each point of intersection.
-
Using area-lights. Multiple rays are cast towards each area-light source for every point of intersection. The exact number of rays is determined by the number of cells your area-light has.
To speed up the rendering of your scenes, you can take advantage of the multi-threaded rendering capabilities of the ray tracer, which enabled to use 8 CPU threads by default. This number can be customized by settings the RENDER_THREADS
environment variable before running and setting its value to the number of desired threads. For example:
RENDER_THREADS=16 cargo run --release # uses 16 threads
A progress bar showing the current rendering progress can be toggled by passing the --progress
flag when running from the command line:
cargo run --release -- --progress