refactor world_models generation

This commit is contained in:
Quaternions 2025-02-06 08:28:20 -08:00
parent 24c01962bf
commit d384744d2d
2 changed files with 66 additions and 47 deletions
lib
bsp_loader/src
common/src

@ -120,63 +120,81 @@ pub fn convert<'a>(
mb.build(polygon_groups,graphics_groups,vec![])
}).collect();
let brush_mesh_start_idx=world_meshes.len();
let mut world_models=Vec::new();
// the one and only world model 0
world_models.push(model::Model{
mesh:model::MeshId::new(0),
attributes:ATTRIBUTE_DECORATION,
transform:integer::Planar64Affine3::IDENTITY,
color:glam::Vec4::W,
});
for ent_result in bsp.entities.iter().map(|ent|ent.parse()){
let ent=match ent_result{
Ok(ent)=>ent,
Err(e)=>{
println!("Bsp Entity parse error: {e}");
continue;
},
};
match ent{
vbsp::Entity::Brush(brush)
|vbsp::Entity::BrushIllusionary(brush)
|vbsp::Entity::BrushWall(brush)
|vbsp::Entity::BrushWallToggle(brush)=>{
//The first character of brush.model is '*'
match brush.model[1..].parse(){
Ok(mesh_id)=>{
world_models.push(model::Model{
mesh:model::MeshId::new(mesh_id),
attributes:ATTRIBUTE_DECORATION,
transform:integer::Planar64Affine3::new(
integer::mat3::identity(),
valve_transform(brush.origin.into())
),
color:(glam::Vec3::from_array([
brush.color.r as f32,
brush.color.g as f32,
brush.color.b as f32
])/255.0).extend(1.0),
});
},
Err(e)=>{
println!("Brush model int parse error: {e}");
},
}
},
// vbsp::Entity::Spawn(spawn)=>
_=>(),
}
}
// physics models
for brush in &bsp.brushes{
if !brush.flags.contains(vbsp::BrushFlags::SOLID){
continue;
}
let mesh_result=crate::brush::brush_to_mesh(bsp,brush);
match mesh_result{
Ok(mesh)=>world_meshes.push(mesh),
Ok(mesh)=>{
let mesh_id=model::MeshId::new(world_meshes.len() as u32);
world_meshes.push(mesh);
world_models.push(model::Model{
mesh:mesh_id,
attributes:ATTRIBUTE_CONTACT_DEFAULT,
transform:integer::Planar64Affine3::new(
integer::mat3::identity(),
integer::vec3::ZERO,
),
color:glam::Vec4::ONE,
});
},
Err(e)=>println!("Brush mesh error: {e}"),
}
}
let world_models:Vec<model::Model>=
//one instance of the main world mesh
std::iter::once((
//world_model
model::MeshId::new(0),
//model_origin
vbsp::Vector::from([0.0,0.0,0.0]),
//model_color
vbsp::Color{r:255,g:255,b:255},
)).chain(
//entities sprinkle instances of the other meshes around
bsp.entities.iter()
.flat_map(|ent|ent.parse())//ignore entity parsing errors
.filter_map(|ent|match ent{
vbsp::Entity::Brush(brush)=>Some(brush),
vbsp::Entity::BrushIllusionary(brush)=>Some(brush),
vbsp::Entity::BrushWall(brush)=>Some(brush),
vbsp::Entity::BrushWallToggle(brush)=>Some(brush),
_=>None,
}).flat_map(|brush|
//The first character of brush.model is '*'
brush.model[1..].parse().map(|mesh_id|//ignore parse int errors
(model::MeshId::new(mesh_id),brush.origin,brush.color)
)
)
).map(|(mesh_id,model_origin,vbsp::Color{r,g,b})|model::Model{
mesh:mesh_id,
attributes:ATTRIBUTE_DECORATION,
transform:integer::Planar64Affine3::new(
integer::mat3::identity(),
valve_transform(model_origin.into())
),
color:(glam::Vec3::from_array([r as f32,g as f32,b as f32])/255.0).extend(1.0),
}).chain(
// physics models
(brush_mesh_start_idx..world_meshes.len()).map(|mesh_id|model::Model{
mesh:model::MeshId::new(mesh_id as u32),
attributes:ATTRIBUTE_CONTACT_DEFAULT,
transform:integer::Planar64Affine3::new(
integer::mat3::identity(),
integer::vec3::ZERO,
),
color:glam::Vec4::ONE,
})
).collect();
PartialMap1{
attributes:unique_attributes,

@ -654,6 +654,7 @@ pub struct Planar64Affine3{
pub translation:Planar64Vec3,
}
impl Planar64Affine3{
pub const IDENTITY:Self=Self::new(mat3::identity(),vec3::ZERO);
#[inline]
pub const fn new(matrix3:Planar64Mat3,translation:Planar64Vec3)->Self{
Self{matrix3,translation}