diff --git a/project/src/main.rs b/project/src/main.rs index d369dfc..d4b0b64 100644 --- a/project/src/main.rs +++ b/project/src/main.rs @@ -245,34 +245,52 @@ fn main() -> AnyResult<()> { window.update_with_buffer(&buffer, WINDOW_WIDTH, WINDOW_HEIGHT)?; window.set_target_fps(30); - let mut frame = 0.0; - - let trb = Point3d::new(5.0, 5.0, 5.0); - let trf = Point3d::new(4.0, 5.0, 5.0); - let tlb = Point3d::new(5.0, 5.0, 4.0); - let tlf = Point3d::new(4.0, 5.0, 4.0); - let blf = Point3d::new(4.0, 4.0, 4.0); - let blb = Point3d::new(5.0, 4.0, 4.0); - let brf = Point3d::new(4.0, 4.0, 5.0); - let brb = Point3d::new(5.0, 4.0, 5.0); + let trb = Point3d::new(1.0, 1.0, 1.0); + let trf = Point3d::new(0.0, 1.0, 1.0); + let tlb = Point3d::new(1.0, 1.0, 0.0); + let tlf = Point3d::new(0.0, 1.0, 0.0); + let blf = Point3d::new(0.0, 0.0, 0.0); + let blb = Point3d::new(1.0, 0.0, 0.0); + let brf = Point3d::new(0.0, 0.0, 1.0); + let brb = Point3d::new(1.0, 0.0, 1.0); let mut cube = Cube::new(trb, trf, tlb, tlf, blf, blb, brf, brb); let mut gif_buffer: Vec> = Vec::new(); + let mut x_trans = 0.0; + let mut y_trans = 0.0; + let mut z_trans = 0.0; + + const X_TRANS_RATE: f64 = 1.0 / 30.0; + const Y_TRANS_RATE: f64 = 0.5 / 30.0; + const Z_TRANS_RATE: f64 = 2.0 / 30.0; + + const MAX_TRANS: f64 = 6.0; + while window.is_open() { //Reload the background for each frame let mut buffer = background_buffer.clone(); - frame += 1.0; + cube.translate(Point3d::new(X_TRANS_RATE, Y_TRANS_RATE, Z_TRANS_RATE)); - cube.translate(Point3d::new(0.01, 0.0, 0.0)); update_buffer_with_pixels(&mut buffer, cube.render(&m)); - if frame > 150.0 { - frame = 0.0; + x_trans += X_TRANS_RATE; + y_trans += Y_TRANS_RATE; + z_trans += Z_TRANS_RATE; - cube.translate(Point3d::new(-1.5, 0.0, 0.0)); + if x_trans > MAX_TRANS { + cube.translate(Point3d::new(-x_trans, 0.0, 0.0)); + x_trans = 0.0; + } + if y_trans > MAX_TRANS { + cube.translate(Point3d::new(0.0, -y_trans, 0.0)); + y_trans = 0.0; + } + if z_trans > MAX_TRANS { + cube.translate(Point3d::new(0.0, 0.0, -z_trans)); + z_trans = 0.0; } //Required to close, and update frame; @@ -288,7 +306,7 @@ fn main() -> AnyResult<()> { ); } - export_gif(WINDOW_WIDTH, WINDOW_HEIGHT, 30, gif_buffer); + export_gif(WINDOW_WIDTH, WINDOW_HEIGHT, 30, gif_buffer)?; Ok(()) } @@ -442,7 +460,7 @@ fn export_gif( height: usize, fps: u32, frames: Vec>, // Each inner Vec is width * height * 4 bytes (RGBA) -) -> Result<(), Box> { +) -> AnyResult<()> { let file = File::create("animation.gif")?; let mut encoder = GifEncoder::new(file); @@ -451,8 +469,7 @@ fn export_gif( for raw_buffer in frames { // Construct the image from your raw pixel slice - let img = RgbaImage::from_raw(width as u32, height as u32, raw_buffer) - .ok_or("Buffer length didn't match width * height * 4")?; + let img = RgbaImage::from_raw(width as u32, height as u32, raw_buffer).unwrap(); // Calculate frame timing (e.g., 1000 / 30 = 33.3ms) let delay = Delay::from_numer_denom_ms(1000, fps);