main.rs (3066B)
1 use std::rc::Rc; 2 3 use crate::{ 4 camera::{Camera, CameraConfig}, 5 color::LinearColor, 6 hittable::{Hittable, Sphere}, 7 material::{Dielectric, Lambertian, Metal}, 8 random::RandomNumberGenerator, 9 vec3::Vec3, 10 }; 11 12 mod camera; 13 mod color; 14 mod hittable; 15 mod interval; 16 mod material; 17 mod random; 18 mod ray; 19 mod vec3; 20 21 const RNG_SEED: u64 = 42; 22 23 fn main() { 24 let material_ground = Lambertian::new(LinearColor::new(0.5, 0.5, 0.5)); 25 let material1 = Dielectric::new(1.5); 26 let material2 = Lambertian::new(LinearColor::new(0.4, 0.2, 0.1)); 27 let material3 = Metal::new(LinearColor::new(0.7, 0.6, 0.5), 0.); 28 29 let mut rng = RandomNumberGenerator::new(RNG_SEED); 30 31 let mut world: Vec<Box<dyn Hittable>> = vec![ 32 Box::new(Sphere::new( 33 Vec3::new(0., -1000., 0.), 34 1000., 35 Rc::new(material_ground), 36 )), 37 Box::new(Sphere::new(Vec3::new(0., 1., 0.), 1., Rc::new(material1))), 38 Box::new(Sphere::new(Vec3::new(-4., 1., 0.), 1., Rc::new(material2))), 39 Box::new(Sphere::new(Vec3::new(4., 1., 0.), 1., Rc::new(material3))), 40 ]; 41 42 for a in -11..11 { 43 for b in -11..11 { 44 let center = Vec3::new( 45 a as f64 + 0.9 * rng.next_f64(), 46 0.2, 47 b as f64 + 0.9 * rng.next_f64(), 48 ); 49 if (center - Vec3::new(4., 0.2, 0.)).length() > 0.9 { 50 match rng.next_f64() { 51 0.0..0.8 => { 52 let albedo = LinearColor::random(&mut rng) * LinearColor::random(&mut rng); 53 world.push(Box::new(Sphere::new( 54 center, 55 0.2, 56 Rc::new(Lambertian::new(albedo)), 57 ))); 58 } 59 0.8..0.9 => { 60 let albedo = LinearColor::random_in_range(0.5, 1., &mut rng); 61 let fuzz = rng.next_f64_in_range(0., 0.5); 62 world.push(Box::new(Sphere::new( 63 center, 64 0.2, 65 Rc::new(Metal::new(albedo, fuzz)), 66 ))); 67 } 68 _ => { 69 world.push(Box::new(Sphere::new( 70 center, 71 0.2, 72 Rc::new(Dielectric::new(1.5)), 73 ))); 74 } 75 }; 76 } 77 } 78 } 79 80 let config = CameraConfig { 81 aspect_ratio: 16. / 9., 82 image_width: 1200, 83 samples_per_pixel: 300, 84 max_bounces: 50, 85 field_of_view_degrees: 20., 86 look_from: Vec3::new(13., 2., 3.), 87 look_at: Vec3::new(0., 0., 0.), 88 view_up: Vec3::new(0., 1., 0.), 89 defocus_angle_degrees: 0.6, 90 focus_distance: 10., 91 rng, 92 }; 93 let mut camera = Camera::new(&config); 94 let image = camera.render(&world); 95 println!("{}", image.to_ppm_string()); 96 }