Skip to content

Commit

Permalink
Step 9: Beyond the spheres
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarademir committed Aug 2, 2023
1 parent 9435291 commit 0fc8d93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Binary file modified out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 34 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ impl Material {
);
}

const CHECKER_BOARD_1: Vec3 = Vec3::ONE;
const CHECKER_BOARD_2: Vec3 = Vec3 {
x: 1.0,
y: 0.7,
z: 0.3,
};

impl Default for Material {
fn default() -> Self {
Material {
Expand Down Expand Up @@ -296,7 +303,33 @@ fn scene_intersect(
material = sphere.material;
}
}
if spheres_dist < 1000.0 {

let mut checkerboard_dist = std::f32::MAX;
if f32::abs(direction.y) > 1e-3 {
let plane_dist = -(origin.y + 4.0) / direction.y;
let plane = *origin + *direction * plane_dist;

let hits_plane = plane_dist > 0.0
&& f32::abs(plane.x) < 10.0
&& plane.z < -10.0
&& plane.z > -30.0
&& plane_dist < spheres_dist;
if hits_plane {
checkerboard_dist = plane_dist;
hit = plane;
normal = (0.0, 1.0, 0.0).into();

let checker_cond = ((0.5 * hit.x + 1000.0) as i32 + (0.5 * hit.z) as i32) & 1 == 1;

material.diffuse_colour = if checker_cond {
CHECKER_BOARD_1 * 0.3
} else {
CHECKER_BOARD_2 * 0.3
};
}
}

if f32::min(spheres_dist, checkerboard_dist) < 1000.0 {
return Some((material, hit, normal));
}
None
Expand Down

0 comments on commit 0fc8d93

Please sign in to comment.