diff --git a/Cargo.toml b/Cargo.toml index c2f8451..6d4e9d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ shaderc = "0.5.0" cgmath = "0.17" winit = "0.19" image = "0.22.0" -collada = "0.11.0" serde = "1.0.97" serde_derive = "1.0.97" toml = "0.5.1" gilrs = "0.7.1" +gltf = "0.13.0" \ No newline at end of file diff --git a/models/box.bin b/models/box.bin new file mode 100644 index 0000000..c013210 Binary files /dev/null and b/models/box.bin differ diff --git a/models/box.gltf b/models/box.gltf new file mode 100644 index 0000000..76a8ff0 --- /dev/null +++ b/models/box.gltf @@ -0,0 +1,117 @@ +{ + "asset" : { + "generator" : "Khronos glTF Blender I/O v0.9.36", + "version" : "2.0" + }, + "scene" : 0, + "scenes" : [ + { + "name" : "Scene", + "nodes" : [ + 0 + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "Cube" + } + ], + "materials" : [ + { + "doubleSided" : true, + "name" : "Material", + "pbrMetallicRoughness" : { + "baseColorFactor" : [ + 0.800000011920929, + 0.800000011920929, + 0.800000011920929, + 1 + ], + "metallicFactor" : 0, + "roughnessFactor" : 0.4000000059604645 + } + } + ], + "meshes" : [ + { + "name" : "Cube", + "primitives" : [ + { + "attributes" : { + "POSITION" : 0, + "NORMAL" : 1, + "TEXCOORD_0" : 2 + }, + "indices" : 3, + "material" : 0 + } + ] + } + ], + "accessors" : [ + { + "bufferView" : 0, + "componentType" : 5126, + "count" : 24, + "max" : [ + 1, + 1, + 1 + ], + "min" : [ + -1, + -1, + -1 + ], + "type" : "VEC3" + }, + { + "bufferView" : 1, + "componentType" : 5126, + "count" : 24, + "type" : "VEC3" + }, + { + "bufferView" : 2, + "componentType" : 5126, + "count" : 24, + "type" : "VEC2" + }, + { + "bufferView" : 3, + "componentType" : 5123, + "count" : 36, + "type" : "SCALAR" + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 0 + }, + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 288 + }, + { + "buffer" : 0, + "byteLength" : 192, + "byteOffset" : 576 + }, + { + "buffer" : 0, + "byteLength" : 72, + "byteOffset" : 768 + } + ], + "buffers" : [ + { + "byteLength" : 840, + "uri" : "box.bin" + } + ] +} diff --git a/models/cube.dae b/models/cube.dae deleted file mode 100644 index 14708e6..0000000 --- a/models/cube.dae +++ /dev/null @@ -1,111 +0,0 @@ - - - - - Blender User - Blender 2.80.74 commit date:2019-07-11, commit time:13:50, hash:06312c6d2db8 - - 2019-07-29T12:00:25 - 2019-07-29T12:00:25 - - Z_UP - - - - - - - - 0 0 0 1 - - - 0.8 0.8 0.8 1 - - - 0.4 - - - 0 - - - 1 - - - 1.45 - - - - - - - - - - - - - - - - - 1 1 1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1 - - - - - - - - - - 0 0 1 0 -1 0 -1 0 0 0 0 -1 1 0 0 0 1 0 - - - - - - - - - - 0.625 0 0.375 0.25 0.375 0 0.625 0.25 0.375 0.5 0.375 0.25 0.625 0.5 0.375 0.75 0.375 0.5 0.625 0.75 0.375 1 0.375 0.75 0.375 0.5 0.125 0.75 0.125 0.5 0.875 0.5 0.625 0.75 0.625 0.5 0.625 0 0.625 0.25 0.375 0.25 0.625 0.25 0.625 0.5 0.375 0.5 0.625 0.5 0.625 0.75 0.375 0.75 0.625 0.75 0.625 1 0.375 1 0.375 0.5 0.375 0.75 0.125 0.75 0.875 0.5 0.875 0.75 0.625 0.75 - - - - - - - - - - - - - - - 4 0 0 2 0 1 0 0 2 2 1 3 7 1 4 3 1 5 6 2 6 5 2 7 7 2 8 1 3 9 7 3 10 5 3 11 0 4 12 3 4 13 1 4 14 4 5 15 1 5 16 5 5 17 4 0 18 6 0 19 2 0 20 2 1 21 6 1 22 7 1 23 6 2 24 4 2 25 5 2 26 1 3 27 3 3 28 7 3 29 0 4 30 2 4 31 3 4 32 4 5 33 0 5 34 1 5 35 - - - - - - - - - 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 18a6017..79e83ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ impl Game for TestGame { impl TestGame { fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) { - self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/cube.dae", self.log_config.mesh_load_info).into_iter().nth(0).unwrap())); + self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/box.gltf", self.log_config.mesh_load_info).unwrap().into_iter().nth(0).unwrap())); println!("Game started."); } diff --git a/src/mesh.rs b/src/mesh.rs index 8e00cb6..7034fa7 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -1,62 +1,64 @@ use crate::vulkan::Vertex; -use std::path::Path; + +use std::time::SystemTime; pub struct CPUMesh { pub(crate) vertices: Vec, pub(crate) indices: Vec, } -pub fn load_mesh(mesh_path: &str, print_status: bool) -> Vec { - struct TempVertex { - pos: Option<[f32; 3]>, - uv: Option<[f32; 2]>, - normal: Option<[f32; 3]>, +pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result, gltf::Error> { + let mut start_time = None; + let mut total_vertices = 0; + let mut total_indices = 0; + + if print_status { + start_time = Some(SystemTime::now()); + println!("Loading mesh file {}", mesh_path); + } + let (document, buffers, _textures) = gltf::import(mesh_path)?; + let mut meshes = vec![]; + + if print_status { println!("Mesh file loaded after {} seconds, processing...", start_time.unwrap().elapsed().unwrap().as_secs()); } + + + for mesh in document.meshes() { + for primitive in mesh.primitives() { + let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()])); + match (reader.read_indices(), reader.read_positions(), reader.read_tex_coords(0), reader.read_normals()) { + (Some(indices), Some(positions), Some(tex_coords), Some(normals)) => { + let vertices = positions.zip(tex_coords.into_f32()).zip(normals).map(|((p, t), n)| Vertex { + position: p, + uv: t, + normal: n + }).collect(); + let cpu_mesh = CPUMesh { vertices, indices: indices.into_u32().collect() }; + if print_status { + let vert_count = cpu_mesh.vertices.len(); + let index_count = cpu_mesh.indices.len(); + + println!("Loaded mesh {} after {} seconds: {} Vertices, {} Indices", + mesh.name().unwrap_or(""), + start_time.unwrap().elapsed().unwrap().as_secs(), + vert_count, + index_count); + + total_vertices += vert_count; + total_indices += index_count; + } + meshes.push(cpu_mesh); + }, + (None, _, _, _) => println!("Indices missing!"), + (_, None, _, _) => println!("Vertex positions missing!"), + (_, _, None, _) => println!("Tex coords missing!"), + (_, _, _, None) => println!("Normals missing!"), + } + } } if print_status { - println!("Start loading file {}", mesh_path); + println!("Finished loading file, total vertices: {}, total indices: {}", total_vertices, total_indices); } - let file = collada::document::ColladaDocument::from_path(Path::new(mesh_path)).unwrap(); - file.get_obj_set().unwrap().objects.iter().map(|model| { - if print_status { - println!("Loading mesh {}: {} Vertices", model.name, model.vertices.len()); - } - let mut vertices: Vec = model.vertices.iter().map(|v| { - TempVertex { - pos: Some([v.x as f32, v.y as f32, v.z as f32]), - uv: None, - normal: None - } - }).collect(); - let mut indices = Vec::new(); - - model.geometry.iter().for_each(|geometry| geometry.mesh.iter().for_each(|primitive| { - if let collada::PrimitiveElement::Triangles(tris) = primitive { - tris.vertices.iter().for_each(|tri| { - [tri.0, tri.1, tri.2].iter().for_each(|(vertex_index, texture_position_index, normal_index)| { - indices.push(*vertex_index as u32); - vertices[*vertex_index].uv = texture_position_index.or(None).map(|tpi| { - let tex_vertex = model.tex_vertices[tpi]; - [tex_vertex.x as f32, 1.0 - tex_vertex.y as f32] - }); - vertices[*vertex_index].normal = normal_index.or(None).map(|ni| { - let normal = model.normals[ni]; - [normal.x as f32, normal.y as f32, normal.z as f32] - }); - }); - }); - } else { - panic!("Mesh format Polylist not supported!"); - } - })); - - let finished_vertices = vertices.iter().map(|v| Vertex { - position: v.pos.unwrap(), - uv: v.uv.unwrap(), - normal: v.normal.unwrap(), - }).collect(); - - CPUMesh { vertices: finished_vertices, indices } - }).collect() + Ok(meshes) } \ No newline at end of file
4 0 0 2 0 1 0 0 2 2 1 3 7 1 4 3 1 5 6 2 6 5 2 7 7 2 8 1 3 9 7 3 10 5 3 11 0 4 12 3 4 13 1 4 14 4 5 15 1 5 16 5 5 17 4 0 18 6 0 19 2 0 20 2 1 21 6 1 22 7 1 23 6 2 24 4 2 25 5 2 26 1 3 27 3 3 28 7 3 29 0 4 30 2 4 31 3 4 32 4 5 33 0 5 34 1 5 35