diff --git a/Cargo.lock b/Cargo.lock index 0426dec..6c29be9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,31 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossterm" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a84cda67535339806297f1b331d6dd6320470d2a0fe65381e79ee9e156dd3d13" +dependencies = [ + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crunchy" version = "0.2.2" @@ -319,6 +344,18 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mio" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + [[package]] name = "nalgebra" version = "0.7.0" @@ -444,6 +481,29 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "pin-project" version = "1.1.0" @@ -573,6 +633,15 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + [[package]] name = "rustc-serialize" version = "0.3.24" @@ -584,6 +653,7 @@ name = "rustyrays" version = "0.1.0" dependencies = [ "bmp", + "crossterm", "image", "nalgebra", "rand 0.4.6", @@ -595,6 +665,36 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "signal-hook" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simd-adler32" version = "0.3.5" @@ -732,6 +832,72 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + [[package]] name = "zune-inflate" version = "0.2.54" diff --git a/Cargo.toml b/Cargo.toml index fe025d4..775ae0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ bmp = "*" nalgebra = "0.7.0" rand = "*" image = "*" +crossterm = "*" diff --git a/src/camera.rs b/src/camera.rs index 9bbb506..c1962c6 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -58,6 +58,14 @@ impl PerspectiveCamera { dir: Vec3::new(sensor_x, sensor_y, 1.0).normalize(), } } + + pub fn percent_complete(&self, row: u32) -> f64 { + let float_row = row as f64; + let float_scene_height = self.scene_height as f64; + let float_scene_width = self.scene_width as f64; + + ((float_row * float_scene_width) / (float_scene_width * float_scene_height)) * 100.0 + } } impl RaySource for PerspectiveCamera { diff --git a/src/main.rs b/src/main.rs index ce2879d..be7d458 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use std::f32; -//use std::ops::{Add,Mul}; mod camera; use crate::camera::PerspectiveCamera; @@ -23,27 +22,11 @@ use nalgebra::*; use bmp::Image; use bmp::Pixel; -fn main() { - //let mut camera = OrthoCamera { - // pos: Vec3::new(0.0, 0.0, -1000.0), - // output_img: Image::new(2560,2560), - // elements: Vec::new(), - // lights: Vec::new(), - // shadow_bias: 1e-3, - // max_recursion_depth: 5 - //}; - let mut camera = PerspectiveCamera { - pos: Vec3::new(1280.0, 1280.0, -1000.0), - output_img: Image::new(2560,2560), - elements: Vec::new(), - lights: Vec::new(), - shadow_bias: 1e-3, - max_recursion_depth: 5, - fov: 90.0, - scene_width: 2560, - scene_height: 2560, -}; +use std::{thread,time}; +use std::io::{Write,stdout}; +use crossterm::{QueueableCommand,cursor,terminal,ExecutableCommand}; +fn initialize_scene(camera: &mut PerspectiveCamera) { camera.lights.push(LightSrc::new(Vec3::new(200.0, 800.0, 300.0), 5.0)); camera.lights.push(LightSrc::new(Vec3::new(1200.0, 800.0, 300.0), 5.0)); @@ -102,14 +85,48 @@ fn main() { }; camera.elements.push(Element::Sphere(top_sphere)); - println!("Raytracing ..."); +} + +fn main() { + let mut camera = PerspectiveCamera { + pos: Vec3::new(1280.0, 1280.0, -1000.0), + output_img: Image::new(2560,2560), + elements: Vec::new(), + lights: Vec::new(), + shadow_bias: 1e-3, + max_recursion_depth: 5, + fov: 90.0, + scene_width: 2560, + scene_height: 2560, + }; + + initialize_scene(&mut camera); + + println!("Rusty Rays v1.0 Alpha\n"); + println!("Raytracing...\n"); + + let mut stdout = stdout(); + stdout.execute(cursor::Hide).unwrap(); + + stdout.write_all(format!("Progress: ").as_bytes()).unwrap(); + // TODO: Uncomment for (x, y) in camera.output_img.coordinates() { + stdout.queue(cursor::SavePosition).unwrap(); + stdout.write_all(format!("{:.1}%", camera.percent_complete(y)).as_bytes()).unwrap(); + //stdout.write_all(format!("Raytracing.. {} : {}", x, y).as_bytes()).unwrap(); + stdout.queue(cursor::RestorePosition).unwrap(); + stdout.flush().unwrap(); + //thread::sleep(time::Duration::from_millis(100)); + + stdout.queue(cursor::RestorePosition).unwrap(); + stdout.queue(terminal::Clear(terminal::ClearType::FromCursorDown)).unwrap(); camera.output_img.set_pixel(x, y, px!(20, 20, 20)); - //let prime_ray = Ray::new(Vec3::new(x as f64, y as f64, camera.pos.z as f64), Vec3::new(0.0, 0.0, 1.0)); let prime_ray = camera.create_prime(x, y); let pixel = cast_ray(&camera, &prime_ray, 0); camera.output_img.set_pixel(x, y, px!(pixel.red, pixel.green, pixel.blue)); } + stdout.queue(cursor::RestorePosition).unwrap(); + println!("Render complete! 🎉"); let _ = camera.output_img.save("img.bmp"); }