diff --git a/src/main.rs b/src/main.rs index 341074f..4fbe008 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 ); diff --git a/src/vulkan.rs b/src/vulkan.rs index 8a4eb63..d833820 100644 --- a/src/vulkan.rs +++ b/src/vulkan.rs @@ -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, + pub line_vertices: Vec, 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, game: &mut dyn Game) { +pub fn init(mesh_path: &str, line_vertices: Vec, game: &mut dyn Game) { let mut data = GameData { push_constants: PushConstants { time: 0.0, @@ -404,7 +412,7 @@ pub mod fs { } } -fn create_pipeline(device: Arc, sub_pass: Subpass>, vertex_shader_path: &str, fragment_shader_path: &str, is_line: bool) -> Option, Box, Arc>>> { +fn create_pipeline(device: Arc, sub_pass: Subpass>, vertex_shader_path: &str, fragment_shader_path: &str, is_line: bool) -> Option, Box, Arc>>> { 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(device: Arc, sub_pass: Subpass let pipeline; if is_line { pipeline = Arc::new(GraphicsPipeline::start() - .vertex_input_single_buffer::() + .vertex_input_single_buffer::() .vertex_shader(vertex_shader_entry.clone(), ()) .line_list() .viewports_dynamic_scissors_irrelevant(1) @@ -441,7 +449,7 @@ fn create_pipeline(device: Arc, sub_pass: Subpass .unwrap()); } else { pipeline = Arc::new(GraphicsPipeline::start() - .vertex_input_single_buffer::() + .vertex_input_single_buffer::() .vertex_shader(vertex_shader_entry.clone(), ()) .triangle_list() .viewports_dynamic_scissors_irrelevant(1) @@ -500,15 +508,19 @@ fn load_mesh(mesh_path: &str) -> (Vec, Vec) { 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);