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, AcquireTextureId:FnMut(&str)->model::TextureId,
AcquireMeshId:FnMut(&str)->model::MeshId, AcquireMeshId:FnMut(&str)->model::MeshId,
{ {
let mut name_from_texture_id=Vec::new(); let mut unique_render_configs=Vec::new();
let mut texture_id_from_name=std::collections::HashMap::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 //non-deduplicated
let mut spam_pos=Vec::new(); let mut spam_pos=Vec::new();
let mut spam_tex=Vec::new(); let mut spam_tex=Vec::new();
let mut spam_normal=Vec::new(); let mut spam_normal=Vec::new();
let mut spam_vertices=Vec::new(); let mut spam_vertices=Vec::new();
let groups=world_model.faces() let mut graphics_groups=Vec::new();
.filter(|face|face.is_visible())//TODO: look at this let mut physics_group=model::IndexedPhysicsGroup::default();
.map(|face|{ 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=face.texture();
let face_texture_data=face_texture.texture_data(); 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])); 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); let texture_size=glam::vec2(face_texture_data.width as f32,face_texture_data.height as f32);
//texture //texture
let texture_id=if let Some(&texture_id)=texture_id_from_name.get(face_texture_data.name()){ 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
texture_id //so RenderId==TextureId
}else{ //not the most failsafe code but this is just for the map tool lmao
let texture_id=name_from_texture_id.len() as u32; if unique_render_configs.len()==texture_id.get() as usize{
texture_id_from_name.insert(face_texture_data.name().to_string(),texture_id); unique_render_configs.push(model::RenderConfig::texture(texture_id));
name_from_texture_id.push(face_texture_data.name().to_string());
texture_id
}; };
let render_id=model::RenderConfigId::new(texture_id.get());
//normal //normal
let normal=face.normal(); let normal=face.normal();
let normal_idx=spam_normal.len() as u32; let normal_idx=spam_normal.len() as u32;
spam_normal.push(valve_transform(<[f32;3]>::from(normal))); 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 vertex_xyz=<[f32;3]>::from(vertex_position);
let pos=glam::Vec3A::from_array(vertex_xyz); let pos=glam::Vec3A::from_array(vertex_xyz);
let pos_idx=spam_pos.len(); let pos_idx=spam_pos.len();
@ -56,36 +62,46 @@ where
let tex_idx=spam_tex.len() as u32; let tex_idx=spam_tex.len() as u32;
spam_tex.push(tex); 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{ spam_vertices.push(model::IndexedVertex{
pos: pos_idx as u32, pos:model::PositionId::new(pos_idx as u32),
tex: tex_idx as u32, tex:model::TextureCoordinateId::new(tex_idx as u32),
normal: normal_idx, normal:model::NormalId::new(normal_idx),
color: 0, color:model::ColorId::new(0),
}); });
i vertex_id
}).collect(); }).collect();
indices.reverse(); if face.is_visible(){
texture:Some(texture_id), graphics_groups.push(model::IndexedGraphicsGroup{
model::IndexedGroup::PolygonList(vec![model::IndexedPolygon{indices}]) render:render_id,
groups:vec![polygon_group_id],
})
}
physics_group.groups.push(polygon_group_id);
model::PolygonGroup::PolygonList(model::PolygonList::new(vec![indices]))
}).collect(); }).collect();
model::Mesh{ (
unique_pos:spam_pos, model::Mesh{
unique_tex:spam_tex, unique_pos:spam_pos,
unique_normal:spam_normal, unique_tex:spam_tex,
unique_color:vec![glam::Vec4::ONE], unique_normal:spam_normal,
unique_vertices:spam_vertices, unique_color:vec![glam::Vec4::ONE],
groups, unique_vertices:spam_vertices,
instances:vec![model::Model{ polygon_groups,
attributes:gameplay_attributes::CollisionAttributes::Decoration, graphics_groups,
physics_groups:vec![physics_group],
},
model::Model{
mesh:mesh_id,
attributes:TEMP_TOUCH_ME_ATTRIBUTE,
transform:integer::Planar64Affine3::new( transform:integer::Planar64Affine3::new(
integer::Planar64Mat3::default(), integer::Planar64Mat3::default(),
valve_transform(<[f32;3]>::from(world_model.origin)) valve_transform(<[f32;3]>::from(world_model.origin))
), ),
..Default::default() color:glam::Vec4::ONE,
}], },
} )
}).collect(); }).unzip();
//dedupe prop models //dedupe prop models
let mut model_dedupe=std::collections::HashSet::new(); let mut model_dedupe=std::collections::HashSet::new();
@ -212,10 +228,11 @@ where
//actually add the prop models //actually add the prop models
prop_models.append(&mut models); prop_models.append(&mut models);
Ok(model::MeshInstances{ map::CompleteMap{
textures:name_from_texture_id, attributes:unique_attributes,
models:prop_models, meshes,
spawn_point, render_configs:unique_render_configs,
modes:Vec::new(), models,
}) modes:strafesnet_common::gameplay_modes::Modes::new(Vec::new()),
}
} }