152 lines
No EOL
5.8 KiB
Rust
152 lines
No EOL
5.8 KiB
Rust
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<()> {
|
|
|
|
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();
|
|
// 0 top left far
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, 0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 1 top right far
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, 0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 2 top left near
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, 0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 3 top right near
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, 0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
|
|
// 4 bottom left far
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, -0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 5 bottom right far
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, -0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 6 bottom left near
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, -0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
// 7 bottom right near
|
|
self.vertices.push(vertex::Vertex::new(
|
|
vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, -0.5),
|
|
vec3(0.0, 1.0, 0.0),
|
|
vec2(0.0, 0.0)
|
|
));
|
|
|
|
|
|
// top
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 0);
|
|
self.indices.push(index as u32 + 2);
|
|
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 1);
|
|
self.indices.push(index as u32 + 0);
|
|
|
|
// bottom
|
|
self.indices.push(index as u32 + 6);
|
|
self.indices.push(index as u32 + 4);
|
|
self.indices.push(index as u32 + 7);
|
|
|
|
self.indices.push(index as u32 + 4);
|
|
self.indices.push(index as u32 + 5);
|
|
self.indices.push(index as u32 + 7);
|
|
|
|
// left
|
|
self.indices.push(index as u32 + 0);
|
|
self.indices.push(index as u32 + 4);
|
|
self.indices.push(index as u32 + 2);
|
|
|
|
self.indices.push(index as u32 + 6);
|
|
self.indices.push(index as u32 + 2);
|
|
self.indices.push(index as u32 + 4);
|
|
|
|
// right
|
|
self.indices.push(index as u32 + 1);
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 5);
|
|
|
|
self.indices.push(index as u32 + 5);
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 7);
|
|
|
|
// near
|
|
self.indices.push(index as u32 + 6);
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 2);
|
|
|
|
self.indices.push(index as u32 + 3);
|
|
self.indices.push(index as u32 + 6);
|
|
self.indices.push(index as u32 + 7);
|
|
|
|
// far
|
|
self.indices.push(index as u32 + 0);
|
|
self.indices.push(index as u32 + 1);
|
|
self.indices.push(index as u32 + 4);
|
|
|
|
self.indices.push(index as u32 + 5);
|
|
self.indices.push(index as u32 + 4);
|
|
self.indices.push(index as u32 + 1);
|
|
}
|
|
}
|
|
|
|
(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);
|
|
}
|
|
} |