diff --git a/src/renderer/utils.rs b/src/renderer/utils.rs index e75cf85..80be958 100644 --- a/src/renderer/utils.rs +++ b/src/renderer/utils.rs @@ -82,7 +82,19 @@ impl ColorVertex { } } -//--GpuMesh struct---------------------------------------------------------------------------------- +//--Mesh struct------------------------------------------------------------------------------------- + +/// Handles the buffers to be used to draw a Mesh +/// +/// The mesh consist of an array of vertices, and its ccorresponding indices. +/// +/// A vertex can be any type that can be converted to a byte array using the bytemuck crate. +/// Generaly, adding `#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]` to a struct +/// is enough for that. +/// +/// An index is a standard u16 +/// +/// A mesh can be used with, and only with, the renderer for wich it was created pub struct Mesh where V: Copy + Clone + bytemuck::Pod + bytemuck::Zeroable, @@ -97,6 +109,7 @@ impl Mesh where V: Copy + Clone + bytemuck::Pod + bytemuck::Zeroable + std::fmt::Debug { + /// Creates a new mesh using the [Device] from the renderer to be used pub fn new(device: &wgpu::Device) -> Self { use std::mem::size_of; use wgpu_types::BufferUsages as Usages; @@ -126,18 +139,26 @@ where } } + /// Updates the vertices of the [Mesh]. The modification will be applied before the next + /// rendering of the [Mesh]. The function will fail if the given slice has more elements than + /// specified at the [Mesh] creation pub fn set_vertices(&mut self, renderer: &WgpuRenderer, vertices: &[V]) { renderer.queue.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(vertices)); } + /// Updates the indices of the [Mesh]. The modification will be applied before the next + /// rendering of the [Mesh]. The function will fail if the given slice has more elements than + /// specified at the [Mesh] creation pub fn set_indices(&mut self, renderer: &WgpuRenderer, indices: &[u16]) { renderer.queue.write_buffer(&self.index_buffer, 0, bytemuck::cast_slice(indices)); } + /// Returns a [BufferSlice] containing the vertices, to be used in a render pass pub fn get_vertex_buffer_slice(&self) -> wgpu::BufferSlice { self.vertex_buffer.slice(..) } + /// Returns a [BufferSlice] containing the indices, to be used in a render pass pub fn get_index_buffer_slice(&self) -> wgpu::BufferSlice { self.index_buffer.slice(..) }