Added index buffer
This commit is contained in:
parent
dac885748a
commit
99d5b76f02
@ -38,9 +38,17 @@ impl Vertex {
|
||||
}
|
||||
|
||||
const VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [-0.5, -0.5, 0.0], color: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [-0.0868241, 0.49240386, 0.0], color: [0.5, 0.0, 0.5] },
|
||||
Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.5, 0.0, 0.5] },
|
||||
Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.5, 0.0, 0.5] },
|
||||
Vertex { position: [0.35966998, -0.3473291, 0.0], color: [0.5, 0.0, 0.5] },
|
||||
Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.5, 0.0, 0.5] },
|
||||
];
|
||||
|
||||
const INDICES: &[u16] = &[
|
||||
0, 1, 4,
|
||||
1, 2, 4,
|
||||
2, 3, 4,
|
||||
];
|
||||
|
||||
struct StateApplication<'a> {
|
||||
@ -105,7 +113,10 @@ struct State<'a> {
|
||||
|
||||
render_pipeline: wgpu::RenderPipeline,
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
index_buffer: wgpu::Buffer,
|
||||
|
||||
num_vertices: u32,
|
||||
num_indices: u32,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
@ -122,8 +133,10 @@ impl<'a> State<'a> {
|
||||
|
||||
let render_pipeline = Self::create_render_pipeline(&device, &config);
|
||||
let vertex_buffer = Self::create_vertex_buffer(&device);
|
||||
let index_buffer = Self::create_index_buffer(&device);
|
||||
|
||||
let num_vertices = VERTICES.len() as u32;
|
||||
let num_indices = INDICES.len() as u32;
|
||||
|
||||
Self {
|
||||
surface,
|
||||
@ -135,8 +148,10 @@ impl<'a> State<'a> {
|
||||
|
||||
render_pipeline,
|
||||
vertex_buffer,
|
||||
index_buffer,
|
||||
|
||||
num_vertices,
|
||||
num_indices,
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,6 +163,16 @@ impl<'a> State<'a> {
|
||||
// TODO: Update logic here
|
||||
}
|
||||
|
||||
fn create_index_buffer(device: &Device) -> wgpu::Buffer {
|
||||
device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Index Buffer"),
|
||||
contents: bytemuck::cast_slice(INDICES),
|
||||
usage: wgpu::BufferUsages::INDEX,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn create_vertex_buffer(device: &Device) -> wgpu::Buffer {
|
||||
device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
@ -301,7 +326,8 @@ impl<'a> State<'a> {
|
||||
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||
render_pass.draw(0..self.num_vertices, 0..1);
|
||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
|
||||
}
|
||||
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user