Skip to content

Commit

Permalink
add shadows with no acne
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaretto committed Mar 31, 2024
1 parent 3566c30 commit b1475ca
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.77.0"
components = [ "rustfmt", "clippy" ]
components = [ "rustfmt", "clippy", "rust-analyzer", "rust-src" ]
5 changes: 2 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ mod tests {
use super::*;

use crate::color::Color;
use crate::matrix;
use crate::vector;
use approx::relative_eq;
use core::f64::consts::PI;
Expand All @@ -97,10 +96,10 @@ mod tests {
#[test]
fn test_camera_pixel_size_calculation() {
let camera = Camera::new(200, 125, FRAC_PI_2);
relative_eq!(camera.pixel_size(), 0.01, epsilon = matrix::EPSILON);
relative_eq!(camera.pixel_size(), 0.01, epsilon = crate::EPSILON);

let camera = Camera::new(125, 200, FRAC_PI_2);
relative_eq!(camera.pixel_size(), 0.01, epsilon = matrix::EPSILON);
relative_eq!(camera.pixel_size(), 0.01, epsilon = crate::EPSILON);
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ impl Color {
}
}

pub const EPSILON: f64 = 0.001;
pub const WHITE: Color = Color {
red: 1.0,
green: 1.0,
Expand All @@ -22,9 +21,9 @@ pub const WHITE: Color = Color {

impl PartialEq for Color {
fn eq(&self, other: &Self) -> bool {
let red_diff = (self.red - other.red).abs() <= EPSILON;
let green_diff = (self.green - other.green).abs() <= EPSILON;
let blue_diff = (self.blue - other.blue).abs() <= EPSILON;
let red_diff = (self.red - other.red).abs() <= crate::EPSILON;
let green_diff = (self.green - other.green).abs() <= crate::EPSILON;
let blue_diff = (self.blue - other.blue).abs() <= crate::EPSILON;

red_diff && green_diff && blue_diff
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[macro_use]
extern crate impl_ops;

pub const EPSILON: f64 = 0.001;

pub mod camera;
pub mod canvas;
pub mod color;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() {
world.objects.push(right);
world.objects.push(left);

let mut camera = camera::Camera::new(500, 250, FRAC_PI_3);
let mut camera = camera::Camera::new(400, 400, FRAC_PI_3);
camera.transform =
matrix::view_transform(point!(0, 1.5, -5), point!(0, 1, 0), vector!(0, 1, 0));

Expand Down
4 changes: 1 addition & 3 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ pub fn view_transform(from: Tuple, to: Tuple, up: Tuple) -> Matrix {
orientation * Matrix::identity(4).translation(-from.x, -from.y, -from.z)
}

pub const EPSILON: f64 = 0.001;

impl PartialEq for Matrix {
fn eq(&self, other: &Self) -> bool {
if self.matrix.is_empty() && other.matrix.is_empty() {
Expand All @@ -196,7 +194,7 @@ impl PartialEq for Matrix {

for x in 0..lines {
for y in 0..columns {
if (self.at(x, y) - other.at(x, y)).abs() > EPSILON {
if (self.at(x, y) - other.at(x, y)).abs() > crate::EPSILON {
return false;
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Intersection<'_> {
t: self.t,
object: self.object,
point,
over_point: point + normalv * crate::EPSILON,
eyev,
normalv,
inside,
Expand All @@ -127,6 +128,7 @@ pub struct Computation<'a> {
pub t: f64,
pub object: &'a Sphere,
pub point: Tuple,
pub over_point: Tuple,
pub eyev: Tuple,
pub normalv: Tuple,
pub inside: bool,
Expand Down Expand Up @@ -287,6 +289,26 @@ mod tests {
assert_eq!(intersect.object, &sphere);
}

#[test]
fn test_hit_offset_point() {
let charles = Ray {
origin: point!(0.0, 0.0, -5.0),
direction: vector!(0.0, 0.0, 1.0),
};

let mut sphere = Sphere::init();
sphere.transform = Matrix::identity(4).translation(0.0, 0.0, 1.0);

let i = Intersection {
t: 5.0,
object: &sphere,
};
let comps = i.prepare_computations(&charles);

assert!(comps.over_point.z < -crate::EPSILON / 2.0);
assert!(comps.point.z > comps.over_point.z);
}

#[test]
fn test_hit() {
let i1 = Intersection {
Expand Down
4 changes: 1 addition & 3 deletions src/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::matrix;
use crate::matrix::*;
use crate::{matrix, EPSILON};
use std::ops;

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -120,8 +120,6 @@ impl Tuple {
}
}

pub const EPSILON: f64 = 0.001;

impl PartialEq for Tuple {
fn eq(&self, other: &Self) -> bool {
let x_diff = (self.x - other.x).abs() <= EPSILON;
Expand Down
2 changes: 1 addition & 1 deletion src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl World {
comps.point,
comps.eyev,
comps.normalv,
false,
self.is_shadowed(&comps.over_point),
);
current_color = current_color + color;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/scenarios.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use raytracer::{
color::Color,
light::Light,
matrix::Matrix,
point,
ray::{Intersection, Ray, Sphere},
tuple::Tuple,
vector,
world::World,
};

#[test]
// test shadowed scene
fn shade_git_given_an_intersection_in_shadow() {
let mut w = World::new();
w.lights.push(Light {
position: point!(0, 0, -10),
intensity: Color::new(1.0, 1.0, 1.0),
});
w.objects.push(Sphere::init());
let mut s2 = Sphere::init();
s2.transform = Matrix::identity(4).translation(0.0, 0.0, 10.0);
w.objects.push(s2);
let r = Ray::new(point!(0, 0, 5), vector!(0, 0, 1));
let i = Intersection {
t: 4.0,
object: &w.objects[1],
};
let comps = i.prepare_computations(&r);
let c = w.shade_hit(&comps);
assert_eq!(c, Color::new(0.1, 0.1, 0.1));
}

0 comments on commit b1475ca

Please # to comment.