From 1207c2c55c772155d02a148d76616881f3f6c125 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sun, 2 Mar 2025 00:25:53 +0700 Subject: upd: anti aliasing --- src/camera.rs | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'src/camera.rs') diff --git a/src/camera.rs b/src/camera.rs index 587a312..8e9cf68 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,16 +1,18 @@ -use crate::calculus::calculus::{Point3, Ray, Vec3}; -use crate::global::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, VIEWPORT_HEIGHT}; +use crate::calculus::calculus::{sample_square, Point3, Ray, Vec3}; +use crate::global::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, SAMPLES_PER_PIXEL, VIEWPORT_HEIGHT}; use crate::interval::Interval; -use crate::object::{HitRecord, Hittable, HittableList, Sphere}; +use crate::object::{HitRecord, Hittable, HittableList}; pub struct Camera { pub aspect_ratio: f32, pub image_width: usize, + pub samples_per_pixel: usize, image_height: usize, center: Point3, pixel_upper_left: Point3, delta_pixel_u: Vec3, delta_pixel_v: Vec3, + pixel_samples_scale: f32, } impl Camera { @@ -23,6 +25,9 @@ impl Camera { let center = CAMERA_CENTER; let focal_length = FOCAL_LENGTH; + let samples_per_pixel = SAMPLES_PER_PIXEL; + let pixel_samples_scale = 1.0 / samples_per_pixel as f32; + let viewport_height = VIEWPORT_HEIGHT; let viewport_width = viewport_height * (image_width as f32 / image_height as f32); @@ -48,6 +53,23 @@ impl Camera { pixel_upper_left, delta_pixel_u, delta_pixel_v, + samples_per_pixel, + pixel_samples_scale, + } + } + + fn get_ray(&self, i: usize, j: usize) -> Ray { + let offset = sample_square(); + let pixel_sample = self.pixel_upper_left + .add(&self.delta_pixel_u.scalar_mul(i as f32 + offset.x)) + .add(&self.delta_pixel_v.scalar_mul(j as f32 + offset.y)); + + let ray_origin = self.center; + let ray_direction = pixel_sample.sub(&ray_origin); + + Ray { + origin: ray_origin, + direction: ray_direction, } } @@ -75,16 +97,14 @@ impl Camera { pub fn render(&self, display_buffer: &mut DisplayBuffer, world: &HittableList) { (0..self.image_height).for_each(|j| { (0..self.image_width).for_each(|i| { - let pixel_center = self.pixel_upper_left - .add(&self.delta_pixel_u.scalar_mul(i as f32)) - .add(&self.delta_pixel_v.scalar_mul(j as f32)); - let ray_direction = pixel_center.sub(&self.center); - let ray = Ray { - origin: &self.center, - direction: &ray_direction, - }; - let color = self.ray_color(&ray, &world); - display_buffer[j][i] = Pixel::from_color(&color); + let mut pixel_color = Color::new(0.0, 0.0, 0.0); + (0..self.samples_per_pixel).for_each(|_| { + let r = self.get_ray(i, j); + let ray_color = self.ray_color(&r, world); + pixel_color = pixel_color.add(&ray_color); + }); + pixel_color = pixel_color.mul_scalar(self.pixel_samples_scale); + display_buffer[j][i] = Pixel::from_color(&pixel_color); }) }) } -- cgit v1.2.3-70-g09d2