mesh loading, broken mesh

This commit is contained in:
zomseffen 2024-04-26 18:22:14 +02:00
parent f6276bfdf6
commit 31d4a139ef
9 changed files with 16153 additions and 27 deletions

View file

@ -5,32 +5,12 @@ use vulkanalia::prelude::v1_0::*;
use std::mem::size_of;
use std::ptr::copy_nonoverlapping as memcpy;
use cgmath::{vec2, vec3};
pub type Mat4 = cgmath::Matrix4<f32>;
use crate::app_data;
use crate::vertex;
use crate::command_buffer;
static VERTICES: [vertex::Vertex; 8] = [
vertex::Vertex::new(vec3(-0.5, -0.5, 0.0), vec3(1.0, 0.0, 0.0), vec2(1.0, 0.0)),
vertex::Vertex::new(vec3(0.5, -0.5, 0.0), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0)),
vertex::Vertex::new(vec3(0.5, 0.5, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.0, 1.0)),
vertex::Vertex::new(vec3(-0.5, 0.5, 0.0), vec3(1.0, 1.0, 1.0), vec2(1.0, 1.0)),
vertex::Vertex::new(vec3(-0.5, -0.5, -0.5), vec3(1.0, 0.0, 0.0), vec2(1.0, 0.0)),
vertex::Vertex::new(vec3(0.5, -0.5, -0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0)),
vertex::Vertex::new(vec3(0.5, 0.5, -0.5), vec3(0.0, 0.0, 1.0), vec2(0.0, 1.0)),
vertex::Vertex::new(vec3(-0.5, 0.5, -0.5), vec3(1.0, 1.0, 1.0), vec2(1.0, 1.0)),
];
pub const INDICES: &[u16] = &[
0, 1, 2,
2, 3, 0,
4, 5, 6,
6, 7, 4,
];
pub unsafe fn create_buffer(
instance: &Instance,
device: &Device,
@ -69,7 +49,7 @@ pub unsafe fn create_vertex_buffer(
device: &Device,
data: &mut app_data::AppData,
) -> Result<()> {
let size = (size_of::<vertex::Vertex>() * VERTICES.len()) as u64;
let size = (size_of::<vertex::Vertex>() * data.vertices.len()) as u64;
let (staging_buffer, staging_buffer_memory) = create_buffer(
instance,
@ -87,7 +67,7 @@ pub unsafe fn create_vertex_buffer(
vk::MemoryMapFlags::empty(),
)?;
memcpy(VERTICES.as_ptr(), memory.cast(), VERTICES.len());
memcpy(data.vertices.as_ptr(), memory.cast(), data.vertices.len());
device.unmap_memory(staging_buffer_memory);
@ -150,7 +130,7 @@ pub unsafe fn create_index_buffer(
device: &Device,
data: &mut app_data::AppData,
) -> Result<()> {
let size = (size_of::<u16>() * INDICES.len()) as u64;
let size = (size_of::<u16>() * data.indices.len()) as u64;
let (staging_buffer, staging_buffer_memory) = create_buffer(
instance,
@ -168,7 +148,7 @@ pub unsafe fn create_index_buffer(
vk::MemoryMapFlags::empty(),
)?;
memcpy(INDICES.as_ptr(), memory.cast(), INDICES.len());
memcpy(data.indices.as_ptr(), memory.cast(), data.indices.len());
device.unmap_memory(staging_buffer_memory);