Better vertex definitions

This commit is contained in:
2019-07-26 23:10:48 +02:00
parent 5fdd99b051
commit 96afb5a14b
2 changed files with 30 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
use crate::vulkan::{Vertex, GameData, Game};
use crate::vulkan::{GameData, Game, LinePoint};
use winit::{Event, WindowEvent, ElementState};
use std::iter::FromIterator;
use cgmath::{Matrix4, Rad, Point3, Vector3, Deg};
@@ -70,8 +70,8 @@ fn main() {
vulkan::init(
"models/box.obj",
vec![
Vertex { position: [-0.9, 1., 0.] },
Vertex { position: [0.9, 0., 0.] },
LinePoint { position: [-0.9, 1., 0.] },
LinePoint { position: [0.9, 0., 0.] },
],
&mut game
);

View File

@@ -11,7 +11,7 @@ use vulkano::swapchain::{AcquireError, PresentMode, SurfaceTransform, Swapchain,
use vulkano::swapchain;
use vulkano::sync::{GpuFuture, FlushError};
use vulkano::sync;
use vulkano::pipeline::vertex::SingleBufferDefinition;
use vulkano::pipeline::vertex::{SingleBufferDefinition};
use vulkano::descriptor::PipelineLayoutAbstract;
use vulkano::format::Format;
@@ -47,8 +47,16 @@ const ENABLE_VALIDATION_LAYERS: bool = false;
#[derive(Default, Debug, Clone)]
pub struct Vertex {
pub position: [f32; 3],
pub uv: [f32; 2],
pub normal: [f32; 3],
}
vulkano::impl_vertex!(Vertex, position);
vulkano::impl_vertex!(Vertex, position, uv, normal);
#[derive(Default, Debug, Clone)]
pub struct LinePoint {
pub position: [f32; 3],
}
vulkano::impl_vertex!(LinePoint, position);
pub trait Game {
fn update(self: &mut Self, game_data: &mut GameData);
@@ -59,7 +67,7 @@ pub trait Game {
pub struct GameData {
pub start_time: SystemTime,
pub line_vertices: Vec<Vertex>,
pub line_vertices: Vec<LinePoint>,
pub push_constants: PushConstants,
pub recreate_pipeline: bool,
pub aspect_ratio: f32,
@@ -68,7 +76,7 @@ pub struct GameData {
pub fn init(mesh_path: &str, line_vertices: Vec<Vertex>, game: &mut dyn Game) {
pub fn init(mesh_path: &str, line_vertices: Vec<LinePoint>, game: &mut dyn Game) {
let mut data = GameData {
push_constants: PushConstants {
time: 0.0,
@@ -404,7 +412,7 @@ pub mod fs {
}
}
fn create_pipeline<T: RenderPassAbstract>(device: Arc<Device>, sub_pass: Subpass<Arc<T>>, vertex_shader_path: &str, fragment_shader_path: &str, is_line: bool) -> Option<Arc<GraphicsPipeline<SingleBufferDefinition<Vertex>, Box<dyn PipelineLayoutAbstract + Send + Sync>, Arc<T>>>> {
fn create_pipeline<T: RenderPassAbstract, V: vulkano::pipeline::vertex::Vertex>(device: Arc<Device>, sub_pass: Subpass<Arc<T>>, vertex_shader_path: &str, fragment_shader_path: &str, is_line: bool) -> Option<Arc<GraphicsPipeline<SingleBufferDefinition<V>, Box<dyn PipelineLayoutAbstract + Send + Sync>, Arc<T>>>> {
if let Some((shader, shader_data)) = read_shader(vertex_shader_path, fragment_shader_path) {
let vertex_shader_entry;
let fragment_shader_entry;
@@ -431,7 +439,7 @@ fn create_pipeline<T: RenderPassAbstract>(device: Arc<Device>, sub_pass: Subpass
let pipeline;
if is_line {
pipeline = Arc::new(GraphicsPipeline::start()
.vertex_input_single_buffer::<Vertex>()
.vertex_input_single_buffer::<V>()
.vertex_shader(vertex_shader_entry.clone(), ())
.line_list()
.viewports_dynamic_scissors_irrelevant(1)
@@ -441,7 +449,7 @@ fn create_pipeline<T: RenderPassAbstract>(device: Arc<Device>, sub_pass: Subpass
.unwrap());
} else {
pipeline = Arc::new(GraphicsPipeline::start()
.vertex_input_single_buffer::<Vertex>()
.vertex_input_single_buffer::<V>()
.vertex_shader(vertex_shader_entry.clone(), ())
.triangle_list()
.viewports_dynamic_scissors_irrelevant(1)
@@ -500,15 +508,19 @@ fn load_mesh(mesh_path: &str) -> (Vec<Vertex>, Vec<u32>) {
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 };
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);