diff --git a/Cargo.lock b/Cargo.lock index 4ecc5ba..60a47a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,10 +6,11 @@ version = 4 name = "RaylibRustPhysics" version = "0.1.0" dependencies = [ + "clearscreen", "glam 0.27.0", "macroquad", "nalgebra", - "rand", + "rand 0.9.2", "rapier3d", "strum", ] @@ -83,6 +84,25 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "clearscreen" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85a8ab73a1c02b0c15597b22e09c7dc36e63b2f601f9d1e83ac0c3decd38b1ae" +dependencies = [ + "nix", + "terminfo", + "thiserror", + "which", + "windows-sys 0.59.0", +] + [[package]] name = "color_quant" version = "1.1.0" @@ -119,12 +139,28 @@ dependencies = [ "log", ] +[[package]] +name = "env_home" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" + [[package]] name = "equivalent" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "fdeflate" version = "0.3.7" @@ -144,6 +180,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foldhash" version = "0.1.5" @@ -344,6 +386,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + [[package]] name = "log" version = "0.4.28" @@ -389,6 +437,18 @@ dependencies = [ "rawpointer", ] +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniquad" version = "0.4.8" @@ -460,6 +520,28 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121" +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -573,6 +655,44 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "phf" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_codegen" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared", + "rand 0.8.5", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + [[package]] name = "png" version = "0.17.16" @@ -644,6 +764,15 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.9.2" @@ -651,7 +780,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" dependencies = [ "rand_chacha", - "rand_core", + "rand_core 0.9.3", ] [[package]] @@ -661,9 +790,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.9.3", ] +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "rand_core" version = "0.9.3" @@ -728,6 +863,19 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" +[[package]] +name = "rustix" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +dependencies = [ + "bitflags 2.10.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + [[package]] name = "safe_arch" version = "0.7.4" @@ -756,6 +904,12 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + [[package]] name = "slab" version = "0.4.11" @@ -824,6 +978,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "terminfo" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4ea810f0692f9f51b382fff5893887bb4580f5fa246fde546e0b13e7fcee662" +dependencies = [ + "fnv", + "nom", + "phf", + "phf_codegen", +] + [[package]] name = "thiserror" version = "2.0.17" @@ -877,6 +1043,17 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "which" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fabb953106c3c8eea8306e4393700d7657561cb43122571b172bbfb7c7ba1d" +dependencies = [ + "env_home", + "rustix", + "winsafe", +] + [[package]] name = "wide" version = "0.7.33" @@ -909,6 +1086,100 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" + [[package]] name = "wit-bindgen" version = "0.46.0" diff --git a/Cargo.toml b/Cargo.toml index 1c884a6..b5d8d5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ nalgebra = {version="0.34", features = ["convert-glam027"]} glam = "0.27" rand = "0.9.2" strum = { version = "0.27", features = ["derive"] } +clearscreen = "4.0.2" [[bin]] name = "tools" diff --git a/src/camera.rs b/src/camera.rs index acf8ff5..1aed8f6 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,7 +1,7 @@ use macroquad::prelude::*; const MOVE_SPEED: f32 = 1.0; -const LOOK_SPEED: f32 = 0.01; +const LOOK_SPEED: f32 = 0.1; pub struct FirstPersonCamera { pub position: Vec3, diff --git a/src/drone.rs b/src/drone.rs index c921d1a..8780f42 100644 --- a/src/drone.rs +++ b/src/drone.rs @@ -1,20 +1,27 @@ use nalgebra as na; use rapier3d::prelude as rp; -use crate::engine::World; +use crate::engine::{ColliderExtraData, World}; -mod controller; +pub mod controller; use controller::*; pub struct MotorCharacteristics { - relative_motor_positions: [na::OPoint>; 4], - max_thrust: f32, - max_torque: f32, + pub relative_motor_positions: [na::OPoint>; 4], + pub max_thrust: f32, + pub max_torque: f32, } impl Default for MotorCharacteristics { fn default() -> Self { Self { + /* + * Motor position indices + * 1 --- 0 + * | | + * | | + * 2 --- 3 + */ relative_motor_positions: [ rp::point![5.0, 3.0, 5.0], rp::point![-5.0, 3.0, 5.0], @@ -27,8 +34,8 @@ impl Default for MotorCharacteristics { } } -struct Drone { - rb_handle: rp::RigidBodyHandle, +pub struct Drone { + pub rb_handle: rp::RigidBodyHandle, motor_characteristics: MotorCharacteristics, pub controller: Box, } @@ -38,23 +45,36 @@ impl Drone { world: &mut World, controller: Box, motor_characteristics: MotorCharacteristics, + mass: f32, ) -> Drone { let drone_rb_handle = world.register_body( rp::RigidBodyBuilder::dynamic() .translation(rp::vector![0.0, 10.0, 0.0]) - .rotation(rp::vector![0.6, 0.0, 0.0]) - .linear_damping(0.1) - .angular_damping(0.1) + .rotation(rp::vector![0.0, 0.0, 0.0]) + /* + * These damping values keep the simulation more realistic, + * They act as air resistance + * + * Values are kind of random for now. Calculating them requires the final model + * A Poor Man's fluid simulation :D + */ + .linear_damping(0.1) // Damps velocity slowly + .angular_damping(0.1) // Damps angular velocity slowly .build(), ); world.register_collider( rp::ColliderBuilder::cuboid(5.0, 0.5, 5.0) + .mass(mass) .restitution(0.3) .build(), drone_rb_handle, - None, + Some(ColliderExtraData { + color: macroquad::color::WHITE, + }), ); + // Should only need to be called once? + // No one should be changing motor characteristics during flight time. controller.set_motor_characteristics(&motor_characteristics); return Drone { @@ -63,9 +83,59 @@ impl Drone { motor_characteristics: motor_characteristics, }; } - pub fn update_controller(&mut self, world: &World, tick: u64) { + + fn apply_throttles(&mut self, world: &mut World) { + let throttles = self.controller.get_motor_throttles(); + + let drone_rb = world.bodies.get_mut(self.rb_handle).unwrap(); + + /* + * Clear all the previously applied forces and torques, or theyll add-up every tick + */ + drone_rb.reset_forces(true); + drone_rb.reset_torques(true); + + for (i, motor_position) in self + .motor_characteristics + .relative_motor_positions + .iter() + .enumerate() + { + let throttle = throttles[i].clamp(0.0, 1.0); + let thrust = self.motor_characteristics.max_thrust * throttle; + let torque = self.motor_characteristics.max_torque * throttle; + + // Thrust is applied upward at motor position. + // + // Force calculated with (0.0, thrust, 0.0) points to World Up, + // Apply RB's rotation to point at RB's Up + // + // motor_position is relative, transform it by the RB's position first + let mut thrust_force = nalgebra::Vector3::new(0.0, thrust, 0.0); + thrust_force = drone_rb.rotation().transform_vector(&thrust_force); + drone_rb.add_force_at_point( + thrust_force, + drone_rb.position().transform_point(motor_position), + true, + ); + + let torque = if i % 2 == 0 { + rp::vector![0.0, torque, 0.0] + } else { + rp::vector![0.0, -torque, 0.0] + }; + drone_rb.add_torque(torque, true); + } + } + + fn update_controller(&mut self, world: &World) { let rb = world.bodies.get(self.rb_handle).unwrap(); self.controller.set_position(*rb.position()); - self.controller.set_time(tick as f32 / 60.0); + self.controller.set_time(world.get_time()); + } + + pub fn process_tick(&mut self, world: &mut World) { + self.update_controller(world); + self.apply_throttles(world); } } diff --git a/src/drone/controller.rs b/src/drone/controller.rs index b00993c..a251ba0 100644 --- a/src/drone/controller.rs +++ b/src/drone/controller.rs @@ -4,24 +4,38 @@ use std::any::Any; use crate::drone::MotorCharacteristics; pub trait DroneController { + // Allow downcast of trait -> class. + // + // This gives us the ability to have a Box + // And transform it into a pointer to its class. fn as_any(&self) -> &dyn Any; - fn as_mut_any(&mut self) -> &mut dyn Any; + /* + * Methods called by Drone, to transmit information to the controller. + */ fn set_position(&self, position: rp::Isometry); fn set_time(&self, time: f32); fn set_motor_characteristics(&self, motor_characteristics: &MotorCharacteristics); - fn get_motor_speeds(&self) -> [f32; 4]; + /* + * Throttle should be between 0 and 1. Values will be by the Drone class. + */ + fn get_motor_throttles(&self) -> [f32; 4]; } -struct DefaultController { - motor_speed: f32, +/* +* DefaultController just sets throttle to motor_throttle, a parameter passed to it on its contructor +*/ +pub struct DefaultController { + motor_throttle: f32, } impl DefaultController { pub fn new(motor_speed: f32) -> DefaultController { - return DefaultController { motor_speed }; + return DefaultController { + motor_throttle: motor_speed, + }; } } @@ -29,9 +43,14 @@ impl DroneController for DefaultController { fn set_position(&self, _position: rp::Isometry) {} fn set_time(&self, _time: f32) {} fn set_motor_characteristics(&self, _motor_characteristics: &MotorCharacteristics) {} - fn get_motor_speeds(&self) -> [f32; 4] { - return [self.motor_speed; 4]; + fn get_motor_throttles(&self) -> [f32; 4] { + return [self.motor_throttle; 4]; } + + /* + * These should be implemented like this for all the classes that implement DroneController + * Works as an example implementation + */ fn as_any(&self) -> &dyn Any { self } diff --git a/src/engine.rs b/src/engine.rs index fa7a3be..bcc04e5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -29,6 +29,7 @@ pub struct World { pub bodies: rp::RigidBodySet, pub colliders: rp::ColliderSet, pub collider_data: HashMap, + pub tick: u64, } pub struct ColliderExtraData { @@ -51,6 +52,11 @@ impl World { self.hooks.as_ref(), self.events.as_ref(), ); + self.tick += 1; + } + + pub fn get_time(&self) -> f32 { + return self.integration_parameters.dt * (self.tick as f32); } pub fn clear_ofb(&mut self) { @@ -140,6 +146,7 @@ impl Default for World { bodies: rp::RigidBodySet::new(), colliders: rp::ColliderSet::new(), collider_data: HashMap::new(), + tick: 0, }; } } diff --git a/src/main.rs b/src/main.rs index 8c2b521..92c1e0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,20 @@ mod graphics_util; use crate::rendering::Renderer; -#[macroquad::main("3D")] +fn window_conf() -> mq::Conf { + mq::Conf { + window_title: "RustDroneSim".to_owned(), + window_resizable: true, + // fullscreen: true, + platform: mq::miniquad::conf::Platform { + linux_backend: mq::miniquad::conf::LinuxBackend::WaylandOnly, + ..Default::default() + }, + ..Default::default() + } +} + +#[macroquad::main(window_conf)] async fn main() { let mut world = World::default(); let mut renderer = Renderer::new(); @@ -21,19 +34,25 @@ async fn main() { vector![70.0, 150.0, -90.0].into(), vector![-0.4, -0.7, 0.6].into(), ); + renderer.update_light(&world); - // Physics Initialization + // Ground collider world.register_free_collider( ColliderBuilder::cuboid(250.0, 5.0, 250.0) .restitution(0.5) .build(), None, ); - // add_objects(&mut world); - // - renderer.update_light(&world); - let mut tick: u64 = 0; + let mut drone = drone::Drone::new( + &mut world, + Box::new(drone::controller::DefaultController::new(1.0)), + drone::MotorCharacteristics { + max_thrust: 2.0 * 3.5 * 0.25, + ..Default::default() + }, + 0.350, + ); loop { renderer.update_camera(); @@ -55,40 +74,23 @@ async fn main() { renderer.apply_config(); } - // Physics Simulation - // { - // let drone_rb = world.bodies.get_mut(drone_rb_handle).unwrap(); - // drone_rb.reset_forces(true); - // drone_rb.reset_torques(true); - // for (i, pos) in motor_positions.iter().enumerate() { - // // Thrust is applied upward at motor position - // let mut force = nalgebra::Vector3::new(0.0, drone_rb.mass() * 2.8, 0.0); - // - // // drone_rb.reset_forces(true); - // - // force = drone_rb.rotation().transform_vector(&force); - // drone_rb.add_force_at_point(force, drone_rb.position().transform_point(&pos), true); - // // drone_rb.apply_force_at_point(force, *pos, true); - // - // // let torque = if i % 2 == 0 { - // // vector![0.0, 0.5, 0.0] - // // } else { - // // vector![0.0, 0.5, 0.0] - // // }; - // // drone_rb.add_torque(torque, true); - // // println!("{:}", drone_rb.translation()); - // } - // } - + // Physics world.step(); + drone.process_tick(&mut world); + let _ = clearscreen::clear(); + println!( + "{:}", + world.bodies.get(drone.rb_handle).unwrap().translation() + ); + println!("{:?}", drone.controller.get_motor_throttles()); + // Rendering renderer.draw(&mut world); - if tick % 30 == 0 { + if world.tick % 30 == 0 { world.clear_ofb(); renderer.update_light(&world); } - tick += 1; mq::next_frame().await; } diff --git a/src/rendering.rs b/src/rendering.rs index 305bb13..4bf39e7 100644 --- a/src/rendering.rs +++ b/src/rendering.rs @@ -10,6 +10,7 @@ use rapier3d::prelude::*; use strum::IntoEnumIterator; mod ui; +const DEBUG_UI: bool = false; const DEPTH_MAP_SIZE: u32 = 1024; const LIGHT_PROJECTION_SIZE: f32 = 40.0; @@ -274,25 +275,28 @@ impl Renderer { } fn render_ui(&mut self) { mq::set_default_camera(); - mq::draw_texture_ex( - &self.depth_target.texture, - 320., - 20., - mq::WHITE, - mq::DrawTextureParams { - flip_y: true, - dest_size: Some(mq::vec2(200.0, 200.0)), - ..Default::default() - }, - ); - macroquad::ui::root_ui().window( - hash!("MaterialWindow"), - mq::vec2(20.0, 20.0), - mq::vec2(300.0, 300.0), - |ui| { - self.ui_state.ui(ui); - }, - ); + + if DEBUG_UI { + mq::draw_texture_ex( + &self.depth_target.texture, + 320., + 20., + mq::WHITE, + mq::DrawTextureParams { + flip_y: true, + dest_size: Some(mq::vec2(200.0, 200.0)), + ..Default::default() + }, + ); + macroquad::ui::root_ui().window( + hash!("MaterialWindow"), + mq::vec2(20.0, 20.0), + mq::vec2(300.0, 300.0), + |ui| { + self.ui_state.ui(ui); + }, + ); + } } pub fn draw(&mut self, mut world: &mut World) { if self.enable_shadows { diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 2297094..e9dcb1b 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -58,11 +58,11 @@ float calculate_shadow() { { for (int y = -range; y <= range; y++) { - float pcfDepth = texture(shadow_map, proj_coords.xy + (vec2(x, y) * texelSize) * shadow_step_size).r; + float pcfDepth = texture(shadow_map, proj_coords.xy + (vec2(float(x), float(y)) * texelSize) * shadow_step_size).r; shadow += current_depth - bias > pcfDepth ? 0.0 : 1.0; } } - shadow /= shadow_step_count * shadow_step_count; + shadow /= float(shadow_step_count * shadow_step_count); return shadow; } @@ -74,7 +74,7 @@ void main() { app_color = normalize(abs(v_normal)); } else { - app_color = color / 256; + app_color = color / 256.0; } if (render_shadows_bool == 1) { diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index 79fb790..d1e09a4 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -1,9 +1,9 @@ #version 330 core precision highp float; -attribute vec3 position; -attribute vec4 color0; -attribute vec4 normal; +in vec3 position; +in vec4 color0; +in vec4 normal; uniform mat4 Model; uniform mat4 Projection;