feat: implement gltf loader
This commit is contained in:
1 parent
ba9b677545
commit
5eb71f06cf
16 files changed
+172
-1375211
No files matched your search
+1
-1
@@ -81,7 +81,7 @@ impl MeshRegistry {
|
||||
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);
|
||||
println!("Loaded mesh \"{}\" with {} vertices and {} indices", id, obj_num_verts, obj_num_idx);
|
||||
|
||||
// TODO Create bounding boxes for each mesh and associate them with the struct.
|
||||
mesh_list.insert(
|
||||
|
||||
@@ -21,7 +21,7 @@ impl RenderBatch {
|
||||
instance_registry: Arc<InstanceRegistry>
|
||||
) -> Vec<Self> {
|
||||
mesh_registry.meshes.iter()
|
||||
.map(|(id, mesh)| {
|
||||
.filter_map(|(id, mesh)| {
|
||||
let instances = instance_registry.get_instances_for(id);
|
||||
|
||||
let gpu_instances = instances.iter()
|
||||
@@ -31,6 +31,10 @@ impl RenderBatch {
|
||||
})
|
||||
.collect::<Vec<GpuInstance>>();
|
||||
|
||||
if gpu_instances.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Build instances into a RenderBatch
|
||||
let instance_buffer = Buffer::from_iter(
|
||||
allocator.clone(),
|
||||
@@ -46,11 +50,11 @@ impl RenderBatch {
|
||||
gpu_instances
|
||||
).expect("Couldn't allocate instance buffer");
|
||||
|
||||
Self {
|
||||
Some(Self {
|
||||
mesh_id: id.clone(),
|
||||
instance_buffer,
|
||||
instance_count: instances.len() as u32,
|
||||
}
|
||||
})
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,47 @@
|
||||
use std::path::Path;
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use crate::loaders::mesh_loader::{MeshFileLoader, MeshInfo};
|
||||
|
||||
pub struct GltfLoader;
|
||||
|
||||
impl MeshFileLoader for GltfLoader {
|
||||
fn load_mesh(&self, path: &Path) -> Result<MeshInfo> {
|
||||
todo!()
|
||||
let scenes = easy_gltf::load(path)
|
||||
.map_err(|e| anyhow!("could not load gltf file: {e}"))?;
|
||||
|
||||
println!("Found {} scene(s) in gltf file", scenes.len());
|
||||
|
||||
let scene = scenes.first()
|
||||
.ok_or(anyhow!("no scenes in gltf file"))?;
|
||||
|
||||
println!("Found {} model(s) in gltf file", scene.models.len());
|
||||
|
||||
let mut vertices = Vec::new();
|
||||
let mut normals = Vec::new();
|
||||
let mut uvs = Vec::new();
|
||||
let mut indices = Vec::<u32>::new();
|
||||
for model in &scene.models {
|
||||
// Indices must be offset by the number of vertices that are already registered.
|
||||
let idx_offset = vertices.len() as u32;
|
||||
|
||||
// Put all vertex info together for a MeshInfo struct
|
||||
for vertex in model.vertices() {
|
||||
vertices.push(*vertex.position.as_ref());
|
||||
normals.push(*vertex.normal.as_ref());
|
||||
uvs.push([vertex.tex_coords.x, vertex.tex_coords.y, 0.0]);
|
||||
}
|
||||
model.indices()
|
||||
.expect("failed to find indices")
|
||||
.iter()
|
||||
.map(|i| i + idx_offset)
|
||||
.for_each(|i| indices.push(i));
|
||||
}
|
||||
|
||||
Ok(MeshInfo {
|
||||
vertices,
|
||||
indices,
|
||||
normals,
|
||||
uvs
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,13 @@ use crate::loaders::obj_loader::ObjLoader;
|
||||
use super::gltf_loader::*;
|
||||
|
||||
pub struct MeshInfo {
|
||||
/// The mesh's vertices.
|
||||
pub vertices: Vec<[f32; 3]>,
|
||||
/// The mesh's vertex normals.
|
||||
pub normals: Vec<[f32; 3]>,
|
||||
/// The mesh's vertex indices.
|
||||
pub indices: Vec<u32>,
|
||||
/// The texture coordinates. Note that these may also be used on 3D textures.
|
||||
pub uvs: Vec<[f32; 3]>,
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user