1
0
Fork 0

[REFACTOR] Begin refactor mesh

This commit is contained in:
Florian RICHER 2022-06-15 14:04:10 +02:00
parent 0c1dabb99a
commit 2fb1a1dc7c
2 changed files with 73 additions and 1 deletions

63
src/render/mesh.rs Normal file
View file

@ -0,0 +1,63 @@
use wgpu::{Device, util::DeviceExt, Queue};
use super::{Vertex, Renderable, Instance};
pub struct Mesh {
vertex_array: Vec<Vertex>,
index_array: Vec<u16>,
num_indices: u32,
instance_array: Vec<Instance>,
texture_bind_group: Option<wgpu::BindGroup>,
vertex_buffer: Option<wgpu::Buffer>,
index_buffer: Option<wgpu::Buffer>,
instance_buffer: Option<wgpu::Buffer>,
}
impl Renderable for Mesh {
fn prepare(&mut self, device: &Device) {
self.vertex_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(&self.vertex_array),
usage: wgpu::BufferUsages::VERTEX,
}));
self.index_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(&self.index_array),
usage: wgpu::BufferUsages::INDEX,
}));
self.num_indices = self.index_array.len() as u32;
let instance_data = self.instance_array
.iter()
.map(Instance::to_raw)
.collect::<Vec<_>>();
self.instance_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Instance Buffer"),
contents: bytemuck::cast_slice(&instance_data),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
}));
}
fn update_instances(&mut self, queue: &Queue) {
let instance_data = self
.instance_array
.iter()
.map(Instance::to_raw)
.collect::<Vec<_>>();
queue.write_buffer(
&self.instance_buffer.as_ref().unwrap(),
0,
bytemuck::cast_slice(&instance_data),
);
}
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
render_pass.set_bind_group(0, &self.texture_bind_group.as_ref().unwrap(), &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.as_ref().unwrap().slice(..).clone());
render_pass.set_vertex_buffer(1, self.instance_buffer.as_ref().unwrap().slice(..));
render_pass.set_index_buffer(self.index_buffer.as_ref().unwrap().slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.index_array.len() as _, 0, 0..self.num_indices);
}
}

View file

@ -12,4 +12,13 @@ pub use texture::Texture;
mod instance;
pub use instance::{
Instance, InstanceRaw
};
};
use wgpu::{Device, Queue};
mod mesh;
pub trait Renderable {
fn prepare(&mut self, device: &Device);
fn update_instances(&mut self, device: &Queue);
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
}