diff --git a/Cargo.toml b/Cargo.toml index 2556222..71ef8a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ shaderc = "0.5.0" cgmath = "0.17" winit = "0.19" image = "0.22.0" -tobj = "0.1.8" +collada = "0.11.0" serde = "1.0.97" serde_derive = "1.0.97" toml = "0.5.1" \ No newline at end of file diff --git a/models/cube.dae b/models/cube.dae new file mode 100644 index 0000000..14708e6 --- /dev/null +++ b/models/cube.dae @@ -0,0 +1,111 @@ + + + + + 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 9a98428..8500769 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,7 @@ fn main() { let line_count = 30; vulkan::init( - vec!["models/box.obj"], + vec!["models/cube.dae"], (-line_count..=line_count) .flat_map(|it| vec![ LinePoint { position: [it as f32, -line_count as f32, 0.] }, diff --git a/src/vulkan.rs b/src/vulkan.rs index 34e8886..ca36513 100644 --- a/src/vulkan.rs +++ b/src/vulkan.rs @@ -22,7 +22,7 @@ use winit::{EventsLoop, Window, WindowBuilder, Event, WindowEvent}; use std::sync::Arc; use std::time::SystemTime; -use std::path::PathBuf; +use std::path::{PathBuf, Path}; use std::ffi::{CStr}; use cgmath::{Matrix4, SquareMatrix}; @@ -34,7 +34,7 @@ use shaderc; use vs::ty::PushConstants; use line_vs::ty::LinePushConstants; -use tobj::{load_obj}; +use collada; const VALIDATION_LAYERS: &[&str] = &[ "VK_LAYER_LUNARG_standard_validation" @@ -218,7 +218,7 @@ pub fn init(mesh_paths: Vec<&str>, line_vertices: Vec, game: &mut dyn PresentMode::Fifo, true, None).unwrap() }; - mesh_paths.iter().for_each(|path| data.meshes.push(load_mesh(device.clone(), path))); + mesh_paths.iter().flat_map(|path| load_mesh(device.clone(), path)).for_each(|mesh| data.meshes.push(mesh)); let line_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), data.line_vertices.iter().cloned()).unwrap(); @@ -522,42 +522,53 @@ fn read_shader(vert_path_relative: &str, frag_path_relative: &str) -> Option<(Co } } -fn load_mesh(device: Arc, mesh_path: &str) -> Mesh { - let mut vertices = Vec::new(); - let mut indices = Vec::new(); - - let (models, _materials) = load_obj(mesh_path.as_ref()).unwrap(); - - for model in models.iter() { - let mesh = &model.mesh; - - for index in &mesh.indices { - let ind_usize = *index as usize; - let position = [ - mesh.positions[ind_usize * 3], - mesh.positions[ind_usize * 3 + 1], - mesh.positions[ind_usize * 3 + 2], - ]; - - let uv = [ - mesh.texcoords[ind_usize * 2], - 1.0 - mesh.texcoords[ind_usize * 2 + 1], - ]; - - let normal = [ - mesh.normals[ind_usize * 3], - mesh.normals[ind_usize * 3 + 1], - mesh.normals[ind_usize * 3 + 2], - ]; - - let vertex = Vertex { position, uv, normal }; - vertices.push(vertex); - let index = indices.len() as u32; - indices.push(index); - } +fn load_mesh(device: Arc, mesh_path: &str) -> Vec { + struct TempVertex { + pos: Option<[f32; 3]>, + uv: Option<[f32; 2]>, + normal: Option<[f32; 3]>, } - let vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), vertices.into_iter()).unwrap(); - let index_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::index_buffer(), indices.into_iter()).unwrap(); - Mesh { vertex_buffer, index_buffer } + let file = collada::document::ColladaDocument::from_path(Path::new(mesh_path)).unwrap(); + file.get_obj_set().unwrap().objects.iter().map(|model| { + 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(), + }); + + let vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), finished_vertices.into_iter()).unwrap(); + let index_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::index_buffer(), indices.into_iter()).unwrap(); + Mesh { vertex_buffer, index_buffer } + }).collect() } \ 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