mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-15 13:24:20 +00:00
Index buffer
This commit is contained in:
@ -23,7 +23,7 @@ env_logger = "0.10"
|
||||
log = "0.4"
|
||||
wgpu = "0.18"
|
||||
winit = "0.28"
|
||||
bytemuck = { version = "1.12", features = [ "derive" ] }
|
||||
bytemuck = { version = "1.12", features = ["derive"] }
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
tokio = { version = "1.36.0", features = ["full"] }
|
||||
|
46
src/lib.rs
46
src/lib.rs
@ -107,7 +107,8 @@ pub struct State {
|
||||
window: Window,
|
||||
render_pipeline: wgpu::RenderPipeline,
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
num_vertices: u32,
|
||||
index_buffer: wgpu::Buffer,
|
||||
num_indices: u32,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@ -198,6 +199,14 @@ impl State {
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
@ -238,8 +247,6 @@ impl State {
|
||||
multiview: None, // 5.
|
||||
});
|
||||
|
||||
let num_vertices = VERTICES.len() as u32;
|
||||
|
||||
Self {
|
||||
window,
|
||||
surface,
|
||||
@ -249,7 +256,8 @@ impl State {
|
||||
size,
|
||||
render_pipeline,
|
||||
vertex_buffer,
|
||||
num_vertices,
|
||||
index_buffer,
|
||||
num_indices,
|
||||
}
|
||||
}
|
||||
|
||||
@ -306,9 +314,10 @@ impl State {
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&self.render_pipeline); // 2.
|
||||
render_pass.set_pipeline(&self.render_pipeline);
|
||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||
render_pass.draw(0..3, 0..1);
|
||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16); // 1.
|
||||
render_pass.draw_indexed(0..self.num_indices, 0, 0..1); // 2.
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
@ -347,17 +356,18 @@ impl Vertex {
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
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] }, // A
|
||||
Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.5, 0.0, 0.5] }, // B
|
||||
Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.5, 0.0, 0.5] }, // C
|
||||
Vertex { position: [0.35966998, -0.3473291, 0.0], color: [0.5, 0.0, 0.5] }, // D
|
||||
Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.5, 0.0, 0.5] }, // E
|
||||
];
|
||||
|
||||
#[rustfmt::skip]
|
||||
const INDICES: &[u16] = &[
|
||||
0, 1, 4,
|
||||
1, 2, 4,
|
||||
2, 3, 4,
|
||||
];
|
||||
|
Reference in New Issue
Block a user