indexed drawing
This commit is contained in:
@@ -33,6 +33,8 @@ use shaderc;
|
||||
use crate::PushConstants;
|
||||
use vulkano::instance::debug::{DebugCallback, MessageTypes};
|
||||
|
||||
use tobj::{load_obj};
|
||||
|
||||
const VALIDATION_LAYERS: &[&str] = &[
|
||||
"VK_LAYER_LUNARG_standard_validation"
|
||||
];
|
||||
@@ -57,7 +59,6 @@ pub trait Game {
|
||||
|
||||
pub struct GameData {
|
||||
pub start_time: SystemTime,
|
||||
pub mesh_vertices: Vec<Vertex>,
|
||||
pub line_vertices: Vec<Vertex>,
|
||||
pub push_constants: PushConstants,
|
||||
pub recreate_pipeline: bool,
|
||||
@@ -65,7 +66,9 @@ pub struct GameData {
|
||||
pub shutdown: bool,
|
||||
}
|
||||
|
||||
pub fn init(mesh_vertices: Vec<Vertex>, line_vertices: Vec<Vertex>, game: &mut dyn Game) {
|
||||
|
||||
|
||||
pub fn init(mesh_path: &str, line_vertices: Vec<Vertex>, game: &mut dyn Game) {
|
||||
let mut data = GameData {
|
||||
push_constants: PushConstants {
|
||||
time: 0.0,
|
||||
@@ -78,7 +81,6 @@ pub fn init(mesh_vertices: Vec<Vertex>, line_vertices: Vec<Vertex>, game: &mut d
|
||||
recreate_pipeline: false,
|
||||
aspect_ratio: 1.0,
|
||||
shutdown: false,
|
||||
mesh_vertices,
|
||||
line_vertices,
|
||||
};
|
||||
|
||||
@@ -192,8 +194,10 @@ pub fn init(mesh_vertices: Vec<Vertex>, line_vertices: Vec<Vertex>, game: &mut d
|
||||
PresentMode::Fifo, true, None).unwrap()
|
||||
};
|
||||
|
||||
let mesh_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data.mesh_vertices.iter().cloned()).unwrap();
|
||||
let line_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data.line_vertices.iter().cloned()).unwrap();
|
||||
let (mesh_vertices, mesh_indices) = load_mesh(mesh_path);
|
||||
let mesh_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), mesh_vertices.into_iter()).unwrap();
|
||||
let mesh_index_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::index_buffer(), mesh_indices.into_iter()).unwrap();
|
||||
let line_vertex_buffer = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::vertex_buffer(), data.line_vertices.iter().cloned()).unwrap();
|
||||
|
||||
let render_pass = Arc::new(vulkano::single_pass_renderpass!(
|
||||
device.clone(),
|
||||
@@ -313,7 +317,7 @@ pub fn init(mesh_vertices: Vec<Vertex>, line_vertices: Vec<Vertex>, game: &mut d
|
||||
.begin_render_pass(framebuffers[image_num].clone(), false, clear_values).unwrap()
|
||||
|
||||
// We are now inside the first subpass of the render pass. We add a draw command.
|
||||
.draw(pipeline.clone(), &dynamic_state, mesh_vertex_buffer.clone(), (), data.push_constants.clone()).unwrap()
|
||||
.draw_indexed(pipeline.clone(), &dynamic_state, mesh_vertex_buffer.clone(), mesh_index_buffer.clone(), (), data.push_constants.clone()).unwrap()
|
||||
.draw(line_pipeline.clone(), &dynamic_state, line_vertex_buffer.clone(), (), ()).unwrap()
|
||||
|
||||
// We leave the render pass by calling `draw_end`. Note that if we had multiple
|
||||
@@ -479,3 +483,37 @@ fn read_shader(vert_path_relative: &str, frag_path_relative: &str) -> Option<(Co
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn load_mesh(mesh_path: &str) -> (Vec<Vertex>, Vec<u32>) {
|
||||
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 color = [1.0, 1.0, 1.0];
|
||||
//
|
||||
// let tex_coord = [
|
||||
// mesh.texcoords[ind_usize * 2],
|
||||
// 1.0 - mesh.texcoords[ind_usize * 2 + 1],
|
||||
// ];
|
||||
|
||||
let vertex = Vertex { position };
|
||||
vertices.push(vertex);
|
||||
let index = indices.len() as u32;
|
||||
indices.push(index);
|
||||
}
|
||||
}
|
||||
|
||||
(vertices, indices)
|
||||
}
|
||||
Reference in New Issue
Block a user