2019-12-02 09:31:07 +00:00
|
|
|
use crate::{vertex::Vertex, asset::Asset};
|
|
|
|
use wgpu::{Buffer, Device};
|
2019-12-02 18:48:08 +00:00
|
|
|
use zerocopy::AsBytes;
|
2019-12-02 09:31:07 +00:00
|
|
|
|
|
|
|
pub enum MeshType {
|
|
|
|
Cube,
|
|
|
|
Plane {
|
|
|
|
size: i8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Mesh {
|
|
|
|
pub vertices: Vec<Vertex>,
|
|
|
|
pub indices: Vec<u16>,
|
|
|
|
pub vertex_buffer: Option<Buffer>,
|
|
|
|
pub index_buffer: Option<Buffer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mesh {
|
|
|
|
pub fn setup_buffers(&mut self, device: &Device) {
|
|
|
|
if let None = self.vertex_buffer {
|
|
|
|
self.vertex_buffer = Some(device.create_buffer_with_data(self.vertices.as_bytes(), wgpu::BufferUsage::VERTEX));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let None = self.index_buffer {
|
|
|
|
self.index_buffer = Some(device.create_buffer_with_data(self.indices.as_bytes(), wgpu::BufferUsage::INDEX));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Asset<MeshType> for Mesh {
|
|
|
|
fn load(descriptor: MeshType) -> Self {
|
|
|
|
let (vertices, indices) = match descriptor {
|
|
|
|
MeshType::Cube => create_cube(),
|
|
|
|
MeshType::Plane { size } => create_plane(size),
|
|
|
|
};
|
|
|
|
|
|
|
|
Mesh {
|
|
|
|
vertices,
|
|
|
|
indices,
|
|
|
|
vertex_buffer: None,
|
|
|
|
index_buffer: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex {
|
|
|
|
Vertex {
|
|
|
|
pos: [pos[0], pos[1], pos[2], 1],
|
|
|
|
normal: [nor[0], nor[1], nor[2], 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
|
|
|
|
let vertex_data = [
|
|
|
|
// top (0, 0, 1)
|
|
|
|
vertex([-1, -1, 1], [0, 0, 1]),
|
|
|
|
vertex([1, -1, 1], [0, 0, 1]),
|
|
|
|
vertex([1, 1, 1], [0, 0, 1]),
|
|
|
|
vertex([-1, 1, 1], [0, 0, 1]),
|
|
|
|
// bottom (0, 0, -1)
|
|
|
|
vertex([-1, 1, -1], [0, 0, -1]),
|
|
|
|
vertex([1, 1, -1], [0, 0, -1]),
|
|
|
|
vertex([1, -1, -1], [0, 0, -1]),
|
|
|
|
vertex([-1, -1, -1], [0, 0, -1]),
|
|
|
|
// right (1, 0, 0)
|
|
|
|
vertex([1, -1, -1], [1, 0, 0]),
|
|
|
|
vertex([1, 1, -1], [1, 0, 0]),
|
|
|
|
vertex([1, 1, 1], [1, 0, 0]),
|
|
|
|
vertex([1, -1, 1], [1, 0, 0]),
|
|
|
|
// left (-1, 0, 0)
|
|
|
|
vertex([-1, -1, 1], [-1, 0, 0]),
|
|
|
|
vertex([-1, 1, 1], [-1, 0, 0]),
|
|
|
|
vertex([-1, 1, -1], [-1, 0, 0]),
|
|
|
|
vertex([-1, -1, -1], [-1, 0, 0]),
|
|
|
|
// front (0, 1, 0)
|
|
|
|
vertex([1, 1, -1], [0, 1, 0]),
|
|
|
|
vertex([-1, 1, -1], [0, 1, 0]),
|
|
|
|
vertex([-1, 1, 1], [0, 1, 0]),
|
|
|
|
vertex([1, 1, 1], [0, 1, 0]),
|
|
|
|
// back (0, -1, 0)
|
|
|
|
vertex([1, -1, 1], [0, -1, 0]),
|
|
|
|
vertex([-1, -1, 1], [0, -1, 0]),
|
|
|
|
vertex([-1, -1, -1], [0, -1, 0]),
|
|
|
|
vertex([1, -1, -1], [0, -1, 0]),
|
|
|
|
];
|
|
|
|
|
|
|
|
let index_data: &[u16] = &[
|
|
|
|
0, 1, 2, 2, 3, 0, // top
|
|
|
|
4, 5, 6, 6, 7, 4, // bottom
|
|
|
|
8, 9, 10, 10, 11, 8, // right
|
|
|
|
12, 13, 14, 14, 15, 12, // left
|
|
|
|
16, 17, 18, 18, 19, 16, // front
|
|
|
|
20, 21, 22, 22, 23, 20, // back
|
|
|
|
];
|
|
|
|
|
|
|
|
(vertex_data.to_vec(), index_data.to_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_plane(size: i8) -> (Vec<Vertex>, Vec<u16>) {
|
|
|
|
let vertex_data = [
|
|
|
|
vertex([size, -size, 0], [0, 0, 1]),
|
|
|
|
vertex([size, size, 0], [0, 0, 1]),
|
|
|
|
vertex([-size, -size, 0], [0, 0, 1]),
|
|
|
|
vertex([-size, size, 0], [0, 0, 1]),
|
|
|
|
];
|
|
|
|
|
|
|
|
let index_data: &[u16] = &[0, 1, 2, 2, 1, 3];
|
|
|
|
|
|
|
|
(vertex_data.to_vec(), index_data.to_vec())
|
|
|
|
}
|