Created
April 17, 2022 18:41
-
-
Save hikilaka/9b3602f3a3a0260378bfaf750d1d92bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use wgpu::{Buffer, BufferUsages, Device}; | |
use wgpu::util::{BufferInitDescriptor, DeviceExt}; | |
use crate::vertex::Vertex; | |
pub struct Mesh { | |
vertex_buffer: Buffer, | |
index_buffer: Buffer, | |
vertex_count: usize, | |
index_count: usize, | |
} | |
impl Mesh { | |
pub fn new(vertex_buffer: Buffer, index_buffer: Buffer, vertex_count: usize, index_count: usize) -> Self { | |
Self { | |
vertex_buffer, | |
index_buffer, | |
vertex_count, | |
index_count, | |
} | |
} | |
pub fn get_vertex_buffer(&self) -> &Buffer { | |
&self.vertex_buffer | |
} | |
pub fn get_index_buffer(&self) -> &Buffer { | |
&self.index_buffer | |
} | |
pub fn get_vertex_count(&self) -> u32 { | |
self.vertex_count as u32 | |
} | |
pub fn get_index_count(&self) -> u32 { | |
self.index_count as u32 | |
} | |
} | |
pub struct MeshBuilder { | |
vertices: Vec<Vertex>, | |
indices: Vec<u16>, | |
} | |
impl MeshBuilder { | |
pub fn new() -> Self { | |
Self { | |
vertices: Vec::new(), | |
indices: Vec::new(), | |
} | |
} | |
pub fn add_vertex(&mut self, vertex: Vertex) -> &mut Self { | |
self.vertices.push(vertex); | |
self | |
} | |
pub fn add_index(&mut self, index: u16) -> &mut Self { | |
self.indices.push(index); | |
self | |
} | |
pub fn build(&self, device: &Device) -> Mesh { | |
let vertex_buffer = device.create_buffer_init(&BufferInitDescriptor { | |
label: Some("Vertex Buffer"), | |
contents: bytemuck::cast_slice(self.vertices.as_slice()), | |
usage: BufferUsages::VERTEX, | |
}); | |
let index_buffer = device.create_buffer_init(&BufferInitDescriptor { | |
label: Some("Index Buffer"), | |
contents: bytemuck::cast_slice(self.indices.as_slice()), | |
usage: BufferUsages::INDEX, | |
}); | |
Mesh::new(vertex_buffer, index_buffer, self.vertices.len(), self.indices.len()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment