Crate rtwlib

Source
Expand description

§rtwlib

This is a simple and raytracing library, designed for simple use and modification, based on the books“Ray Tracing in One Weekend“ and “Ray Tracing: the next week” by Peter Shirley. This isn’t optimized for performance, so I would heavily reccomend only using it in situations where computation time isn’t a concern.

§Get started

To render a scene, you need two things: a Camera and a scene, in the form of a HittableList. The following code creates a scene and camera, and renders the scene to a Vec containing the RGB values of the pixels.

use rtwlib::{camera::Camera, hittable::HittableList};

fn main() {
    let mut world = HittableList {
        objects: Vec::new(),
    };
    let mut cam = Camera::new();

    let image_bytes = cam.render_to_bytes(world, |progress| println!("Progress: {}%", progress));
    // Do something with the bytes
}a

This is the bare minimum needed to render a scene, and will result in a sky background, as no objects have been added to the scene.

§Adding objects to the scene

Any object that implements the Hittable trait can be added to the scene, the base crate only provides a Sphere object that can be used, but you can create your own objects by implementing the Hittable trait for your own objects. Objects also need to have an associated material, to inform how light bounces should be calculated which can be any object that implements the Material trait, this can be

fn main() {
    let mut world = HittableList::new();

    let material = Rc::new(Lambertian::new(Color::new(0.3, 0.86, 0.1))); //create a new material with a vaugely green color
    let sphere = Sphere::new(Point3::new(0., 0., -1.), 0.5, material); //create a new sphere at 0, 0, -1 with a radius of 0.5, using the material we just created

    world.add(sphere); //add the sphere to the world

    ...
//

Modules§

camera
This crate contains the Camera struct, and associated functions. The functions are here to provide a simple way to render a scene, but feel free to implement your own methods on the Camera struct to render to different outputs.
color
This module contans all functions and structs related to colors and color manipulation. This includes the Color struct, an alias for Vec3, and functions to convert colors to different formats, as well as color manipulation functions such as gamma correction.
hittable
Hittable is a trait that is used to define objects that can be hit by rays, and HittableList represents a list of Hittable objects, or a scene. This module contains the hittable trait, and the hittable list struct, which is a collection of hittable objects. Any hittable object must implement the Hittable trait, which requires the hit function to be implemented, which determines if a ray hits the object. The HittableList struct is a collection of hittable objects, and implements the Hittable trait itself, allowing for nested collections of objects ( I dont see why you would need that ).
material
materials is a collection of types that implement the Material trait. materials are associated with objects, and determine how it interacts with light Every material has a scatter function, which takes an input ray and a hit record, and returns a boolean indicating if the ray was scattered, and modifies the input variables to reflect the scattered ray, colors, and other properties. The available materials are:
ray
This module contains the Ray struct, which represents a ray in 3D space.
utils
A collection of miscelanious ( unused ) utility functions, might come in handy later.
vec3
Vec3 is a simple 3D vector struct, with x, y, and z components, and a bunch of utility functions. All of the vector math used in the raytracer is implemented here. Vec3 has a few aliases, such as Point3, which is used to represent a point in 3D space, and Color, which is used to represent a color, and these are exchangable. ( althoug I would reccomend using Color for colors, and Point3 for discrete positions )

Macros§

vec3
A macro for creating a new Vec3 with the given x, y, and z components.