strafe-project/src/load_bsp.rs

74 lines
1.9 KiB
Rust
Raw Normal View History

2024-01-08 18:29:57 -08:00
const VALVE_SCALE:f32=1.0/16.0;
2024-01-08 17:31:51 -08:00
pub fn generate_indexed_models<R:std::io::Read+std::io::Seek>(input:&mut R)->Result<crate::model::IndexedModelInstances,vbsp::BspError>{
2023-10-09 21:37:07 -07:00
let mut s=Vec::new();
match input.read_to_end(&mut s){
2024-01-08 17:31:51 -08:00
Ok(_)=>(),
Err(e)=>println!("load_bsp::generate_indexed_models read_to_end failed: {:?}",e),
2023-10-09 21:37:07 -07:00
}
match vbsp::Bsp::read(s.as_slice()){
2024-01-08 17:31:51 -08:00
Ok(bsp)=>{
let mut spawn_point=crate::integer::Planar64Vec3::ZERO;
2023-10-02 15:28:57 -07:00
2024-01-08 17:31:51 -08:00
let mut indexed_models=Vec::new();
2024-01-08 18:06:18 -08:00
let vertices: Vec<_> = bsp
.vertices
.iter()
2024-01-08 18:29:57 -08:00
.map(|vertex|[vertex.position.x*VALVE_SCALE,vertex.position.z*VALVE_SCALE,vertex.position.y*VALVE_SCALE])
2024-01-08 18:06:18 -08:00
.collect();
2024-01-08 18:29:57 -08:00
let world_objects=bsp.models().map(|world_model|{
2024-01-08 18:06:18 -08:00
let world_polygons:Vec<obj::SimplePolygon> = world_model
.faces()
.filter(|face| face.is_visible())
.map(|face| {
face.vertex_indexes()
.map(|vertex_index| obj::IndexTuple(vertex_index as usize, Some(0), Some(0)))
.collect()
})
.map(obj::SimplePolygon)
.collect();
2024-01-08 18:29:57 -08:00
obj::Object {
2024-01-08 18:06:18 -08:00
name: "".to_string(),
groups: vec![obj::Group {
name: "".to_string(),
index: 0,
material: None,
polys: world_polygons,
}],
2024-01-08 18:29:57 -08:00
}
}).collect();
2024-01-08 18:06:18 -08:00
2024-01-08 18:29:57 -08:00
let obj_data = obj::ObjData {
position: vertices,
texture: vec![[0.0,0.0]],
normal: vec![[1.0,0.0,0.0]],
objects: world_objects,
material_libs: Vec::new(),
};
2024-01-08 18:06:18 -08:00
2024-01-08 18:29:57 -08:00
let mut new_indexed_models=crate::model::generate_indexed_model_list_from_obj(obj_data,glam::Vec4::ONE);
2024-01-08 18:06:18 -08:00
2024-01-08 18:29:57 -08:00
for indexed_model in &mut new_indexed_models{
indexed_model.instances.push(crate::model::ModelInstance{attributes:crate::model::CollisionAttributes::Decoration,..Default::default()});
2024-01-08 18:06:18 -08:00
}
2024-01-08 18:29:57 -08:00
indexed_models.append(&mut new_indexed_models);
2024-01-08 17:31:51 -08:00
Ok(crate::model::IndexedModelInstances{
textures:Vec::new(),
models:indexed_models,
spawn_point,
modes:Vec::new(),
})
},
Err(e)=>{
println!("rotten {:?}",e);
Err(e)
},
2023-10-02 15:28:57 -07:00
}
}