solstrale/
lib.rs

1#![warn(missing_docs)]
2//! A multithreaded Monte Carlo path tracing library, that as such has features like:
3//! * Global illumination
4//! * Caustics
5//! * Reflection
6//! * Refraction
7//! * Soft shadows
8//!
9//! Additionally, the library has:
10//! * Loading of obj models with included materials
11//! * Multithreaded Bvh creation to greatly speed up rendering
12//! * Post-processing of rendered images by:
13//!   * [Open Image Denoise](https://www.openimagedenoise.org/)
14//!   * Bloom filter
15//! * Bump mapping
16//! * Light attenuation
17//!
18//! ## Example:
19//! ```rust
20//! # use std::sync::mpsc::channel;
21//! # use std::thread;
22//! # use image::RgbImage;
23//! # use solstrale::camera::CameraConfig;
24//! # use solstrale::geo::vec3::Vec3;
25//! # use solstrale::hittable::{Bvh, Sphere, Hittable};
26//! # use solstrale::material::{DiffuseLight, Lambertian};
27//! # use solstrale::material::texture::SolidColor;
28//! # use solstrale::ray_trace;
29//! # use solstrale::renderer::{RenderConfig, Scene};
30//! # use solstrale::renderer::shader::PathTracingShader;
31//! let camera = CameraConfig {
32//!     vertical_fov_degrees: 20.,
33//!     aperture_size: 0.1,
34//!     look_from: Vec3::new(0., 0., 4.),
35//!     look_at: Vec3::new(0., 0., 0.),
36//!     up: Vec3::new(0., 1., 0.),
37//! };
38//! let mut world = Vec::new();
39//! let yellow = Lambertian::new(SolidColor::new(1., 1., 0.), None);
40//! let light = DiffuseLight::new(10., 10., 10., None);
41//! world.push(Sphere::new(Vec3::new(0., 0., 0.), 0.5, yellow));
42//!
43//! let scene = Scene {
44//!     world: Bvh::new(world),
45//!     camera,
46//!     background_color: Vec3::new(0.2, 0.3, 0.5),
47//!     render_config: RenderConfig::default(),
48//! };
49//!
50//! let (output_sender, output_receiver) = channel();
51//! let (_, abort_receiver) = channel();
52//!
53//! thread::spawn(move || {
54//!     ray_trace(scene, &output_sender, &abort_receiver).unwrap();
55//! });
56//!
57//! for render_output in output_receiver {
58//!     let _image = render_output.render_image;
59//! }
60//! ```
61//!
62//! ## Example output
63//! ![bedroom2](https://github.com/DanielPettersson/solstrale-rust/assets/3603911/a78e4a85-2acb-409f-b7f4-4f6c5afb797e)
64//! ![conference](https://github.com/DanielPettersson/solstrale-rust/assets/3603911/8c88c777-0b85-4854-bd14-10a999bb3f78)
65//! ![happy](https://github.com/DanielPettersson/solstrale-rust/assets/3603911/c5357792-a3dc-42f9-8230-320140f9c30e)
66//! ![sponza-bump2](https://github.com/DanielPettersson/solstrale-rust/assets/3603911/0ab79ed9-cddf-46b1-84e7-03cef35f5600)
67
68//! ## Credits
69//! The ray tracing is inspired by the excellent [Ray Tracing in One Weekend Book Series](https://github.com/RayTracing/raytracing.github.io) by Peter Shirley
70
71use crate::renderer::{RenderProgress, Renderer, Scene};
72use std::error::Error;
73use std::sync::mpsc::{Receiver, Sender};
74
75pub mod camera;
76pub mod geo;
77pub mod hittable;
78pub mod loader;
79pub mod material;
80pub mod pdf;
81pub mod post;
82pub mod random;
83pub mod renderer;
84pub mod util;
85
86/// Executes the ray tracing with the given [`Scene`] and reports [`RenderProgress`] on
87/// the output [`Sender`]. Listens to abort [`Receiver`] for aborting a started ray trace operation
88///
89/// # Arguments
90/// * `scene` - A scene describing how, and what should be rendered
91/// * `output` - Channel where render progress will be sent
92/// * `abort` - Channel to send abort signals to the renderer
93pub fn ray_trace<'a>(
94    scene: Scene,
95    output: &'a Sender<RenderProgress>,
96    abort: &'a Receiver<bool>,
97) -> Result<(), Box<dyn Error>> {
98    Renderer::new(scene)?.render(output, abort)
99}