From 19107f20c9dfad2c015fcc2e7c9a881a09453610 Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Thu, 7 Jul 2022 13:06:46 +0200 Subject: [PATCH] Allow scenes to specify their camera --- src/main.rs | 91 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index ebe59b9..f6ce11a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,14 @@ mod perlin; mod noise; mod image_texture; -fn earth() -> HittableList { +// Image +const ASPECT_RATIO: f64 = 3.0 / 2.0; +const IMAGE_WIDTH: usize = 400; +const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize; +const SAMPLES_PER_PIXEL: i32 = 100; +const MAX_DEPTH: i32 = 50; + +fn earth() -> (HittableList, Camera) { let mut world:HittableList = Vec::new(); let earth_texture = ImageTexture::new("textures/earthmap.jpg"); let earth_material = Arc::new( @@ -40,10 +47,26 @@ fn earth() -> HittableList { radius: 2.0, material: earth_material })); - world + + let look_from = Point3::new(13.0, 2.0, 3.0); + let look_at = Point3::new(0.0, 0.0, 0.0); + let focus_dist = 2.0; + + let cam = Camera::new( + look_from, + look_at, + Vec3::new(0.0, 1.0, 0.0), + ASPECT_RATIO, + 20.0, + 0.0, + focus_dist, + 0.0, + 1.0); + + (world, cam) } -fn two_spheres() -> HittableList { +fn two_spheres() -> (HittableList, Camera) { let mut world:HittableList = Vec::new(); let checker = CheckerTexture::colored( Color::new(0.2, 0.3, 0.1), @@ -59,9 +82,25 @@ fn two_spheres() -> HittableList { radius: 10.0, material: checker_material })); - world + + let look_from = Point3::new(13.0, 2.0, 3.0); + let look_at = Point3::new(0.0, 0.0, 0.0); + let focus_dist = 2.0; + + let cam = Camera::new( + look_from, + look_at, + Vec3::new(0.0, 1.0, 0.0), + ASPECT_RATIO, + 20.0, + 0.0, + focus_dist, + 0.0, + 1.0); + + (world, cam) } -fn two_perlin_spheres() -> HittableList { +fn two_perlin_spheres() -> (HittableList, Camera) { let mut world:HittableList = Vec::new(); let noise = NoiseTexture { noise: Perlin::new(), scale: 4.0 }; let noise_material = Arc::new(Material::Lambertian(Lambertian::textured(Arc::new(noise)))); @@ -75,10 +114,26 @@ fn two_perlin_spheres() -> HittableList { radius: 2.0, material: noise_material })); - world + + let look_from = Point3::new(13.0, 2.0, 3.0); + let look_at = Point3::new(0.0, 0.0, 0.0); + let focus_dist = 2.0; + + let cam = Camera::new( + look_from, + look_at, + Vec3::new(0.0, 1.0, 0.0), + ASPECT_RATIO, + 20.0, + 0.0, + focus_dist, + 0.0, + 1.0); + + (world, cam) } -fn random_scene() -> HittableList { +fn random_scene() -> (HittableList, Camera) { let mut world: HittableList = Vec::new(); let checker = CheckerTexture::colored( @@ -155,22 +210,10 @@ fn random_scene() -> HittableList { material: material3 })); - world -} - -fn main() { - // Image - const ASPECT_RATIO: f64 = 3.0 / 2.0; - const IMAGE_WIDTH: usize = 400; - const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize; - const SAMPLES_PER_PIXEL: i32 = 1; - const MAX_DEPTH: i32 = 50; - let look_from = Point3::new(13.0, 2.0, 3.0); let look_at = Point3::new(0.0, 0.0, 0.0); let focus_dist = 2.0; - // Camera let cam = Camera::new( look_from, look_at, @@ -179,12 +222,16 @@ fn main() { 20.0, 0.0, focus_dist, - 0.0, - 1.0); + 0.0, + 1.0); + (world, cam) +} + +fn main() { // World let scene: u8 = 2; - let world = match scene { + let (world, cam) = match scene { 0 => two_spheres(), 1 => two_perlin_spheres(), 2 => earth(),