use rand::prelude::*; use std::io::Write; use crate::color::Color; use crate::hit::Hittable; use crate::material::Scatter; use crate::ray::Ray; use crate::vec::{Point3, Vec3, cross, unit_vector}; pub struct Camera { /// Rendered image width in pixel count image_width: u32, /// Rendered image height image_height: u32, /// // Camera center center: Point3, /// Location of pixel 0, 0 pixel00_loc: Point3, /// Offset to pixel to the right pixel_delta_u: Vec3, /// Offset to pixel below pixel_delta_v: Vec3, /// Count of random samples for each pixel samples_per_pixel: u32, /// Maximum number of ray bounces into scene max_depth: u32, defocus_angle: f64, /// Defocus disk horizontal radius defocus_disk_u: Vec3, /// Defocus disk vertical radius defocus_disk_v: Vec3, } pub struct CameraOptions { pub aspect_ratio: f64, pub image_width: u32, pub vertical_fov: f64, pub look_from: Point3, pub look_at: Point3, /// Variation angle of rays through each pixel pub defocus_angle: f64, /// Distance from camera lookfrom point to plane of perfect focus pub focus_dist: f64, } impl Default for CameraOptions { fn default() -> Self { Self { aspect_ratio: 1.0, image_width: 100, vertical_fov: 90.0, look_from: Point3::new(0.0, 0.0, 0.0), look_at: Point3::new(0.0, 0.0, -1.0), defocus_angle: 0.0, focus_dist: 10.0, } } } fn ray_color(ray: &Ray, world: &impl Hittable, depth: u32, rng: &mut ThreadRng) -> Color { if depth <= 0 { return Color::default(); } if let Some(hit_record) = world.hit(&ray, 0.001..=f64::INFINITY) { let Scatter { attenuation, scattered, } = hit_record.material.scatter(ray, &hit_record, rng); return Color(*attenuation * *ray_color(&scattered, world, depth - 1, rng)); } let unit_direction = unit_vector(ray.direction); let a = 0.5 * (unit_direction.y + 1.0); Color((1.0 - a) * Vec3::new(1.0, 1.0, 1.0) + a * Vec3::new(0.5, 0.7, 1.0)) } impl Camera { pub fn new( CameraOptions { aspect_ratio, image_width, vertical_fov, look_from, look_at, defocus_angle, focus_dist, }: CameraOptions, ) -> Self { let image_height = (image_width as f64 / aspect_ratio) as u32; let image_height = if image_height < 1 { 1 } else { image_height }; let theta = vertical_fov.to_radians(); let h = (theta / 2.0).tan(); let viewport_height = 2.0 * h * focus_dist; let viewport_width = viewport_height * (image_width as f64) / (image_height as f64); let vup = Vec3::new(0.0, 1.0, 0.0); let w = unit_vector(look_from - look_at); let u = unit_vector(cross(vup, w)); let v = cross(w, u); let viewport_u = viewport_width * u; let viewport_v = viewport_height * -v; let pixel_delta_u = viewport_u / image_width as f64; let pixel_delta_v = viewport_v / image_height as f64; let center = look_from; let viewport_upper_left = center - (focus_dist * w) - viewport_u / 2.0 - viewport_v / 2.0; let pixel00_loc = viewport_upper_left + 0.5 * (pixel_delta_u + pixel_delta_v); let defocus_radius = focus_dist * (defocus_angle / 2.0).to_radians().tan(); let defocus_disk_u = u * defocus_radius; let defocus_disk_v = v * defocus_radius; Camera { image_width, image_height, center, pixel00_loc, pixel_delta_u, pixel_delta_v, samples_per_pixel: 10, max_depth: 10, defocus_disk_u, defocus_disk_v, defocus_angle, } } /// Returns the vector to a random point in the [-.5,-.5]-[+.5,+.5] unit square. fn sample_square(rng: &mut ThreadRng) -> Vec3 { Vec3::new( rng.random_range(-0.5..0.5), rng.random_range(-0.5..0.5), 0.0, ) } /// Returns a random point in the camera defocus disk. fn defocus_disk_sample( center: Point3, defocus_disk_u: Vec3, defocus_disk_v: Vec3, rng: &mut ThreadRng, ) -> Point3 { let p = Vec3::random_unit_disk(rng); center + (p.x * defocus_disk_u) + (p.y * defocus_disk_v) } fn get_ray(&self, i: usize, j: usize, rng: &mut ThreadRng) -> Ray { let Self { center, pixel00_loc, pixel_delta_u, pixel_delta_v, defocus_angle, defocus_disk_u, defocus_disk_v, .. } = &self; let offset = Self::sample_square(rng); let pixel_center = *pixel00_loc + ((i as f64 + offset.x) * *pixel_delta_u) + ((j as f64 + offset.y) * *pixel_delta_v); let ray_origin = if *defocus_angle <= 0.0 { *center } else { Self::defocus_disk_sample(*center, *defocus_disk_u, *defocus_disk_v, rng) }; let ray_direction = pixel_center - ray_origin; Ray::new(ray_origin, ray_direction) } pub fn render(&self, world: &impl Hittable, writer: &mut impl Write) { let Self { image_width, image_height, samples_per_pixel, .. } = &self; write!(writer, "P3\n{image_width} {image_height}\n255\n").expect("ppm header"); use rayon::prelude::*; let mut pixels = vec![Color::default(); (*image_width * *image_height) as usize]; pixels .par_chunks_mut(*image_width as usize) .enumerate() .for_each(|(j, row)| { let mut rng = rand::rng(); for (i, pixel) in row.iter_mut().enumerate() { let mut pixel_color = Color::default(); for _ in 0..*samples_per_pixel { let ray = self.get_ray(i, j, &mut rng); *pixel_color += *ray_color(&ray, world, self.max_depth, &mut rng); } *pixel_color /= *samples_per_pixel as f64; *pixel = pixel_color; } }); for pixel_color in pixels { writeln!(writer, "{pixel_color} ").expect("write_color"); } writer.flush().expect("flush"); eprintln!("\r Done. \n"); } }