dae dae
This commit is contained in:
@@ -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.] },
|
||||
|
||||
@@ -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<LinePoint>, 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<Device>, 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<Device>, mesh_path: &str) -> Vec<Mesh> {
|
||||
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<TempVertex> = 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()
|
||||
}
|
||||
Reference in New Issue
Block a user