From 6b89d4b046462f574099cb38c6f10fec8ddb79e3 Mon Sep 17 00:00:00 2001 From: ishanjain28 Date: Wed, 23 Jan 2019 03:00:41 +0530 Subject: [PATCH] Added some comments, Refactored a bit --- ria-weekend/src/ray.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ria-weekend/src/ray.rs b/ria-weekend/src/ray.rs index 6a285ba..ab0b884 100644 --- a/ria-weekend/src/ray.rs +++ b/ria-weekend/src/ray.rs @@ -20,26 +20,34 @@ impl Ray { } } -fn color(ray: Ray) -> Vec3 { +// linear interpolation based on y coordinate +// top to down +fn linear_interpolate_y(ray: Ray) -> Vec3 { let unit_direction = ray.direction().unit_vector(); let t = 0.5 * (unit_direction.y() + 1.0); - return Vec3::new(1.0, 1.0, 1.0) * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t; + // (1.0 - t) * start blend_color + t * end color + Vec3::new(1.0, 1.0, 1.0) * (1.0 - t) + Vec3::new(0.0, 0.0, 0.0) * t } pub fn create_ray_demo(buf: &mut String, w: u32, h: u32) { - let lower_left_corner = Vec3::new(-2.0, -1.0, -1.0); - let horizontal = Vec3::new(4.0, 0.0, 0.0); + // uses standard cg RHS notation + // y up, z pointing outwards and x to right + let lower_left_corner = Vec3::new(-1.0, -1.0, -1.0); + let horizontal = Vec3::new(2.0, 0.0, 0.0); let vertical = Vec3::new(0.0, 2.0, 0.0); + // observer let origin = Vec3::new(0.0, 0.0, 0.0); for j in (0..h).rev() { for i in 0..w { + // relative offsets + // current position to total width/length let u = i as f32 / w as f32; let v = j as f32 / h as f32; let ray = Ray::new(origin, lower_left_corner + horizontal * u + vertical * v); - let color = color(ray); + let color = linear_interpolate_y(ray); let ir = (255.99 * color[0]) as u32; let ig = (255.99 * color[1]) as u32; let ib = (255.99 * color[2]) as u32;