license Crates.io Build Status

A Rust implementation of a small ray/pathtracer.

Inspired by Kevin Beason's educational 99-line raytracer/pathtracer.

alt text

Supports: - Ray-to-Sphere - Ray-to-Plane - Ray-to-Rectangle - Ray-to-Triangle (slow, no acceleration yet. Soon)

Usage

```toml

Cargo.toml

[dependencies] smallpt = "0.2.0" ```

Example

```rust extern crate smallpt; use smallpt::*;

let mut scene = Scene::init();

// Spheres // Mirror scene.add(Box::new(Sphere::new( 16.5, Vec3::new(27.0, 16.5, 47.0), Material::new(Vec3::zero(), Vec3::new(1.0, 1.0, 1.0), BSDF::Mirror), )));

// Glass scene.add(Box::new(Sphere::new( 16.5, Vec3::new(73.0, 16.5, 78.0), Material::new(Vec3::zero(), Vec3::new(1.0, 1.0, 1.0), BSDF::Glass), )));

// Planes // Bottom scene.add(Box::new(Plane::new( Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), Material::new(Vec3::zero(), Vec3::new(0.75, 0.75, 0.75), BSDF::Diffuse), )));

// Left scene.add(Box::new(Plane::new( Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Material::new(Vec3::zero(), Vec3::new(0.75, 0.25, 0.25), BSDF::Diffuse), )));

// Right scene.add(Box::new(Plane::new( Vec3::new(99.0, 0.0, 0.0), Vec3::new(-1.0, 0.0, 0.0), Material::new(Vec3::zero(), Vec3::new(0.25, 0.25, 0.75), BSDF::Diffuse), )));

// Front scene.add(Box::new(Plane::new( Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), Material::new(Vec3::zero(), Vec3::new(0.75, 0.75, 0.75), BSDF::Diffuse), )));

// Back scene.add(Box::new(Plane::new( Vec3::new(0.0, 0.0, 170.0), Vec3::new(0.0, 0.0, -1.0), Material::new(Vec3::zero(), Vec3::zero(), BSDF::Diffuse), )));

// Top scene.add(Box::new(Plane::new( Vec3::new(0.0, 81.6, 0.0), Vec3::new(0.0, -1.0, 0.0), Material::new(Vec3::zero(), Vec3::new(0.75, 0.75, 0.75), BSDF::Diffuse), )));

// Light (emissive rectangle) scene.add(Box::new(Rectangle::new( Vec3::new(50.0, 81.5, 50.0), Vec3::new(0.0, -1.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 33.0, 33.0, Material::new(Vec3::new(12.0, 12.0, 12.0), Vec3::zero(), BSDF::Diffuse), )));

// Light (emissive rectangle) scene.add(Box::new(Rectangle::new( Vec3::new(50.0, 81.5, 50.0), Vec3::new(0.0, -1.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 1.0), 33.0, 33.0, Material::new(Vec3::new(12.0, 12.0, 12.0), Vec3::zero(), BSDF::Diffuse), )));

let aperture = 0.5135; let cameraorigin = Vec3::new(50.0, 50.0, 300.0); let cameradirection = Vec3::new(0.0, -0.05, -1.0).normalize(); let cameraright = Vec3::new(width as f32 * aperture / height as f32, 0.0, 0.0); let cameraup = cameraright.cross(cameradirection).normalize() * aperture;

let camera = Camera::new(cameraorigin, cameradirection, cameraright, cameraup);

// Render let numsamples = 16; let width = 512; let height = 512; let mut numrays = 0; let mut backbuffer = vec![Vec3::zero(); width * height];
trace(&scene, &camera, width, height, numsamples, &mut backbuffer, &mut numrays); ```

Status

Code is still quite in flux, being refined on a weekly basis. More simplification and changes coming soon.