#ray-tracing #random #book #scene #rgb #peter #shirley

ray_tracing_core

Ray Tracing based on Peter Shirley's mini books

2 releases

0.1.1 Nov 20, 2021
0.1.0 Apr 5, 2021

#524 in Graphics APIs


Used in 2 crates

MIT/Apache

185KB
5.5K SLoC

Crate ray_tracing_core

GitHub page rabbid76.github.io/ray-tracing-with-rust
GitHub repository Rabbid76/ray-tracing-with-rust

Based on Peter Shirley's books:

cover scene - ray tracing 3

“Note that I avoid most “modern features” of C++, but inheritance and operator overloading are too useful for ray tracers to pass on.”
Peter Shirley, Ray Tracing in One Weekend

Example

use ray_tracing_core::random;
use ray_tracing_core::test::TestSceneSimple;
use ray_tracing_core::types::ColorRGB;
use ray_tracing_core::types::FSize;

fn main() {
    let cx = 40;
    let cy = 20;
    let samples = 10;
    let scene = TestSceneSimple::new().scene;
    
    let mut pixel_data: Vec<u8> = Vec::with_capacity(cx * cy * 4);
    pixel_data.resize(cx * cy * 4, 0);
    
    for x in 0..cx {
        for y in 0..cy {
            let mut c = ColorRGB::new(0.0, 0.0, 0.0);
            for _ in 0..samples {
                let u = (x as FSize + random::generate_size()) / cx as FSize;
                let v = 1.0 - (y as FSize + random::generate_size()) / cy as FSize;
                c = c + scene.ray_trace_color(u, v);
            }
            c = c / samples as FSize;
    
            let i = (y * cx) + x;
            pixel_data[i * 4] = (c[0].sqrt() * 255.0).round() as u8;
            pixel_data[i * 4 + 1] = (c[1].sqrt() * 255.0).round() as u8;
            pixel_data[i * 4 + 2] = (c[2].sqrt() * 255.0).round() as u8;
            pixel_data[i * 4 + 3] = 255;
        }
    }
    
    // [...]
}

Dependencies

~5MB
~95K SLoC