It is inspired from tinyraytracer by ssloy which is written in C++. This is a Rust version of it. It is a very good tutorial for beginners to learn ray tracing. I have tried to keep the code as close to the original as possible.
- git
- rustc (> 1.50)
- cargo (> 1.50)
If you have cargo
installed, you can compile and run it using:
git clone https://github.com/Qazalbash/tinyraytracer-Rust.git
cd tinyraytracer-Rust
cargo run --release
Otherwise, you can compile it manually using rustc
and run the executable tinyraytracer
:
git clone https://github.com/Qazalbash/tinyraytracer-Rust.git
cd tinyraytracer-Rust
rustc -C opt-level=3 -C target-cpu=native -C lto -C codegen-units=1 -C panic=abort -C debuginfo=0 --out-dir ./target --crate-name tinyraytracer src/main.rs
The code is divided into 4 files:
constants.rs
: It contains the constants used in the code.main.rs
: It contains the main function and the code for rendering the scene.math.rs
: It contains the code for vectors.primitive.rs
: It contains the code for primitives types likeSphere
andMaterial
.
In main.rs
there is a special function to print patterns on the sides. It is called pattern
. It is just for fun. By default it is set to:
#[inline]
fn pattern(a: f32, b: f32) -> math::Vec3 {
match ((a + 1000.0) as i32 + b as i32) & 1 == 1 {
true => constants::DARK_SQUARE,
false => constants::LIGHT_SQUARE,
}
}
There are many things that can be improved in this code. The first and foremost thing is to make it more optimized. I have tried to make it as optimized as possible, but there is still a lot of room for improvement. One aspect is to less use static variables and use dynamic memory allocation instead. I am also planning to make this in OpenCL and CUDA.