From 2569a487921d2c3b211a09adee22f72b0bdef2bc Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 9 Jun 2024 15:58:24 -0400 Subject: [PATCH] Finish PPM conversions and plot the projectile --- ray-tracer/src/bin/projectile.rs | 30 ++++++++++++++++++++---------- ray-tracer/src/lib.rs | 5 ++++- ray-tracer/src/ppm.rs | 4 +--- ray-tracer/src/types/canvas.rs | 6 +++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ray-tracer/src/bin/projectile.rs b/ray-tracer/src/bin/projectile.rs index 46711a1..4342f39 100644 --- a/ray-tracer/src/bin/projectile.rs +++ b/ray-tracer/src/bin/projectile.rs @@ -1,4 +1,5 @@ -use ray_tracer::types::*; +use ray_tracer::{types::*, PPM}; +use std::{fs::File, io::Write}; #[derive(Clone, Copy)] struct Projectile { @@ -18,26 +19,35 @@ fn tick(env: &Environment, proj: &Projectile) -> Projectile { } fn main() { + let mut canvas = Canvas::new(900, 550); + let start = Projectile { position: Point::new(0., 1., 0.), - velocity: Vector::new(1., 1., 0.).normalize() * 5., + velocity: Vector::new(1., 5., 0.).normalize() * 5., }; let e = Environment { gravity: Vector::new(0., -0.1, 0.), - wind: Vector::new(-0.1, 0., 0.), + wind: Vector::new(-0.01, 0., 0.), }; let mut p = start; while p.position.y() > 0. { p = tick(&e, &p); + + let x = p.position.x().round() as usize; + let y = p.position.y().round() as usize; + if x > 1 && x < 900 && y > 1 && y < 550 { + *canvas.pixel_mut(x, 550 - y - 1) = Color::new(1., 1., 0.); + *canvas.pixel_mut(x+1, 550 - y - 1) = Color::new(1., 1., 0.); + *canvas.pixel_mut(x-1, 550 - y - 1) = Color::new(1., 1., 0.); + *canvas.pixel_mut(x, 550 - y) = Color::new(1., 1., 0.); + *canvas.pixel_mut(x, 550 - y - 2) = Color::new(1., 1., 0.); + } } - println!( - "distance travelled: [{}] {} {} {}", - (p.position - start.position).magnitude(), - p.position.x(), - p.position.y(), - p.position.z(), - ); + let ppm = PPM::from(canvas); + + let mut file = File::create("projectile.ppm").unwrap(); + file.write(ppm.as_bytes()).unwrap(); } diff --git a/ray-tracer/src/lib.rs b/ray-tracer/src/lib.rs index a498975..b6604d1 100644 --- a/ray-tracer/src/lib.rs +++ b/ray-tracer/src/lib.rs @@ -1,2 +1,5 @@ -pub mod ppm; +mod ppm; + pub mod types; + +pub use ppm::PPM; diff --git a/ray-tracer/src/ppm.rs b/ray-tracer/src/ppm.rs index 4689d8a..50c8792 100644 --- a/ray-tracer/src/ppm.rs +++ b/ray-tracer/src/ppm.rs @@ -24,13 +24,11 @@ fn join_to_line_limit(data: impl IntoIterator) -> Vec { lines.push(line); } - println!("{:?}", lines); - lines } #[derive(Debug)] -struct PPM(String); +pub struct PPM(String); impl From for PPM { fn from(c: Canvas) -> Self { diff --git a/ray-tracer/src/types/canvas.rs b/ray-tracer/src/types/canvas.rs index edbfc43..85fe37e 100644 --- a/ray-tracer/src/types/canvas.rs +++ b/ray-tracer/src/types/canvas.rs @@ -36,7 +36,11 @@ impl Canvas { #[inline] fn addr(&self, y: usize, x: usize) -> usize { - y * self.width() + x + let val = y * self.width() + x; + if val >= self.pixels.len() { + eprintln!("[{val}] out of range: [{x}, {y}]"); + } + val } }