wow it's wrong again

This commit is contained in:
Quaternions 2024-07-26 17:26:57 -07:00
parent 931cef52b1
commit a00b6a63c9

View File

@ -14,6 +14,7 @@ pub enum Error{
InvalidHeader(binrw::Error), InvalidHeader(binrw::Error),
InvalidBlockId(BlockId), InvalidBlockId(BlockId),
InvalidMeshId(model::MeshId), InvalidMeshId(model::MeshId),
InvalidModelId(model::ModelId),
InvalidTextureId(model::TextureId), InvalidTextureId(model::TextureId),
InvalidData(binrw::Error), InvalidData(binrw::Error),
IO(std::io::Error), IO(std::io::Error),
@ -168,18 +169,20 @@ struct Region{
//consider including a bvh in the region instead of needing to rebalance the physics bvh on the fly //consider including a bvh in the region instead of needing to rebalance the physics bvh on the fly
model_count:u32, model_count:u32,
#[br(count=model_count)] #[br(count=model_count)]
models:Vec<newtypes::model::Model>, models:Vec<(u32,newtypes::model::Model)>,
} }
//code deduplication reused in into_complete_map //code deduplication reused in into_complete_map
fn read_region<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<model::Model>,Error>{ fn read_region<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<(model::ModelId,model::Model)>,Error>{
//load region from disk //load region from disk
//parse the models and determine what resources need to be loaded //parse the models and determine what resources need to be loaded
//load resources into self.resources //load resources into self.resources
//return Region //return Region
let mut block=file.block_reader(block_id).map_err(Error::File)?; let mut block=file.block_reader(block_id).map_err(Error::File)?;
let region:Region=block.read_le().map_err(Error::InvalidData)?; let region:Region=block.read_le().map_err(Error::InvalidData)?;
Ok(region.models.into_iter().map(Into::into).collect()) Ok(region.models.into_iter().map(|(model_id,model)|
(model::ModelId::new(model_id),model.into())
).collect())
} }
fn read_mesh<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<model::Mesh,Error>{ fn read_mesh<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<model::Mesh,Error>{
let mut block=file.block_reader(block_id).map_err(Error::File)?; let mut block=file.block_reader(block_id).map_err(Error::File)?;
@ -251,7 +254,7 @@ impl<R:BinReaderExt> StreamableMap<R>{
self.bvh.the_tester(aabb,&mut |&block_id|block_ids.push(block_id)); self.bvh.the_tester(aabb,&mut |&block_id|block_ids.push(block_id));
block_ids block_ids
} }
pub fn load_region(&mut self,block_id:BlockId)->Result<Vec<model::Model>,Error>{ pub fn load_region(&mut self,block_id:BlockId)->Result<Vec<(model::ModelId,model::Model)>,Error>{
read_region(&mut self.file,block_id) read_region(&mut self.file,block_id)
} }
pub fn load_mesh(&mut self,mesh_id:model::MeshId)->Result<model::Mesh,Error>{ pub fn load_mesh(&mut self,mesh_id:model::MeshId)->Result<model::Mesh,Error>{
@ -263,14 +266,18 @@ impl<R:BinReaderExt> StreamableMap<R>{
read_texture(&mut self.file,block_id) read_texture(&mut self.file,block_id)
} }
pub fn into_complete_map(mut self)->Result<strafesnet_common::map::CompleteMap,Error>{ pub fn into_complete_map(mut self)->Result<strafesnet_common::map::CompleteMap,Error>{
let mut block_ids=Vec::new();
self.bvh.into_visitor(&mut |block_id|block_ids.push(block_id));
//count on reading the file in sequential order being fastest //count on reading the file in sequential order being fastest
block_ids.sort(); let mut block_ids=std::collections::BinaryHeap::new();
self.bvh.into_visitor(&mut |block_id|block_ids.push(block_id));
//load all regions //load all regions
let mut models=Vec::new(); let mut model_pairs=HashMap::new();
for block_id in block_ids{ for block_id in block_ids{
models.append(&mut read_region(&mut self.file,block_id)?); model_pairs.extend(read_region(&mut self.file,block_id)?);
}
let mut models=Vec::with_capacity(model_pairs.len());
for model_id in 0..model_pairs.len() as u32{
let model_id=model::ModelId::new(model_id);
models.push(model_pairs.remove(&model_id).ok_or(Error::InvalidModelId(model_id))?);
} }
//load all meshes //load all meshes
let mut meshes=Vec::with_capacity(self.resource_blocks.meshes.len()); let mut meshes=Vec::with_capacity(self.resource_blocks.meshes.len());
@ -302,7 +309,7 @@ fn collect_spacial_blocks(
block_location:&mut Vec<u64>, block_location:&mut Vec<u64>,
block_headers:&mut Vec<SpacialBlockHeader>, block_headers:&mut Vec<SpacialBlockHeader>,
sequential_block_data:&mut std::io::Cursor<&mut Vec<u8>>, sequential_block_data:&mut std::io::Cursor<&mut Vec<u8>>,
bvh_node:strafesnet_common::bvh::BvhWeightNode<usize,newtypes::model::Model> bvh_node:strafesnet_common::bvh::BvhWeightNode<usize,(model::ModelId,newtypes::model::Model)>
)->Result<(),Error>{ )->Result<(),Error>{
//inspect the node weights top-down. //inspect the node weights top-down.
//When a node weighs less than the limit, //When a node weighs less than the limit,
@ -311,9 +318,9 @@ fn collect_spacial_blocks(
let mut models=Vec::new(); let mut models=Vec::new();
let mut model_count=0; let mut model_count=0;
let extents=bvh_node.aabb().clone().into(); let extents=bvh_node.aabb().clone().into();
bvh_node.into_visitor(&mut |model|{ bvh_node.into_visitor(&mut |(model_id,model)|{
model_count+=1; model_count+=1;
models.push(model); models.push((model_id.get(),model));
}); });
let id=BlockId::new(block_headers.len() as u32+1); let id=BlockId::new(block_headers.len() as u32+1);
block_headers.push(SpacialBlockHeader{ block_headers.push(SpacialBlockHeader{
@ -343,14 +350,14 @@ fn collect_spacial_blocks(
/// otherwise sort by distance to start zone /// otherwise sort by distance to start zone
pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::CompleteMap)->Result<(),Error>{ pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::CompleteMap)->Result<(),Error>{
//serialize models and make a bvh that knows the file size of the branch //serialize models and make a bvh that knows the file size of the branch
let boxen=map.models.into_iter().map(|model|{ let boxen=map.models.into_iter().enumerate().map(|(model_id,model)|{
//grow your own aabb //grow your own aabb
let mesh=map.meshes.get(model.mesh.get() as usize).ok_or(Error::InvalidMeshId(model.mesh))?; let mesh=map.meshes.get(model.mesh.get() as usize).ok_or(Error::InvalidMeshId(model.mesh))?;
let mut aabb=strafesnet_common::aabb::Aabb::default(); let mut aabb=strafesnet_common::aabb::Aabb::default();
for &pos in &mesh.unique_pos{ for &pos in &mesh.unique_pos{
aabb.grow(model.transform.transform_point3(pos)); aabb.grow(model.transform.transform_point3(pos));
} }
Ok((model.into(),aabb)) Ok(((model::ModelId::new(model_id as u32),model.into()),aabb))
}).collect::<Result<Vec<_>,_>>()?; }).collect::<Result<Vec<_>,_>>()?;
let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|_|std::mem::size_of::<newtypes::model::Model>()); let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|_|std::mem::size_of::<newtypes::model::Model>());
//build blocks //build blocks