Skip to content

Commit

Permalink
normals!
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBiscardi committed Aug 27, 2023
1 parent 5b05773 commit 9bec2b1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main.rs
Expand Up @@ -82,8 +82,12 @@ impl Ray {
self.origin + t * self.direction
}
fn color(&self) -> DVec3 {
if hit_sphere(&DVec3::new(0., 0., -1.), 0.5, self) {
return DVec3::new(1., 0., 0.);
let t =
hit_sphere(&DVec3::new(0., 0., -1.), 0.5, self);
if t > 0.0 {
let N = (self.at(t) - DVec3::new(0., 0., -1.))
.normalize();
return 0.5 * (N + 1.0);
};

let unit_direction: DVec3 =
Expand All @@ -98,11 +102,16 @@ fn hit_sphere(
center: &DVec3,
radius: f64,
ray: &Ray,
) -> bool {
) -> f64 {
let oc: DVec3 = ray.origin - *center;
let a = ray.direction.dot(ray.direction);
let b = 2.0 * oc.dot(ray.direction);
let c = oc.dot(oc) - radius * radius;
let discriminant = b * b - 4. * a * c;
discriminant >= 0.

if discriminant < 0. {
-1.0
} else {
(-b - discriminant.sqrt()) / (2.0 * a)
}
}

0 comments on commit 9bec2b1

Please sign in to comment.