can make a ppm

This commit is contained in:
Jon Janzen
2021-03-20 20:12:47 -06:00
parent 842c93568b
commit 1f33d87193
4 changed files with 52 additions and 7 deletions

16
Cargo.lock generated
View File

@@ -15,6 +15,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "canvas"
version = "0.1.0"
dependencies = [
"color",
]
[[package]]
name = "color"
version = "0.1.0"
dependencies = [
"approx",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.14" version = "0.2.14"
@@ -35,5 +49,7 @@ dependencies = [
name = "tuples" name = "tuples"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"canvas",
"color",
"structs", "structs",
] ]

View File

@@ -7,4 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
canvas = { path = "canvas" }
color = { path = "color" }
structs = { path = "structs" } structs = { path = "structs" }

View File

@@ -14,6 +14,14 @@ impl Canvas {
pixels : vec![vec![Color::new(0.0, 0.0, 0.0); width]; height], pixels : vec![vec![Color::new(0.0, 0.0, 0.0); width]; height],
} }
} }
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
self.height
}
} }
impl Canvas { impl Canvas {

View File

@@ -1,6 +1,10 @@
use canvas::Canvas;
use color::Color;
use structs::Tuple; use structs::Tuple;
use std::fmt; use std::fmt;
use std::fs::File;
use std::io::prelude::*;
#[derive(Debug)] #[derive(Debug)]
struct Environment { struct Environment {
@@ -57,15 +61,30 @@ fn main() {
let mut ball = Projectile::new( let mut ball = Projectile::new(
Tuple::point(0.0, 0.0, 1.0), Tuple::point(0.0, 0.0, 1.0),
Tuple::vector(10.0, 0.0, 100.0), Tuple::vector(2.2, 0.0, 21.0),
); );
let mut tick = 0; let mut canvas = Canvas::new(300, 300);
println!("tick {} the ball {}", tick, ball); let color = Color::new(1.0, 1.0, 0.0);
while ball.position.z() > 0.0 { while ball.position.z() < canvas.height() as f32
ball.tick(&env); && ball.position.z() >= 0.0
&& ball.position.x() < canvas.width() as f32
&& ball.position.x() >= 0.0 {
canvas.write_pixel(ball.position.x() as usize, canvas.height() - ball.position.z() as usize, color);
println!("tick {} the ball {}", tick, ball); ball.tick(&env);
tick += 1;
} }
let ppm = canvas.to_ppm();
let mut f = match File::create("out.ppm") {
Ok(f) => f,
Err(e) => panic!("file error. {}", e),
};
let _ = match f.write_all(ppm.as_bytes()) {
Ok(w) => w,
Err(e) => panic!("did not write. {}", e),
};
} }