finish write file

This commit is contained in:
Quaternions 2024-07-25 15:33:55 -07:00
parent e81909d375
commit a3aa6ea7d5

@ -283,34 +283,43 @@ impl<R:BinReaderExt> StreamableMap<R>{
} }
const BVH_NODE_MAX_WEIGHT:usize=64*1024;//64 kB const BVH_NODE_MAX_WEIGHT:usize=64*1024;//64 kB
fn collect_spacial_blocks(block_headers:&mut Vec<SpacialBlockHeader>,block_datas:&mut Vec<Vec<u8>>,bvh_node:strafesnet_common::bvh::BvhWeightNode<usize,Vec<u8>>){ fn collect_spacial_blocks(
block_headers:&mut Vec<SpacialBlockHeader>,
sequential_block_data:&mut std::io::Cursor<&mut Vec<u8>>,
bvh_node:strafesnet_common::bvh::BvhWeightNode<usize,newtypes::model::Model>
)->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,
//serialize its entire contents into a region block //serialize its entire contents into a region block
if *bvh_node.weight()<BVH_NODE_MAX_WEIGHT{ if *bvh_node.weight()<BVH_NODE_MAX_WEIGHT{
let mut data=Vec::new(); let mut models=Vec::new();
let mut 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 |mut model_data|{ bvh_node.into_visitor(&mut |model|{
count+=1; model_count+=1;
data.append(&mut model_data); models.push(model);
}); });
let id=BlockId::new(block_headers.len() as u32); let id=BlockId::new(block_headers.len() as u32);
block_headers.push(SpacialBlockHeader{ block_headers.push(SpacialBlockHeader{
id, id,
extents, extents,
}); });
block_datas.push(data); let region=Region{
model_count,
models,
};
binrw::BinWrite::write_le(&region,sequential_block_data).map_err(Error::InvalidData)?;
}else{ }else{
match bvh_node.into_content(){ match bvh_node.into_content(){
strafesnet_common::bvh::RecursiveContent::Branch(bvh_node_list)=>{ strafesnet_common::bvh::RecursiveContent::Branch(bvh_node_list)=>{
for bvh_node in bvh_node_list{ for bvh_node in bvh_node_list{
collect_spacial_blocks(block_headers,block_datas,bvh_node); collect_spacial_blocks(block_headers,sequential_block_data,bvh_node)?;
} }
}, },
strafesnet_common::bvh::RecursiveContent::Leaf(_)=>panic!(),//bvh branches are 20 leaves minimum strafesnet_common::bvh::RecursiveContent::Leaf(_)=>panic!(),//bvh branches are 20 leaves minimum
} }
} }
Ok(())
} }
/// TODO: Optionally provide a bot that describes the path through the map /// TODO: Optionally provide a bot that describes the path through the map
@ -324,18 +333,35 @@ pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::Comple
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));
} }
let serializable_model:newtypes::model::Model=model.into(); Ok((model.into(),aabb))
let mut data=Vec::new();
binrw::BinWrite::write_le(&serializable_model,&mut std::io::Cursor::new(&mut data)).map_err(Error::InvalidData)?;
Ok((data,aabb))
}).collect::<Result<Vec<_>,_>>()?; }).collect::<Result<Vec<_>,_>>()?;
let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|model|model.len()); let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|_|std::mem::size_of::<newtypes::model::Model>());
//build blocks //build blocks
let mut spacial_blocks=Vec::new(); let mut spacial_blocks=Vec::new();//for header
let mut spacial_blocks_data=Vec::new(); let mut sequential_block_data=Vec::new();
collect_spacial_blocks(&mut spacial_blocks,&mut spacial_blocks_data,bvh); let mut cursor_to_data=std::io::Cursor::new(&mut sequential_block_data);
let resource_blocks=Vec::new(); collect_spacial_blocks(&mut spacial_blocks,&mut cursor_to_data,bvh)?;
//let resource_blocks_data=Vec::new(); let mut block_id=spacial_blocks.len() as u32;//continue block id
let mut resource_blocks=Vec::new();//for header
//meshes
for mesh in map.meshes.into_iter(){
resource_blocks.push(ResourceBlockHeader{
resource:ResourceType::Mesh,
id:BlockId::new(block_id),
});
block_id+=1;
let serializable_mesh:newtypes::model::Mesh=mesh.into();
binrw::BinWrite::write_le(&serializable_mesh,&mut cursor_to_data).map_err(Error::InvalidData)?;
}
//textures
for mut texture in map.textures.into_iter(){
resource_blocks.push(ResourceBlockHeader{
resource:ResourceType::Texture,
id:BlockId::new(block_id),
});
block_id+=1;
sequential_block_data.append(&mut texture);
}
//build header //build header
let header=MapHeader{ let header=MapHeader{
num_spacial_blocks:spacial_blocks.len() as u32, num_spacial_blocks:spacial_blocks.len() as u32,
@ -352,6 +378,8 @@ pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::Comple
render_configs:map.render_configs.into_iter().map(Into::into).collect(), render_configs:map.render_configs.into_iter().map(Into::into).collect(),
}; };
//write header //write header
writer.write_le(&header).map_err(Error::InvalidData)?;
//write blocks //write blocks
writer.write(&sequential_block_data).map_err(Error::IO)?;
Ok(()) Ok(())
} }