Skip to content

Commit

Permalink
8 - antialiasing
Browse files Browse the repository at this point in the history
  • Loading branch information
grishy committed Feb 18, 2024
1 parent 96f6683 commit f3334ee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ nalgebra = "0.32.3"
indicatif = "0.17.8"
rayon = "1.8.1"
itertools = "0.12.1"
rand = "0.8.5"
47 changes: 38 additions & 9 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use indicatif::ProgressIterator;
use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools;
use rand::{self, Rng};
use std::fs::File;
use std::ops::Range;

Expand All @@ -20,6 +21,7 @@ pub struct Camera {
pixel00_loc: Point3, // Location of pixel 0, 0
pixel_delta_u: Vector3, // Offset to pixel to the right
pixel_delta_v: Vector3, // Offset to pixel below
samples_per_pixel: i32, // Count of random samples for each pixel
}

impl Camera {
Expand Down Expand Up @@ -57,6 +59,7 @@ impl Camera {
pixel00_loc: pixel00_loc,
pixel_delta_u: pixel_delta_u,
pixel_delta_v: pixel_delta_v,
samples_per_pixel: 100,
}
}

Expand All @@ -75,18 +78,22 @@ impl Camera {
let pixels: Vec<String> = (0..self.image_height)
.cartesian_product(0..self.image_width)
.map(|(y, x)| {
let pixel_center = self.pixel00_loc
+ (x as f64 * self.pixel_delta_u)
+ (y as f64 * self.pixel_delta_v);
let ray_direction = pixel_center - self.center;
let r = ray::Ray::new(self.center, ray_direction);
let pixel_color = ray_color(&r, &world);
// Send few rays to the pixel
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
for _ in 0..self.samples_per_pixel {
let r = self.get_ray(x, y);
pixel_color += ray_color(&r, &world);
}

// Divide the color by the number of samples.
pixel_color /= self.samples_per_pixel as f64;
pixel_color *= 256.0;

format!(
"{} {} {}",
(255.999 * pixel_color.x) as i32,
(255.999 * pixel_color.y) as i32,
(255.999 * pixel_color.z) as i32
(pixel_color.x.clamp(0., 256.)) as i32,
(pixel_color.y.clamp(0., 256.)) as i32,
(pixel_color.z.clamp(0., 256.)) as i32
)
})
.progress_with(pb)
Expand All @@ -106,6 +113,28 @@ impl Camera {
)
.unwrap();
}

// Get a randomly sampled camera ray for the pixel at location i,j.
fn get_ray(&self, x: i32, y: i32) -> ray::Ray {
let pixel_center =
self.pixel00_loc + (x as f64 * self.pixel_delta_u) + (y as f64 * self.pixel_delta_v);
let pixel_sample = self.pixel_sample_square();

let ray_origin = self.center;
let ray_direction = (pixel_center + pixel_sample) - self.center;

return ray::Ray::new(ray_origin, ray_direction);
}

// Returns a random point in the square surrounding a pixel at the origin.
fn pixel_sample_square(&self) -> Vector3 {
let mut rng = rand::thread_rng();

let px = -0.5 + rng.gen_range(0.0..1.0);
let py = -0.5 + rng.gen_range(0.0..1.0);

return (px * self.pixel_delta_u) + (py * self.pixel_delta_v);
}
}

fn ray_color(ray: &ray::Ray, world: &HittableList) -> Color {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl hittable::Hittable for HittableList {
fn main() {
// Params
let aspect_ratio = 16.0 / 9.0;
let image_width = 600;
let image_width = 400;

// Camera
let camera = camera::Camera::new(aspect_ratio, image_width);
Expand Down

0 comments on commit f3334ee

Please sign in to comment.