From 2dd4f4e940f350adce47fd97a5ef0b531e25e879 Mon Sep 17 00:00:00 2001 From: Guilleag01 Date: Fri, 17 Nov 2023 08:36:32 +0100 Subject: [PATCH] si --- .gitignore | 1 + Cargo.lock | 309 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ src/grid.rs | 45 +++++++ src/lib.rs | 3 + src/main.rs | 18 +++ src/renderer.rs | 83 +++++++++++++ src/util.rs | 89 ++++++++++++++ 8 files changed, 558 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/grid.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/renderer.rs create mode 100644 src/util.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bbb894d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,309 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crossterm" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" +dependencies = [ + "bitflags 2.4.1", + "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 = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" + +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[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 = "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.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "perlin-noise" +version = "0.1.0" +dependencies = [ + "crossterm", + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +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 = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +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.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +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.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9edb8bc --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "perlin-noise" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +crossterm = "0.27.0" +rand = "0.8.5" diff --git a/src/grid.rs b/src/grid.rs new file mode 100644 index 0000000..ffb4b8a --- /dev/null +++ b/src/grid.rs @@ -0,0 +1,45 @@ +use std::usize; + +use rand::{self, Rng}; + +#[derive(Clone)] +pub struct Grid { + size: (usize, usize), + grid: Vec>, +} + +impl Grid { + pub fn new(size: (usize, usize)) -> Self { + let mut rng = rand::thread_rng(); + // rng.gen_range(-1.0..1.0), rng.gen_range(-1.0..1.0) + // let v = vec![vec![[0.0, 0.0]; size.1]; size.0]; + // for i in 0..size.0 { + // for j in 0..size.1 {} + // } + let grid = (0..size.0) + .map(|_| { + (0..size.1) + .map(|_| [rng.gen_range(-1.0..1.0), rng.gen_range(-1.0..1.0)]) + .collect() + }) + .collect(); + + Self { size, grid } + } + + pub fn get_size(&self) -> (usize, usize) { + self.size + } + + pub fn get_grid(&self) -> Vec> { + self.grid.to_owned() + } + + pub fn get_vec(&self, pos: (usize, usize)) -> [f32; 2] { + self.grid[pos.0][pos.1] + } + + pub fn get_mat(&self) -> &Vec> { + &self.grid + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..76c92a2 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod grid; +pub mod renderer; +pub mod util; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..103956d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,18 @@ +use perlin_noise::grid::Grid; +use perlin_noise::renderer::Renderer; + +fn main() { + let grid_size = (5, 10); // (height, width) + + let grid = Grid::new(grid_size); + // println!("{:?}", grid.get_grid()); + + let renderer = Renderer::new(grid); + + renderer + .start() + .expect("An error occurred while starting the screen"); + renderer.render().unwrap(); + renderer.wait_for_key(); + renderer.stop().unwrap(); +} diff --git a/src/renderer.rs b/src/renderer.rs new file mode 100644 index 0000000..8944bee --- /dev/null +++ b/src/renderer.rs @@ -0,0 +1,83 @@ +use crossterm::{ + cursor::{Hide, MoveTo, Show}, + event::{ + read, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags, + }, + execute, queue, + style::Print, + terminal, + terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, +}; + +use crate::grid::Grid; +use crate::util::*; +use std::io::{stdout, Result}; + +pub struct Renderer { + screen_size: (usize, usize), // (height, width) + grid: Grid, + _offset: (usize, usize), +} + +impl Renderer { + pub fn new(grid: Grid) -> Self { + let s = terminal::size().unwrap(); + Self { + screen_size: (s.0 as usize, s.1 as usize), + grid, + _offset: (0, 0), + } + } + + pub fn start(&self) -> Result<()> { + terminal::enable_raw_mode()?; + queue!( + stdout(), + Clear(ClearType::All), + EnterAlternateScreen, + PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES), + PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES), + Hide, + MoveTo(0, 0) + )?; + + execute!(stdout())?; + + Ok(()) + } + + pub fn stop(&self) -> Result<()> { + terminal::disable_raw_mode()?; + execute!( + stdout(), + Show, + LeaveAlternateScreen, + PopKeyboardEnhancementFlags, + PopKeyboardEnhancementFlags + )?; + + Ok(()) + } + + // .:-=+*#%@ + pub fn render(&self) -> Result<()> { + for i in 0..self.screen_size.0 { + for j in 0..self.screen_size.1 { + let value = get_value_at_point((i, j), &self.grid, self.screen_size); + // println!("{}", value); + let char = " .:-=+*#%@" + .chars() + .nth(((value + 1.0) * 4.5) as usize) + .unwrap(); + queue!(stdout(), MoveTo(i as u16, j as u16), Print(char))?; + } + } + execute!(stdout())?; + + Ok(()) + } + + pub fn wait_for_key(&self) { + read().unwrap(); + } +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..bbffe7d --- /dev/null +++ b/src/util.rs @@ -0,0 +1,89 @@ +use crate::grid::Grid; + +#[inline(always)] +pub fn vector_dot_product(v1: [f32; 2], v2: [f32; 2]) -> f32 { + v1[0] * v2[0] + v1[1] * v2[1] +} + +pub fn get_value_at_point(point: (usize, usize), grid: &Grid, screen_size: (usize, usize)) -> f32 { + let translated_point = ( + (point.0 * (grid.get_size().0 - 1)) as f32 / (screen_size.0 - 1) as f32, + (point.1 * (grid.get_size().1 - 1)) as f32 / (screen_size.1 - 1) as f32, + ); + + let local_pos = ( + translated_point.0 - translated_point.0.floor(), + translated_point.1 - translated_point.1.floor(), + ); + + // println!("{:?}", point); + + let vecs = [ + [ + grid.get_vec(( + translated_point.0.floor() as usize, + translated_point.1.floor() as usize, + )), + grid.get_vec(( + translated_point.0.ceil() as usize, + translated_point.1.floor() as usize, + )), + ], + [ + grid.get_vec(( + translated_point.0.floor() as usize, + translated_point.1.ceil() as usize, + )), + grid.get_vec(( + translated_point.0.ceil() as usize, + translated_point.1.ceil() as usize, + )), + ], + ]; + // println!("{:?}", local_pos); + let off_vecs = get_off_vecs(local_pos); + // println!("{:?}", off_vecs); + let dot_prods = get_dot_prods(vecs, off_vecs); + interpolate(dot_prods, local_pos) +} + +pub fn get_off_vecs(point: (f32, f32)) -> [[[f32; 2]; 2]; 2] { + // let mut off_vecs = [[[0.0; 2]; 2]; 2]; + // for i in 0..vecs.len() { + // for j in 0..vecs[i].len() { + // off_vecs[i][j] = [vecs[i][j][0] - point.0, vecs[i][j][1] - point.1]; + // } + // } + // off_vecs + + [ + [[point.0, point.1], [point.0 + -1.0, point.1]], + [[point.0, point.1 + -1.0], [point.0 + -1.0, point.1 + -1.0]], + ] +} + +pub fn get_dot_prods(vecs: [[[f32; 2]; 2]; 2], off_vecs: [[[f32; 2]; 2]; 2]) -> [[f32; 2]; 2] { + let mut dot_prods = [[0.0; 2]; 2]; + for i in 0..vecs.len() { + for j in 0..vecs[i].len() { + dot_prods[i][j] = vector_dot_product(vecs[i][j], off_vecs[i][j]); + } + } + dot_prods +} + +pub fn interpolate(dot_prods: [[f32; 2]; 2], point: (f32, f32)) -> f32 { + let int_1 = interpolate_2(dot_prods[0][0], dot_prods[0][1], point.0); + let int_2 = interpolate_2(dot_prods[1][0], dot_prods[1][1], point.0); + interpolate_2(int_1, int_2, point.1) +} + +// Smootherstep https://en.wikipedia.org/wiki/Smoothstep +fn interpolate_2(a0: f32, a1: f32, x: f32) -> f32 { + let res = (a1 - a0) * ((x * (x * 6.0 - 15.0) + 10.0) * x * x * x) + a0; + match (res < -1.0, res > 1.0) { + (true, _) => -1.0, + (_, true) => 1.0, + _ => res, + } +}