From d494adf42e02a420d223f5db096337a48d6b7895 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 13 Feb 2024 02:14:27 -0800 Subject: [PATCH] implement texture_loader --- Cargo.lock | 14 ++++++++++++-- Cargo.toml | 5 +++-- src/graphics.rs | 10 +++++----- src/graphics_worker.rs | 6 +++--- src/physics_worker.rs | 10 +++++----- src/window.rs | 19 +++++++++++++++---- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d5ee2c..6d317d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1515,6 +1515,7 @@ dependencies = [ "pollster", "strafesnet_common", "strafesnet_rbx_loader", + "strafesnet_texture_loader", "wgpu", "winit", ] @@ -1522,7 +1523,7 @@ dependencies = [ [[package]] name = "strafesnet_common" version = "0.1.0" -source = "git+https://git.itzana.me/StrafesNET/common?rev=43f771fb0f19a3751875df1f25bf9cb5874f75f4#43f771fb0f19a3751875df1f25bf9cb5874f75f4" +source = "git+https://git.itzana.me/StrafesNET/common?rev=fccb13bc6080ea6db94ef9175affd4aef1d9249d#fccb13bc6080ea6db94ef9175affd4aef1d9249d" dependencies = [ "glam", "id", @@ -1531,7 +1532,7 @@ dependencies = [ [[package]] name = "strafesnet_rbx_loader" version = "0.1.0" -source = "git+https://git.itzana.me/StrafesNET/rbx_loader?rev=5c9a79390a36ca33e2fe64d9a59da4a98f77662c#5c9a79390a36ca33e2fe64d9a59da4a98f77662c" +source = "git+https://git.itzana.me/StrafesNET/rbx_loader?rev=05e609521a31d14ca2c9def7a778d792022571dc#05e609521a31d14ca2c9def7a778d792022571dc" dependencies = [ "glam", "lazy-regex", @@ -1542,6 +1543,15 @@ dependencies = [ "strafesnet_common", ] +[[package]] +name = "strafesnet_texture_loader" +version = "0.1.0" +source = "git+https://git.itzana.me/StrafesNET/texture_loader?rev=94e951ce06fb30873964ec29f30fcf9c8247a9a5#94e951ce06fb30873964ec29f30fcf9c8247a9a5" +dependencies = [ + "lazy-regex", + "strafesnet_common", +] + [[package]] name = "strict-num" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index 7f066a3..13ce822 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,9 @@ glam = "0.25.0" id = { git = "https://git.itzana.me/Quaternions/id", rev = "1f710976cc786c8853dab73d6e1cee53158deeb0" } parking_lot = "0.12.1" pollster = "0.3.0" -strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "43f771fb0f19a3751875df1f25bf9cb5874f75f4" } -strafesnet_rbx_loader = { git = "https://git.itzana.me/StrafesNET/rbx_loader", rev = "5c9a79390a36ca33e2fe64d9a59da4a98f77662c" } +strafesnet_rbx_loader = { git = "https://git.itzana.me/StrafesNET/rbx_loader", rev = "05e609521a31d14ca2c9def7a778d792022571dc" } +strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "fccb13bc6080ea6db94ef9175affd4aef1d9249d" } +strafesnet_texture_loader = { git = "https://git.itzana.me/StrafesNET/texture_loader", rev = "94e951ce06fb30873964ec29f30fcf9c8247a9a5", features = ["legacy"] } wgpu = "0.19.0" winit = "0.29.2" diff --git a/src/graphics.rs b/src/graphics.rs index 44d3d7b..e3ee6ad 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -147,10 +147,9 @@ impl GraphicsState{ pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){ self.camera.fov=user_settings.calculate_fov(1.0,&self.camera.screen_size).as_vec2(); } - pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap){ + pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap,textures:strafesnet_texture_loader::texture_loader::Textures){ //generate texture view per texture - let num_textures=map.textures.len(); - let texture_views:Vec=map.textures.iter().enumerate().map(|(texture_id,texture_data)|{ + let texture_views:Vec=textures.into_iter().map(|(texture_id,texture_data)|{ let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).unwrap(); let (mut width,mut height)=(image.get_width(),image.get_height()); @@ -187,18 +186,19 @@ impl GraphicsState{ dimension:wgpu::TextureDimension::D2, format, usage:wgpu::TextureUsages::TEXTURE_BINDING|wgpu::TextureUsages::COPY_DST, - label:Some(format!("Texture{}",texture_id).as_str()), + label:Some(format!("Texture{}",texture_id.get()).as_str()), view_formats:&[], }, wgpu::util::TextureDataOrder::LayerMajor, &image.data, ); texture.create_view(&wgpu::TextureViewDescriptor{ - label:Some(format!("Texture{} View",texture_id).as_str()), + label:Some(format!("Texture{} View",texture_id.get()).as_str()), dimension:Some(wgpu::TextureViewDimension::D2), ..wgpu::TextureViewDescriptor::default() }) }).collect(); + let num_textures=texture_views.len(); //split groups with different textures into separate models //the models received here are supposed to be tightly packed,i.e. no code needs to check if two models are using the same groups. diff --git a/src/graphics_worker.rs b/src/graphics_worker.rs index b8fad9a..4f7e8d9 100644 --- a/src/graphics_worker.rs +++ b/src/graphics_worker.rs @@ -4,7 +4,7 @@ pub enum Instruction{ Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2), //UpdateModel(crate::graphics::GraphicsModelUpdate), Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), - GenerateModels(strafesnet_common::map::CompleteMap), + GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_texture_loader::texture_loader::Textures), ClearModels, } @@ -27,8 +27,8 @@ pub fn new<'a>( let mut resize=None; crate::compat_worker::INWorker::new(move |ins:Instruction|{ match ins{ - Instruction::GenerateModels(indexed_model_instances)=>{ - graphics.generate_models(&device,&queue,&indexed_model_instances); + Instruction::GenerateModels(map,textures)=>{ + graphics.generate_models(&device,&queue,&map,textures); }, Instruction::ClearModels=>{ graphics.clear(); diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 8206d1e..d17259f 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -18,7 +18,7 @@ pub enum Instruction{ Input(InputInstruction), Render, Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), - GenerateModels(strafesnet_common::map::CompleteMap), + GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_texture_loader::texture_loader::Textures), ClearModels, //Graphics(crate::graphics_worker::Instruction), } @@ -63,7 +63,7 @@ pub enum Instruction{ &InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)), InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset), }, - Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), + Instruction::GenerateModels(_,_)=>Some(PhysicsInputInstruction::Idle), Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), Instruction::Render=>Some(PhysicsInputInstruction::Idle), @@ -118,10 +118,10 @@ pub enum Instruction{ Instruction::Resize(size,user_settings)=>{ graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); }, - Instruction::GenerateModels(indexed_model_instances)=>{ - physics.generate_models(&indexed_model_instances); + Instruction::GenerateModels(map,textures)=>{ + physics.generate_models(&map); physics.spawn(); - graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(indexed_model_instances)).unwrap(); + graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map,textures)).unwrap(); }, Instruction::ClearModels=>{ physics.state.clear(); diff --git a/src/window.rs b/src/window.rs index e9629c4..b00b74e 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,6 +1,7 @@ use crate::physics_worker::InputInstruction; use strafesnet_common::integer; use strafesnet_common::instruction::TimedInstruction; +use strafesnet_texture_loader::texture_loader::TextureLoaderTrait; pub enum WindowInstruction{ Resize(winit::dpi::PhysicalSize), @@ -41,12 +42,22 @@ impl WindowContext<'_>{ // Ok(strafesnet_snf::SNF::Demo(streamable_map))=>println!("File type not yet supported"), // Err(e)=>println!("Error reading file: {e:?}"), // } - match strafesnet_rbx_loader::read(file){ - Ok(map)=>{ + let mut texture_loader=strafesnet_texture_loader::legacy(); + match (strafesnet_rbx_loader::read(file,|name|{ + match texture_loader.acquire_id(name){ + Ok(texture_id)=>Some(texture_id), + Err(e)=>{ + println!("there was error: {e}"); + None + }, + } + }),texture_loader.load()){ + (Ok(map),Ok(textures))=>{ self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap(); - self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map)}).unwrap(); + self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map,textures)}).unwrap(); }, - Err(e)=>println!("Error reading file: {e:?}"), + (Err(e),_)=>println!("Error reading file: {e:?}"), + (_,Err(e))=>println!("Error loading textures: {e:?}"), } }else{ println!("Failed to open file {path:?}");