From 5ca6cc080995b512d03c460bba683d4e029fe36f Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 8 Sep 2018 21:17:46 -0700 Subject: [PATCH] Add sphere. Move original tracer1 to tracer_blue_sky and create new tracer as tracer_red_dot. --- .../bin/{tracer1.rs => tracer_blue_sky.rs} | 13 ++++++ rtiow/src/bin/tracer_red_dot.rs | 46 +++++++++++++++++++ rtiow/src/ray.rs | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) rename rtiow/src/bin/{tracer1.rs => tracer_blue_sky.rs} (71%) create mode 100644 rtiow/src/bin/tracer_red_dot.rs diff --git a/rtiow/src/bin/tracer1.rs b/rtiow/src/bin/tracer_blue_sky.rs similarity index 71% rename from rtiow/src/bin/tracer1.rs rename to rtiow/src/bin/tracer_blue_sky.rs index d584e50..bac8282 100644 --- a/rtiow/src/bin/tracer1.rs +++ b/rtiow/src/bin/tracer_blue_sky.rs @@ -1,9 +1,22 @@ extern crate rtiow; use rtiow::ray::Ray; +use rtiow::vec3::dot; use rtiow::vec3::Vec3; +fn hit_sphere(center: Vec3, radius: f32, r: Ray) -> bool { + let oc = r.origin() - center; + let a = dot(r.direction(), r.direction()); + let b = 2. * dot(oc, r.direction()); + let c = dot(oc, oc) - radius * radius; + let discriminant = b * b - 4. * a * c; + discriminant > 0. +} + fn color(r: Ray) -> Vec3 { + if hit_sphere(Vec3::new(0., 0., -1.), 0.5, r) { + return Vec3::new(1., 0., 0.); + } let unit_direction = r.direction().unit_vector(); let t = 0.5 * (unit_direction.y + 1.); Vec3::new(1., 1., 1.) * (1. - t) + Vec3::new(0.5, 0.7, 1.) * t diff --git a/rtiow/src/bin/tracer_red_dot.rs b/rtiow/src/bin/tracer_red_dot.rs new file mode 100644 index 0000000..bac8282 --- /dev/null +++ b/rtiow/src/bin/tracer_red_dot.rs @@ -0,0 +1,46 @@ +extern crate rtiow; + +use rtiow::ray::Ray; +use rtiow::vec3::dot; +use rtiow::vec3::Vec3; + +fn hit_sphere(center: Vec3, radius: f32, r: Ray) -> bool { + let oc = r.origin() - center; + let a = dot(r.direction(), r.direction()); + let b = 2. * dot(oc, r.direction()); + let c = dot(oc, oc) - radius * radius; + let discriminant = b * b - 4. * a * c; + discriminant > 0. +} + +fn color(r: Ray) -> Vec3 { + if hit_sphere(Vec3::new(0., 0., -1.), 0.5, r) { + return Vec3::new(1., 0., 0.); + } + let unit_direction = r.direction().unit_vector(); + let t = 0.5 * (unit_direction.y + 1.); + Vec3::new(1., 1., 1.) * (1. - t) + Vec3::new(0.5, 0.7, 1.) * t +} + +fn main() -> Result<(), std::io::Error> { + let nx = 200; + let ny = 100; + println!("P3\n{} {}\n255", nx, ny); + let lower_left_corner = Vec3::new(-2., -1., -1.); + let horizontal = Vec3::new(4., 0., 0.); + let vertical = Vec3::new(0., 2., 0.); + let origin: Vec3 = Default::default(); + for j in (0..ny).rev() { + for i in 0..nx { + let u = i as f32 / nx as f32; + let v = j as f32 / ny as f32; + let r = Ray::new(origin, lower_left_corner + horizontal * u + vertical * v); + let col = color(r); + let ir = (255.99 * col[0]) as u32; + let ig = (255.99 * col[1]) as u32; + let ib = (255.99 * col[2]) as u32; + println!("{} {} {}", ir, ig, ib); + } + } + Ok(()) +} diff --git a/rtiow/src/ray.rs b/rtiow/src/ray.rs index a9a67c0..5e094e6 100644 --- a/rtiow/src/ray.rs +++ b/rtiow/src/ray.rs @@ -1,6 +1,6 @@ use vec3::Vec3; -#[derive(Default)] +#[derive(Copy, Clone, Default)] pub struct Ray { a: Vec3, b: Vec3,