diff --git a/src/main.rs b/src/main.rs index b6420c7..c0e78b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,10 @@ mod renderer; mod scene; use crate::primitives::Vec3; -use crate::scene::Camera; +use crate::scene::{ + Camera, + Scene +}; use crate::renderer::RenderCommand; use rand::SeedableRng; @@ -24,21 +27,20 @@ fn main() { // random generator let mut small_rng = SmallRng::seed_from_u64(0); - // world - let world = scene::random_scene(&mut small_rng); - - // camera - - let cam = Camera::new( - Vec3::new(13.0, 2.0, 3.0), // lookfrom - Vec3::zero(), // lookat - Vec3::new(0.0, 1.0, 0.0), // vup - 20.0, - aspect_ratio, - 0.1, // aperture - 10.0, // dist_to_focus - ); - + // Scene (now includes camera) + let scene = Scene { + camera: Camera::new( + Vec3::new(13.0, 2.0, 3.0), // lookfrom + Vec3::zero(), // lookat + Vec3::new(0.0, 1.0, 0.0), // vup + 20.0, + aspect_ratio, + 0.1, // aperture + 10.0, // dist_to_focus + ), + world: Scene::random_world(&mut small_rng) + }; + // render // The render loop should now be a job submission mechanism // Iterate lines, submitting them as tasks to the thread. diff --git a/src/scene.rs b/src/scene.rs index b44821a..eb984a0 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -228,85 +228,92 @@ impl Camera { } } -pub fn random_scene(srng: &mut SmallRng) -> Hittable { - let mat_ground = Material::Lambertian { albedo: Vec3::new(0.5, 0.5, 0.5) }; - let mut world = Hittable::HittableList { hittables : Vec::::new() }; - - world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground }); - - let distrib_zero_one = Uniform::new(0.0, 1.0); - for a in -11..11 { - for b in -11..11 { - let choose_mat = srng.sample(distrib_zero_one); - let center = Vec3 { - x: a as f32 + 0.9 * srng.sample(distrib_zero_one), - y: 0.2, - z: b as f32 + 0.9 * srng.sample(distrib_zero_one), - }; - if (center - Vec3::new(4.0, 0.2, 0.0)).length() > 0.9 { - if choose_mat < 0.8 { - // diffuse - let albedo = Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one); - let sphere_material = Material::Lambertian { albedo }; - world.push( - Hittable::Sphere { - center, - radius: 0.2, - material: sphere_material, - } - ); - } else if choose_mat < 0.95 { - // metal - let distr_albedo = Uniform::new(0.5, 1.0); - let distr_fuzz = Uniform::new(0.0, 0.5); - - let albedo = Vec3::rand(srng, distr_albedo); - let fuzz = srng.sample(distr_fuzz); - let material = Material::Metal { albedo, fuzz }; - world.push( - Hittable::Sphere { - center, - radius: 0.2, - material: material, - } - ); - } else { - // glass - let material = Material::Dielectric { index_refraction: 1.5 }; - world.push( - Hittable::Sphere{ - center, - radius: 0.2, - material: material, - } - ); +pub struct Scene { + pub camera: Camera, + pub world: Hittable, +} +impl Scene { + pub fn random_world(srng: &mut SmallRng) -> Hittable { + let mat_ground = Material::Lambertian { albedo: Vec3::new(0.5, 0.5, 0.5) }; + let mut world = Hittable::HittableList { hittables : Vec::::new() }; + + world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground }); + + let distrib_zero_one = Uniform::new(0.0, 1.0); + for a in -11..11 { + for b in -11..11 { + let choose_mat = srng.sample(distrib_zero_one); + let center = Vec3 { + x: a as f32 + 0.9 * srng.sample(distrib_zero_one), + y: 0.2, + z: b as f32 + 0.9 * srng.sample(distrib_zero_one), }; + if (center - Vec3::new(4.0, 0.2, 0.0)).length() > 0.9 { + + if choose_mat < 0.8 { + // diffuse + let albedo = Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one); + let sphere_material = Material::Lambertian { albedo }; + world.push( + Hittable::Sphere { + center, + radius: 0.2, + material: sphere_material, + } + ); + } else if choose_mat < 0.95 { + // metal + let distr_albedo = Uniform::new(0.5, 1.0); + let distr_fuzz = Uniform::new(0.0, 0.5); + + let albedo = Vec3::rand(srng, distr_albedo); + let fuzz = srng.sample(distr_fuzz); + let material = Material::Metal { albedo, fuzz }; + world.push( + Hittable::Sphere { + center, + radius: 0.2, + material: material, + } + ); + } else { + // glass + let material = Material::Dielectric { index_refraction: 1.5 }; + world.push( + Hittable::Sphere{ + center, + radius: 0.2, + material: material, + } + ); + + }; + } } } + + let material1 = Material::Dielectric { index_refraction: 1.5 }; + world.push( Hittable::Sphere{ + center: Vec3::new(0.0, 1.0, 0.0), + radius: 1.0, + material: material1 + }); + + let material2 = Material::Lambertian { albedo: Vec3::new(0.4, 0.2, 0.1) }; + world.push( Hittable::Sphere { + center: Vec3::new(-4.0, 1.0, 0.0), + radius: 1.0, + material: material2 + }); + + let material3 = Material::Metal { albedo: Vec3::new(0.7, 0.6, 0.5), fuzz: 0.0 }; + world.push( Hittable::Sphere { + center: Vec3::new(4.0, 1.0, 0.0), + radius: 1.0, + material: material3 + }); + world } - - let material1 = Material::Dielectric { index_refraction: 1.5 }; - world.push( Hittable::Sphere{ - center: Vec3::new(0.0, 1.0, 0.0), - radius: 1.0, - material: material1 - }); - - let material2 = Material::Lambertian { albedo: Vec3::new(0.4, 0.2, 0.1) }; - world.push( Hittable::Sphere { - center: Vec3::new(-4.0, 1.0, 0.0), - radius: 1.0, - material: material2 - }); - - let material3 = Material::Metal { albedo: Vec3::new(0.7, 0.6, 0.5), fuzz: 0.0 }; - world.push( Hittable::Sphere { - center: Vec3::new(4.0, 1.0, 0.0), - radius: 1.0, - material: material3 - }); - - return world; -} +} \ No newline at end of file