VoxelEngine2/src/scene.rs
2024-06-02 15:25:11 +02:00

58 lines
No EOL
2 KiB
Rust

use std::thread::yield_now;
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;
use crate::primitives::cube::Cube;
#[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<()> {
let grid_size = 1000;
//todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting?
// have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives
for x_index in -grid_size..grid_size {
for y_index in -grid_size..grid_size {
let index = self.vertices.len();
let cube = Cube {
pos: vec3(x_index as f32, y_index as f32, 0.0),
color: vec3(0.0, 1.0, 0.0),
tex_coord: vec2(0.0, 0.0)
};
cube.draw(&vk::PrimitiveTopology::TRIANGLE_LIST, index, self);
}
}
(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);
}
}