From d384744d2de9b4e8863175179c82732fce52cd10 Mon Sep 17 00:00:00 2001 From: Quaternions <krakow20@gmail.com> Date: Thu, 6 Feb 2025 08:28:20 -0800 Subject: [PATCH] refactor world_models generation --- lib/bsp_loader/src/bsp.rs | 112 ++++++++++++++++++++++---------------- lib/common/src/integer.rs | 1 + 2 files changed, 66 insertions(+), 47 deletions(-) diff --git a/lib/bsp_loader/src/bsp.rs b/lib/bsp_loader/src/bsp.rs index 3b02f87..77a9fe5 100644 --- a/lib/bsp_loader/src/bsp.rs +++ b/lib/bsp_loader/src/bsp.rs @@ -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, diff --git a/lib/common/src/integer.rs b/lib/common/src/integer.rs index 70bb9e3..d9a4022 100644 --- a/lib/common/src/integer.rs +++ b/lib/common/src/integer.rs @@ -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}