~ttt/ray_tracing_in_one_weekend

66dd5e2784ae2e78e8d9081e5cb20f8035bef929 — Tomasz Kłak 4 years ago 236f843
ch6.1 done
1 files changed, 10 insertions(+), 4 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +10 -4
@@ 4,18 4,24 @@ use vec3::*;
mod ray;
use ray::*;

fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> bool {
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
    let oc = r.origin() - *center;
    let a = dot(&r.direction(), &r.direction());
    let b = 2.0 * dot(&oc, &r.direction());
    let c = dot(&oc, &oc) - radius*radius;
    let discriminant = b*b - 4f64*a*c;
    discriminant > 0.0
    if discriminant < 0.0 {
        -1.0
    } else {
        (-b - discriminant.sqrt() ) / (2.0*a)
    }
}

fn ray_color(r: &Ray) -> Color {
    if hit_sphere(&Point3::new(0.0, 0.0, -1f64), 0.5, r) {
        return Color::new(1.0, 0.0, 0.0);
    let t = hit_sphere(&Point3::new(0.0, 0.0, -1f64), 0.5, r);
    if t > 0.0 {
        let N = unit_vector(r.at(t) - Vec3::new(0.0, 0.0, -1.0));
        return 0.5*Color::new(N.x()+1.0, N.y()+1.0, N.z()+1.0);
    }
    let unit_direction = unit_vector(r.direction());
    let t = 0.5 * (unit_direction.y() + 1.0);