feat: implement MeshLoader
This loader abstracts mesh loading logic into a series of individual file handlers. Each handler is chosen based on the extension of the file to be loaded.
This commit is contained in:
1 parent
b7e31732e6
commit
ba9b677545
9 files changed
+157
-48
No files matched your search
@@ -5,7 +5,7 @@ use nalgebra::{Matrix4, Vector3, Vector4};
|
||||
/// Contains all information about an instance of a mesh, specifically its translation, rotation,
|
||||
/// and scale. Note that the rotation is a [`Vector4`], and not a `Quaternion`.
|
||||
///
|
||||
/// This may be taken from a scene instance by calling the `Instance::from` method on the
|
||||
/// This may be taken from a scene instance by calling the [`MeshInstance::from`] method on the
|
||||
/// corresponding [`SceneInstance`]. This is important, as the `Vector4` is not serializable.
|
||||
///
|
||||
/// Before passing this instance into the shaders, construct a transformation matrix by calling
|
||||
|
||||
+10
-16
@@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||
use vulkano::buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer};
|
||||
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter};
|
||||
use wavefront::Obj;
|
||||
use crate::loaders::mesh_loader::{MeshInfo, MeshLoader};
|
||||
|
||||
/// This struct is slightly misleading - it doesn't actually contain the mesh information, but
|
||||
/// the locations of the mesh's information in the overall application's vertex buffer, index buffer,
|
||||
@@ -47,16 +48,18 @@ impl MeshRegistry {
|
||||
pub fn from_scene(scene: &Scene, allocator: Arc<dyn MemoryAllocator>) -> Self {
|
||||
// Load all the meshes into a single map.
|
||||
println!("Loading meshes from scene");
|
||||
|
||||
let mesh_loader = MeshLoader::new();
|
||||
let meshes = scene
|
||||
.mesh_paths
|
||||
.iter()
|
||||
.map(|(id, path)| {
|
||||
(
|
||||
id.clone(),
|
||||
Obj::from_file(path).expect("Couldn't read wavefront file"),
|
||||
mesh_loader.load_mesh(path).expect("Couldn't read wavefront file"),
|
||||
)
|
||||
})
|
||||
.collect::<HashMap<String, Obj>>();
|
||||
.collect::<HashMap<String, MeshInfo>>();
|
||||
|
||||
let mut buf_verts = Vec::new();
|
||||
let mut buf_indices = Vec::new();
|
||||
@@ -65,27 +68,18 @@ impl MeshRegistry {
|
||||
// Information will be added directly to the verts and indices vectors, and a new Mesh
|
||||
// struct will be created and inserted into the mesh_list.
|
||||
let vert_start = buf_verts.len();
|
||||
let all_verts = obj.positions();
|
||||
let all_norms = obj.normals();
|
||||
let all_verts = obj.vertices;
|
||||
let all_norms = obj.normals;
|
||||
let obj_num_verts = all_verts.len();
|
||||
all_verts.iter().zip(all_norms.iter())
|
||||
.zip(obj.uvs().iter())
|
||||
.zip(obj.uvs.iter())
|
||||
.for_each(|((pos, norm), uv)| {
|
||||
buf_verts.push(Vertex3::new(*pos, *norm, *uv));
|
||||
});
|
||||
// buf_verts.extend(all_verts.map(|v| Vertex3::new(v.position(), v.normal().unwrap_or_default())));
|
||||
|
||||
let index_list = obj
|
||||
.triangles()
|
||||
.flat_map(|t| {
|
||||
t.iter()
|
||||
.map(|i| i.position_index() as u32)
|
||||
.collect::<Vec<u32>>()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
let index_start = buf_indices.len();
|
||||
let obj_num_idx = index_list.len();
|
||||
buf_indices.extend(index_list);
|
||||
let obj_num_idx = obj.indices.len();
|
||||
buf_indices.extend(obj.indices);
|
||||
|
||||
println!("Loaded mesh {} with {} vertices and {} indices", id, obj_num_verts, obj_num_idx);
|
||||
|
||||
|
||||
Reference in new issue
Block a user