[Refactor] Renderable
This commit is contained in:
parent
2982f7cd39
commit
96d1e2441f
5 changed files with 25 additions and 14 deletions
|
@ -106,8 +106,8 @@ impl DefaultMesh {
|
|||
}
|
||||
|
||||
impl Renderable for DefaultMesh {
|
||||
fn prepare(&mut self, device: &wgpu::Device) {
|
||||
self.mesh.prepare(device);
|
||||
fn initialize(&mut self, device: &wgpu::Device) {
|
||||
self.mesh.initialize(device);
|
||||
}
|
||||
|
||||
fn update_instances(&mut self, device: &wgpu::Queue) {
|
||||
|
@ -119,8 +119,12 @@ impl Renderable for DefaultMesh {
|
|||
self.mesh.update_instances(device);
|
||||
}
|
||||
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
self.mesh.render(render_pass);
|
||||
fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
self.mesh.prepare(render_pass);
|
||||
}
|
||||
|
||||
fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
self.mesh.draw(render_pass);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ impl Camera {
|
|||
}
|
||||
|
||||
impl Renderable for Camera {
|
||||
fn prepare(&mut self, device: &wgpu::Device) {
|
||||
fn initialize(&mut self, device: &wgpu::Device) {
|
||||
self.update_uniform();
|
||||
|
||||
self.buffer = Some(
|
||||
|
@ -142,9 +142,11 @@ impl Renderable for Camera {
|
|||
);
|
||||
}
|
||||
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_bind_group(1, &self.bind_group.as_ref().unwrap(), &[]);
|
||||
}
|
||||
|
||||
fn draw<'a>(&'a self, _render_pass: &mut wgpu::RenderPass<'a>) { }
|
||||
}
|
||||
|
||||
impl Controllable for Camera {
|
||||
|
|
|
@ -16,7 +16,7 @@ pub struct Mesh {
|
|||
}
|
||||
|
||||
impl Renderable for Mesh {
|
||||
fn prepare(&mut self, device: &Device) {
|
||||
fn initialize(&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),
|
||||
|
@ -54,12 +54,14 @@ impl Renderable for Mesh {
|
|||
);
|
||||
}
|
||||
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
fn prepare<'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(..));
|
||||
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);
|
||||
}
|
||||
|
||||
fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.draw_indexed(0..self.num_indices as _, 0, 0..self.instance_array.len() as _);
|
||||
}
|
||||
}
|
|
@ -17,7 +17,8 @@ mod mesh;
|
|||
pub use mesh::Mesh;
|
||||
|
||||
pub trait Renderable {
|
||||
fn prepare(&mut self, device: &Device);
|
||||
fn initialize(&mut self, device: &Device);
|
||||
fn update_instances(&mut self, queue: &Queue);
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
|
||||
fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
|
||||
fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
|
||||
}
|
10
src/state.rs
10
src/state.rs
|
@ -84,7 +84,7 @@ impl State {
|
|||
});
|
||||
|
||||
let mut camera = Camera::new(config.width as f32, config.height as f32, 0.2);
|
||||
camera.prepare(&device);
|
||||
camera.initialize(&device);
|
||||
|
||||
let depth_texture =
|
||||
Texture::create_depth_texture(&device, &config, "depth_texture");
|
||||
|
@ -155,7 +155,7 @@ impl State {
|
|||
);
|
||||
|
||||
let mut mesh = DefaultMesh::new(diffuse_bind_group, diffuse_bind_group_pikachu);
|
||||
mesh.prepare(&device);
|
||||
mesh.initialize(&device);
|
||||
|
||||
Self {
|
||||
surface,
|
||||
|
@ -235,8 +235,10 @@ impl State {
|
|||
});
|
||||
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
self.camera.render(&mut render_pass);
|
||||
self.mesh.render(&mut render_pass);
|
||||
self.camera.prepare(&mut render_pass);
|
||||
self.mesh.prepare(&mut render_pass);
|
||||
self.camera.draw(&mut render_pass);
|
||||
self.mesh.draw(&mut render_pass);
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
|
|
Loading…
Reference in a new issue