Index buffer

This commit is contained in:
Werner
2021-11-02 20:07:50 -03:00
parent bb3b163e26
commit 52c16d7b69

View File

@ -43,6 +43,11 @@ const VERTICES: &[Vertex] = &[
Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] }, Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] },
]; ];
#[rustfmt::skip]
const INDICES: &[u16] = &[
0, 1, 2,
];
struct State { struct State {
surface: wgpu::Surface, surface: wgpu::Surface,
device: wgpu::Device, device: wgpu::Device,
@ -51,7 +56,8 @@ struct State {
size: winit::dpi::PhysicalSize<u32>, size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline, render_pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer, vertex_buffer: wgpu::Buffer,
num_vertices: u32, index_buffer: wgpu::Buffer,
num_indices: u32,
} }
impl State { impl State {
@ -152,7 +158,13 @@ impl State {
usage: wgpu::BufferUsages::VERTEX, usage: wgpu::BufferUsages::VERTEX,
}); });
let num_vertices = VERTICES.len() as u32; let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(INDICES),
usage: wgpu::BufferUsages::INDEX,
});
let num_indices = INDICES.len() as u32;
Self { Self {
surface, surface,
@ -162,7 +174,8 @@ impl State {
size, size,
render_pipeline, render_pipeline,
vertex_buffer, vertex_buffer,
num_vertices, index_buffer,
num_indices,
} }
} }
@ -217,8 +230,8 @@ impl State {
render_pass.set_pipeline(&self.render_pipeline); render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..)); render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw(0..self.num_vertices, 0..1); render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
} }
// submit will accept anything that implements IntoIter // submit will accept anything that implements IntoIter