From 480cd0e3be5276a1a08b2849c5e9baad6e4a0d4e Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 29 Jan 2024 16:19:41 -0800 Subject: [PATCH] commonize --- Cargo.lock | 9 + Cargo.toml | 1 + src/aabb.rs | 46 -- src/bvh.rs | 123 ----- src/face_crawler.rs | 4 +- src/graphics.rs | 5 +- src/graphics_worker.rs | 4 +- src/instruction.rs | 53 -- src/integer.rs | 1054 ---------------------------------------- src/load_bsp.rs | 16 +- src/load_roblox.rs | 2 +- src/main.rs | 7 +- src/model.rs | 2 +- src/model_physics.rs | 29 +- src/physics.rs | 52 +- src/physics_worker.rs | 4 +- src/primitives.rs | 2 +- src/settings.rs | 2 +- src/setup.rs | 5 +- src/window.rs | 7 +- src/worker.rs | 8 +- src/zeroes.rs | 40 -- 22 files changed, 88 insertions(+), 1387 deletions(-) delete mode 100644 src/aabb.rs delete mode 100644 src/bvh.rs delete mode 100644 src/instruction.rs delete mode 100644 src/integer.rs delete mode 100644 src/zeroes.rs diff --git a/Cargo.lock b/Cargo.lock index c4586ec9..70a6c927 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1648,12 +1648,21 @@ dependencies = [ "rbx_dom_weak", "rbx_reflection_database", "rbx_xml", + "strafesnet_common", "vbsp", "vmdl", "wgpu", "winit", ] +[[package]] +name = "strafesnet_common" +version = "0.1.0" +source = "git+https://git.itzana.me/StrafesNET/common?rev=434ca29aef7e3015c9ca1ed45de8fef42e33fdfb#434ca29aef7e3015c9ca1ed45de8fef42e33fdfb" +dependencies = [ + "glam", +] + [[package]] name = "strict-num" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index ee74c94e..02d986d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ rbx_binary = "0.7.1" rbx_dom_weak = "2.5.0" rbx_reflection_database = "0.2.7" rbx_xml = "0.13.1" +strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "434ca29aef7e3015c9ca1ed45de8fef42e33fdfb" } vbsp = "0.5.0" vmdl = "0.1.1" wgpu = "0.19.0" diff --git a/src/aabb.rs b/src/aabb.rs deleted file mode 100644 index 65e783f4..00000000 --- a/src/aabb.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::integer::Planar64Vec3; - -#[derive(Clone)] -pub struct Aabb{ - min:Planar64Vec3, - max:Planar64Vec3, -} - -impl Default for Aabb { - fn default()->Self { - Self{min:Planar64Vec3::MAX,max:Planar64Vec3::MIN} - } -} - -impl Aabb{ - pub fn grow(&mut self,point:Planar64Vec3){ - self.min=self.min.min(point); - self.max=self.max.max(point); - } - pub fn join(&mut self,aabb:&Aabb){ - self.min=self.min.min(aabb.min); - self.max=self.max.max(aabb.max); - } - pub fn inflate(&mut self,hs:Planar64Vec3){ - self.min-=hs; - self.max+=hs; - } - pub fn intersects(&self,aabb:&Aabb)->bool{ - (self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all() - } - pub fn size(&self)->Planar64Vec3{ - self.max-self.min - } - pub fn center(&self)->Planar64Vec3{ - self.min.midpoint(self.max) - } - //probably use floats for area & volume because we don't care about precision - // pub fn area_weight(&self)->f32{ - // let d=self.max-self.min; - // d.x*d.y+d.y*d.z+d.z*d.x - // } - // pub fn volume(&self)->f32{ - // let d=self.max-self.min; - // d.x*d.y*d.z - // } -} \ No newline at end of file diff --git a/src/bvh.rs b/src/bvh.rs deleted file mode 100644 index f4c9bc6c..00000000 --- a/src/bvh.rs +++ /dev/null @@ -1,123 +0,0 @@ -use crate::aabb::Aabb; - -//da algaritum -//lista boxens -//sort by {minx,maxx,miny,maxy,minz,maxz} (6 lists) -//find the sets that minimizes the sum of surface areas -//splitting is done when the minimum split sum of surface areas is larger than the node's own surface area - -//start with bisection into octrees because a bad bvh is still 1000x better than no bvh -//sort the centerpoints on each axis (3 lists) -//bv is put into octant based on whether it is upper or lower in each list -enum BvhNodeContent{ - Branch(Vec), - Leaf(usize), -} -impl Default for BvhNodeContent{ - fn default()->Self{ - Self::Branch(Vec::new()) - } -} -#[derive(Default)] -pub struct BvhNode{ - content:BvhNodeContent, - aabb:Aabb, -} - -impl BvhNode{ - pub fn the_tester(&self,aabb:&Aabb,f:&mut F){ - match &self.content{ - &BvhNodeContent::Leaf(model)=>f(model), - BvhNodeContent::Branch(children)=>for child in children{ - //this test could be moved outside the match statement - //but that would test the root node aabb - //you're probably not going to spend a lot of time outside the map, - //so the test is extra work for nothing - if aabb.intersects(&child.aabb){ - child.the_tester(aabb,f); - } - }, - } - } -} - -pub fn generate_bvh(boxen:Vec)->BvhNode{ - generate_bvh_node(boxen.into_iter().enumerate().collect()) -} - -fn generate_bvh_node(boxen:Vec<(usize,Aabb)>)->BvhNode{ - let n=boxen.len(); - if n<20{ - let mut aabb=Aabb::default(); - let nodes=boxen.into_iter().map(|b|{ - aabb.join(&b.1); - BvhNode{ - content:BvhNodeContent::Leaf(b.0), - aabb:b.1, - } - }).collect(); - BvhNode{ - content:BvhNodeContent::Branch(nodes), - aabb, - } - }else{ - let mut octant=std::collections::HashMap::with_capacity(n);//this ids which octant the boxen is put in - let mut sort_x=Vec::with_capacity(n); - let mut sort_y=Vec::with_capacity(n); - let mut sort_z=Vec::with_capacity(n); - for (i,aabb) in boxen.iter(){ - let center=aabb.center(); - octant.insert(*i,0); - sort_x.push((*i,center.x())); - sort_y.push((*i,center.y())); - sort_z.push((*i,center.z())); - } - sort_x.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1)); - sort_y.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1)); - sort_z.sort_by(|tup0,tup1|tup0.1.cmp(&tup1.1)); - let h=n/2; - let median_x=sort_x[h].1; - let median_y=sort_y[h].1; - let median_z=sort_z[h].1; - for (i,c) in sort_x{ - if median_x{ Miss, diff --git a/src/graphics.rs b/src/graphics.rs index 8165e30f..0a0be3ac 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use strafesnet_common::integer; use wgpu::{util::DeviceExt,AstcBlock,AstcChannel}; use crate::model_graphics::{GraphicsVertex,GraphicsModelColor4,GraphicsModelInstance,GraphicsModelSingleTexture,IndexedGraphicsModelSingleTexture,IndexedGroupFixedTexture}; @@ -833,7 +834,7 @@ impl GraphicsState{ }); let camera=GraphicsCamera::default(); - let camera_uniforms = camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(glam::IVec2::ZERO,crate::integer::Time::ZERO)); + let camera_uniforms = camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(glam::IVec2::ZERO,integer::Time::ZERO)); let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Camera"), contents: bytemuck::cast_slice(&camera_uniforms), @@ -902,7 +903,7 @@ impl GraphicsState{ device:&wgpu::Device, queue:&wgpu::Queue, physics_output:crate::physics::PhysicsOutputState, - predicted_time:crate::integer::Time, + predicted_time:integer::Time, mouse_pos:glam::IVec2, ) { //TODO: use scheduled frame times to create beautiful smoothing simulation physics extrapolation assuming no input diff --git a/src/graphics_worker.rs b/src/graphics_worker.rs index b190bb3d..378269c2 100644 --- a/src/graphics_worker.rs +++ b/src/graphics_worker.rs @@ -1,5 +1,7 @@ +use strafesnet_common::integer; + pub enum Instruction{ - Render(crate::physics::PhysicsOutputState,crate::integer::Time,glam::IVec2), + Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2), //UpdateModel(crate::graphics::GraphicsModelUpdate), Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), GenerateModels(crate::model::IndexedModelInstances), diff --git a/src/instruction.rs b/src/instruction.rs deleted file mode 100644 index 08a27bb0..00000000 --- a/src/instruction.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::integer::Time; - -#[derive(Debug)] -pub struct TimedInstruction{ - pub time:Time, - pub instruction:I, -} - -pub trait InstructionEmitter{ - fn next_instruction(&self,time_limit:Time)->Option>; -} -pub trait InstructionConsumer{ - fn process_instruction(&mut self, instruction:TimedInstruction); -} - -//PROPER PRIVATE FIELDS!!! -pub struct InstructionCollector{ - time:Time, - instruction:Option, -} -impl InstructionCollector{ - pub fn new(time:Time)->Self{ - Self{ - time, - instruction:None - } - } - #[inline] - pub fn time(&self)->Time{ - self.time - } - pub fn collect(&mut self,instruction:Option>){ - match instruction{ - Some(unwrap_instruction)=>{ - if unwrap_instruction.time(), - } - } - pub fn instruction(self)->Option>{ - //STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR - match self.instruction{ - Some(instruction)=>Some(TimedInstruction{ - time:self.time, - instruction - }), - None=>None, - } - } -} \ No newline at end of file diff --git a/src/integer.rs b/src/integer.rs deleted file mode 100644 index b04376a6..00000000 --- a/src/integer.rs +++ /dev/null @@ -1,1054 +0,0 @@ -//integer units -#[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)] -pub struct Time(i64); -impl Time{ - pub const MIN:Self=Self(i64::MIN); - pub const MAX:Self=Self(i64::MAX); - pub const ZERO:Self=Self(0); - pub const ONE_SECOND:Self=Self(1_000_000_000); - pub const ONE_MILLISECOND:Self=Self(1_000_000); - pub const ONE_MICROSECOND:Self=Self(1_000); - pub const ONE_NANOSECOND:Self=Self(1); - #[inline] - pub fn from_secs(num:i64)->Self{ - Self(Self::ONE_SECOND.0*num) - } - #[inline] - pub fn from_millis(num:i64)->Self{ - Self(Self::ONE_MILLISECOND.0*num) - } - #[inline] - pub fn from_micros(num:i64)->Self{ - Self(Self::ONE_MICROSECOND.0*num) - } - #[inline] - pub fn from_nanos(num:i64)->Self{ - Self(Self::ONE_NANOSECOND.0*num) - } - //should I have checked subtraction? force all time variables to be positive? - #[inline] - pub fn nanos(&self)->i64{ - self.0 - } -} -impl From for Time{ - #[inline] - fn from(value:Planar64)->Self{ - Time((((value.0 as i128)*1_000_000_000)>>32) as i64) - } -} -impl std::fmt::Display for Time{ - #[inline] - fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ - write!(f,"{}s+{:09}ns",self.0/Self::ONE_SECOND.0,self.0%Self::ONE_SECOND.0) - } -} -impl std::default::Default for Time{ - fn default()->Self{ - Self(0) - } -} -impl std::ops::Neg for Time{ - type Output=Time; - #[inline] - fn neg(self)->Self::Output { - Time(-self.0) - } -} -impl std::ops::Add