This commit is contained in:
2019-07-29 16:45:37 +02:00
parent 3f62333d66
commit e01ff8d395
4 changed files with 163 additions and 41 deletions

View File

@@ -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"

111
models/cube.dae Normal file
View File

@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<asset>
<contributor>
<author>Blender User</author>
<authoring_tool>Blender 2.80.74 commit date:2019-07-11, commit time:13:50, hash:06312c6d2db8</authoring_tool>
</contributor>
<created>2019-07-29T12:00:25</created>
<modified>2019-07-29T12:00:25</modified>
<unit name="meter" meter="1"/>
<up_axis>Z_UP</up_axis>
</asset>
<library_effects>
<effect id="Material-effect">
<profile_COMMON>
<technique sid="common">
<lambert>
<emission>
<color sid="emission">0 0 0 1</color>
</emission>
<diffuse>
<color sid="diffuse">0.8 0.8 0.8 1</color>
</diffuse>
<shininess>
<float sid="shininess">0.4</float>
</shininess>
<reflectivity>
<float sid="specular">0</float>
</reflectivity>
<transparency>
<float sid="alpha">1</float>
</transparency>
<index_of_refraction>
<float sid="ior">1.45</float>
</index_of_refraction>
</lambert>
</technique>
</profile_COMMON>
</effect>
</library_effects>
<library_images/>
<library_materials>
<material id="Material-material" name="Material">
<instance_effect url="#Material-effect"/>
</material>
</library_materials>
<library_geometries>
<geometry id="Cube-mesh" name="Cube">
<mesh>
<source id="Cube-mesh-positions">
<float_array id="Cube-mesh-positions-array" count="24">1 1 1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1</float_array>
<technique_common>
<accessor source="#Cube-mesh-positions-array" count="8" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<source id="Cube-mesh-normals">
<float_array id="Cube-mesh-normals-array" count="18">0 0 1 0 -1 0 -1 0 0 0 0 -1 1 0 0 0 1 0</float_array>
<technique_common>
<accessor source="#Cube-mesh-normals-array" count="6" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<source id="Cube-mesh-map-0">
<float_array id="Cube-mesh-map-0-array" count="72">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</float_array>
<technique_common>
<accessor source="#Cube-mesh-map-0-array" count="36" stride="2">
<param name="S" type="float"/>
<param name="T" type="float"/>
</accessor>
</technique_common>
</source>
<vertices id="Cube-mesh-vertices">
<input semantic="POSITION" source="#Cube-mesh-positions"/>
</vertices>
<triangles material="Material-material" count="12">
<input semantic="VERTEX" source="#Cube-mesh-vertices" offset="0"/>
<input semantic="NORMAL" source="#Cube-mesh-normals" offset="1"/>
<input semantic="TEXCOORD" source="#Cube-mesh-map-0" offset="2" set="0"/>
<p>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</p>
</triangles>
</mesh>
</geometry>
</library_geometries>
<library_controllers/>
<library_visual_scenes>
<visual_scene id="Scene" name="Scene">
<node id="Cube" name="Cube" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#Cube-mesh" name="Cube">
<bind_material>
<technique_common>
<instance_material symbol="Material-material" target="#Material-material">
<bind_vertex_input semantic="UVMap" input_semantic="TEXCOORD" input_set="0"/>
</instance_material>
</technique_common>
</bind_material>
</instance_geometry>
</node>
</visual_scene>
</library_visual_scenes>
<scene>
<instance_visual_scene url="#Scene"/>
</scene>
</COLLADA>

View File

@@ -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.] },

View File

@@ -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()
}