From b0efc9a248e8a6aaf5966dfb2771d830b6e95f17 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 6 Jun 2023 18:17:39 -0500 Subject: [PATCH] Feat: Wide-angle camera --- src/camera.rs | 9 ++++++--- src/main.rs | 55 +++++++++++++-------------------------------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 4804641..376e432 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -12,6 +12,7 @@ use crate::vec3::Vec3; use crate::ray::Ray; +use crate::degrees_to_radians; pub struct Camera { pub origin: Vec3, @@ -21,10 +22,12 @@ pub struct Camera { } impl Camera { - pub fn new() -> Camera { - let aspect_ratio = 16.0 / 9.0; - let vp_height = 2.0; + pub fn new(vfov: f32, aspect_ratio: f32) -> Camera { + let theta = degrees_to_radians(vfov); + let h = (theta / 2.0).tan(); + let vp_height = 2.0 * h; let vp_width = aspect_ratio * vp_height; + let focal_length = 1.0; let horiz = Vec3::new(vp_width, 0.0, 0.0); diff --git a/src/main.rs b/src/main.rs index 4fee26d..2b8db30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,63 +33,36 @@ fn main() { // world - let mat_ground = Material::Lambertian{ albedo: Vec3::new(0.8, 0.8, 0.0) }; - let mat_center = Material::Lambertian{ albedo: Vec3::new(0.1, 0.2, 0.5) }; - let mat_left = Material::Dielectric { index_refraction: 1.5 }; - let mat_right = Material::Metal{ albedo: Vec3::new(0.8, 0.6, 0.2), fuzz: 0.0 }; + let R = std::f32::consts::FRAC_PI_4.cos(); + + let mat_left = Material::Lambertian { albedo: Vec3::new(0.0, 0.0, 1.0) }; + let mat_right = Material::Lambertian { albedo: Vec3::new(1.0, 0.0, 0.0) }; let mut world = HittableList::new(); world.add( Box::new( - Sphere{ - center: Vec3{ x: 0.0, y: -100.5, z: -1.0 }, - radius: 100.0, - material: Some(mat_ground), + Sphere { + center: Vec3::new( -R, 0.0, -1.0), + radius: R, + material: Some(mat_left), } ) ); world.add( Box::new( - Sphere{ - center: Vec3{ x: 0.0, y: 0.0, z: -1.0}, - radius: 0.5, - material: Some(mat_center), - } - ) - ); - - world.add( - Box::new( - Sphere{ - center: Vec3::new(-1.0, 0.0, -1.0), - radius: 0.5, - material: Some(mat_left), } - ) - ); - - world.add( - Box::new( - Sphere{ - center: Vec3::new(-1.0, 0.0, -1.0), - radius: -0.4, - material: Some(mat_left), } - ) - ); - - world.add( - Box::new( - Sphere{ - center: Vec3::new( 1.0, 0.0, -1.0), - radius: 0.5, + Sphere { + center: Vec3::new( R, 0.0, -1.0), + radius: R, material: Some(mat_right), } ) ); + // camera - let cam = Camera::new(); + let cam = Camera::new(90.0, aspect_ratio); // render let mut small_rng = SmallRng::from_entropy(); @@ -139,7 +112,7 @@ fn ray_color(r: Ray, world: &dyn Hittable, depth: u32, srng: &mut SmallRng, dist return Vec3::ones() * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t } -fn degrees_to_radians(degrees: f32) -> f32 { +pub fn degrees_to_radians(degrees: f32) -> f32 { degrees * std::f32::consts::PI / 180.0 }