implement bsp.models() conversion

This commit is contained in:
Quaternions 2024-02-13 19:18:19 -08:00
parent 0fb96d2c19
commit 4678318b3b

View File

@ -13,18 +13,25 @@ where
AcquireTextureId:FnMut(&str)->model::TextureId,
AcquireMeshId:FnMut(&str)->model::MeshId,
{
let mut name_from_texture_id=Vec::new();
let mut texture_id_from_name=std::collections::HashMap::new();
let mut unique_render_configs=Vec::new();
let mut models=bsp.models().map(|world_model|{
let mut unique_attributes=Vec::new();
unique_attributes.push(gameplay_attributes::CollisionAttributes::contact_default());
const TEMP_TOUCH_ME_ATTRIBUTE:gameplay_attributes::CollisionAttributesId=gameplay_attributes::CollisionAttributesId::new(0);
//the generated MeshIds in here will collide with the Loader Mesh Ids
//but I can't think of a good workaround other than just remapping one later.
let (meshes,models):(Vec<model::Mesh>,Vec<model::Model>)=bsp.models().enumerate().map(|(mesh_id,world_model)|{
let mesh_id=model::MeshId::new(mesh_id as u32);
//non-deduplicated
let mut spam_pos=Vec::new();
let mut spam_tex=Vec::new();
let mut spam_normal=Vec::new();
let mut spam_vertices=Vec::new();
let groups=world_model.faces()
.filter(|face|face.is_visible())//TODO: look at this
.map(|face|{
let mut graphics_groups=Vec::new();
let mut physics_group=model::IndexedPhysicsGroup::default();
let polygon_groups=world_model.faces().enumerate().map(|(polygon_group_id,face)|{
let polygon_group_id=model::PolygonGroupId::new(polygon_group_id as u32);
let face_texture=face.texture();
let face_texture_data=face_texture.texture_data();
let (texture_u,texture_v)=(glam::Vec3A::from_slice(&face_texture.texture_transforms_u[0..3]),glam::Vec3A::from_slice(&face_texture.texture_transforms_v[0..3]));
@ -32,20 +39,19 @@ where
let texture_size=glam::vec2(face_texture_data.width as f32,face_texture_data.height as f32);
//texture
let texture_id=if let Some(&texture_id)=texture_id_from_name.get(face_texture_data.name()){
texture_id
}else{
let texture_id=name_from_texture_id.len() as u32;
texture_id_from_name.insert(face_texture_data.name().to_string(),texture_id);
name_from_texture_id.push(face_texture_data.name().to_string());
texture_id
let texture_id=acquire_texture_id(face_texture_data.name()); //this is equivalent to a get_or_create pattern because there is a singular no-texture RenderId
//so RenderId==TextureId
//not the most failsafe code but this is just for the map tool lmao
if unique_render_configs.len()==texture_id.get() as usize{
unique_render_configs.push(model::RenderConfig::texture(texture_id));
};
let render_id=model::RenderConfigId::new(texture_id.get());
//normal
let normal=face.normal();
let normal_idx=spam_normal.len() as u32;
spam_normal.push(valve_transform(<[f32;3]>::from(normal)));
let mut indices:Vec<u32>=face.vertex_positions().map(|vertex_position|{
let mut indices:Vec<model::VertexId>=face.vertex_positions().map(|vertex_position|{
let vertex_xyz=<[f32;3]>::from(vertex_position);
let pos=glam::Vec3A::from_array(vertex_xyz);
let pos_idx=spam_pos.len();
@ -56,36 +62,46 @@ where
let tex_idx=spam_tex.len() as u32;
spam_tex.push(tex);
let i=spam_vertices.len() as u32;
let vertex_id=model::VertexId::new(spam_vertices.len() as u32);
spam_vertices.push(model::IndexedVertex{
pos: pos_idx as u32,
tex: tex_idx as u32,
normal: normal_idx,
color: 0,
pos:model::PositionId::new(pos_idx as u32),
tex:model::TextureCoordinateId::new(tex_idx as u32),
normal:model::NormalId::new(normal_idx),
color:model::ColorId::new(0),
});
i
vertex_id
}).collect();
indices.reverse();
texture:Some(texture_id),
model::IndexedGroup::PolygonList(vec![model::IndexedPolygon{indices}])
if face.is_visible(){
graphics_groups.push(model::IndexedGraphicsGroup{
render:render_id,
groups:vec![polygon_group_id],
})
}
physics_group.groups.push(polygon_group_id);
model::PolygonGroup::PolygonList(model::PolygonList::new(vec![indices]))
}).collect();
model::Mesh{
unique_pos:spam_pos,
unique_tex:spam_tex,
unique_normal:spam_normal,
unique_color:vec![glam::Vec4::ONE],
unique_vertices:spam_vertices,
groups,
instances:vec![model::Model{
attributes:gameplay_attributes::CollisionAttributes::Decoration,
(
model::Mesh{
unique_pos:spam_pos,
unique_tex:spam_tex,
unique_normal:spam_normal,
unique_color:vec![glam::Vec4::ONE],
unique_vertices:spam_vertices,
polygon_groups,
graphics_groups,
physics_groups:vec![physics_group],
},
model::Model{
mesh:mesh_id,
attributes:TEMP_TOUCH_ME_ATTRIBUTE,
transform:integer::Planar64Affine3::new(
integer::Planar64Mat3::default(),
valve_transform(<[f32;3]>::from(world_model.origin))
),
..Default::default()
}],
}
}).collect();
color:glam::Vec4::ONE,
},
)
}).unzip();
//dedupe prop models
let mut model_dedupe=std::collections::HashSet::new();
@ -212,10 +228,11 @@ where
//actually add the prop models
prop_models.append(&mut models);
Ok(model::MeshInstances{
textures:name_from_texture_id,
models:prop_models,
spawn_point,
modes:Vec::new(),
})
map::CompleteMap{
attributes:unique_attributes,
meshes,
render_configs:unique_render_configs,
models,
modes:strafesnet_common::gameplay_modes::Modes::new(Vec::new()),
}
}