moves vertex and index buffer handling to scene
This commit is contained in:
parent
0b3be29525
commit
32577d548b
6 changed files with 111 additions and 79 deletions
src
74
src/scene.rs
Normal file
74
src/scene.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
use vulkanalia::prelude::v1_0::*;
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use cgmath::{vec2, vec3, Matrix, SquareMatrix};
|
||||
|
||||
use crate::app_data::AppData;
|
||||
use crate::buffer;
|
||||
use crate::vertex;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Scene {
|
||||
pub vertices: Vec<vertex::Vertex>,
|
||||
pub indices: Vec<u32>,
|
||||
|
||||
pub vertex_buffer: vk::Buffer,
|
||||
pub vertex_buffer_memory: vk::DeviceMemory,
|
||||
|
||||
pub index_buffer: vk::Buffer,
|
||||
pub index_buffer_memory: vk::DeviceMemory,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> {
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(8.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(0);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(-8.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(1);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(0.0, 8.0, 0.0), vec3(1.0, 1.0, 0.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(2);
|
||||
|
||||
self.vertices.push(
|
||||
vertex::Vertex::new(vec3(0.0, -8.0, 0.0), vec3(0.0, 1.0, 1.0), vec2(0.0, 0.0))
|
||||
);
|
||||
self.indices.push(3);
|
||||
|
||||
let grid_size = 1;
|
||||
for x_index in -grid_size..grid_size {
|
||||
for y_index in -grid_size..grid_size {
|
||||
if !(((x_index as i32).abs() == 8 && y_index == 0) || (x_index == 0 && (y_index as i32).abs() == 8)){
|
||||
let index = self.indices.len();
|
||||
let vert = vertex::Vertex::new(
|
||||
vec3(x_index as f32, y_index as f32, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec2(0.0, 0.0)
|
||||
);
|
||||
self.vertices.push(vert);
|
||||
self.indices.push(index as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(self.vertex_buffer, self.vertex_buffer_memory) = buffer::create_vertex_buffer(instance, device, &data, &self.vertices)?;
|
||||
(self.index_buffer, self.index_buffer_memory) = buffer::create_index_buffer(&instance, &device, &data, &self.indices)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
}
|
||||
|
||||
pub unsafe fn destroy(&mut self, device: &vulkanalia::Device) {
|
||||
device.destroy_buffer(self.index_buffer, None);
|
||||
device.free_memory(self.index_buffer_memory, None);
|
||||
|
||||
device.destroy_buffer(self.vertex_buffer, None);
|
||||
device.free_memory(self.vertex_buffer_memory, None);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue