Add antialiasing

This commit is contained in:
Matthew Gordon 2020-09-09 20:51:30 -04:00
parent 773ca99ac1
commit a5f77e61b3
1 changed files with 7 additions and 5 deletions

View File

@ -8,6 +8,8 @@ use super::sampler::Sampler;
use super::scene::Scene; use super::scene::Scene;
use super::util::Tile; use super::util::Tile;
use rand::random;
struct ImageSampler { struct ImageSampler {
image_height_pixels: usize, image_height_pixels: usize,
image_width_pixels: usize, image_width_pixels: usize,
@ -44,7 +46,7 @@ impl ImageSampler {
let n = n as f64; let n = n as f64;
let i = i as f64; let i = i as f64;
let pixel_size = l * (1.0 / n); let pixel_size = l * (1.0 / n);
(i + 0.5) * pixel_size (i + random::<f64>()) * pixel_size
} }
fn ray_for_pixel(&self, row: usize, column: usize) -> Ray { fn ray_for_pixel(&self, row: usize, column: usize) -> Ray {
@ -141,13 +143,13 @@ mod tests {
#[test] #[test]
fn scale_returns_correct_value_for_zero() { fn scale_returns_correct_value_for_zero() {
let correct_value = (3.0 / 10.0) / 2.0; let correct_value = (3.0 / 10.0) / 2.0;
assert!((ImageSampler::scale(0, 10, 3.0f64) - correct_value).abs() < 0.0000000001) assert!((ImageSampler::scale(0, 10, 3.0f64) - correct_value).abs() < 0.5)
} }
#[test] #[test]
fn scale_returns_correct_value_for_last_pixel() { fn scale_returns_correct_value_for_last_pixel() {
let correct_value = 3.0 - (3.0 / 10.0) / 2.0; let correct_value = 3.0 - (3.0 / 10.0) / 2.0;
assert!((ImageSampler::scale(9, 10, 3.0f64) - correct_value).abs() < 0.0000000001) assert!((ImageSampler::scale(9, 10, 3.0f64) - correct_value).abs() < 0.5)
} }
#[test] #[test]
@ -173,10 +175,10 @@ mod tests {
}; };
let expected_x: f64 = let expected_x: f64 =
ImageSampler::scale(200, 800, target.film_width) - target.film_width * 0.5; ImageSampler::scale(200, 800, target.film_width) - target.film_width * 0.5;
assert!((point_on_film_plane.x() - expected_x).abs() < 0.0000000001); assert!((point_on_film_plane.x() - expected_x).abs() < 0.5 / 200.0);
let expected_y = let expected_y =
-ImageSampler::scale(100, 600, target.film_height) + target.film_height * 0.5; -ImageSampler::scale(100, 600, target.film_height) + target.film_height * 0.5;
assert!((point_on_film_plane.y() - expected_y).abs() < 0.0000000001); assert!((point_on_film_plane.y() - expected_y).abs() < 0.5 / 800.0);
} }
} }
} }