oxide_renderer/mesh/
mod.rs1mod primitive;
4mod vertex;
5
6pub use primitive::*;
7pub use vertex::{triangle_vertices, Vertex, Vertex3D};
8
9use wgpu::{Buffer, BufferDescriptor, BufferUsages, Device};
10
11pub struct Mesh3D {
12 pub vertex_buffer: Buffer,
13 pub index_buffer: Buffer,
14 pub index_count: u32,
15}
16
17impl Mesh3D {
18 pub fn new_cube(device: &Device) -> Self {
19 let vertices = cube_vertices();
20 let indices = cube_indices();
21
22 Self::create(device, &vertices, &indices, Some("Cube"))
23 }
24
25 pub fn new_sphere(device: &Device, segments: u32, rings: u32) -> Self {
26 let vertices = sphere_vertices(segments, rings);
27 let indices = sphere_indices(segments, rings);
28
29 Self::create(device, &vertices, &indices, Some("Sphere"))
30 }
31
32 pub fn create(
34 device: &Device,
35 vertices: &[Vertex3D],
36 indices: &[u16],
37 label: Option<&str>,
38 ) -> Self {
39 let vertex_buffer = device.create_buffer(&BufferDescriptor {
40 label: label.map(|l| format!("{} Vertex Buffer", l)).as_deref(),
41 size: std::mem::size_of_val(vertices) as wgpu::BufferAddress,
42 usage: BufferUsages::VERTEX,
43 mapped_at_creation: true,
44 });
45
46 vertex_buffer
47 .slice(..)
48 .get_mapped_range_mut()
49 .copy_from_slice(bytemuck::cast_slice(vertices));
50 vertex_buffer.unmap();
51
52 let index_buffer = device.create_buffer(&BufferDescriptor {
53 label: label.map(|l| format!("{} Index Buffer", l)).as_deref(),
54 size: std::mem::size_of_val(indices) as wgpu::BufferAddress,
55 usage: BufferUsages::INDEX,
56 mapped_at_creation: true,
57 });
58
59 index_buffer
60 .slice(..)
61 .get_mapped_range_mut()
62 .copy_from_slice(bytemuck::cast_slice(indices));
63 index_buffer.unmap();
64
65 Self {
66 vertex_buffer,
67 index_buffer,
68 index_count: indices.len() as u32,
69 }
70 }
71}
72
73pub struct Mesh {
74 pub vertex_buffer: Buffer,
75 pub vertex_count: u32,
76}
77
78impl Mesh {
79 pub fn new_triangle(device: &Device) -> Self {
80 let vertices = triangle_vertices();
81
82 let vertex_buffer = device.create_buffer(&BufferDescriptor {
83 label: Some("Triangle Vertex Buffer"),
84 size: std::mem::size_of_val(&vertices) as wgpu::BufferAddress,
85 usage: BufferUsages::VERTEX,
86 mapped_at_creation: true,
87 });
88
89 vertex_buffer
90 .slice(..)
91 .get_mapped_range_mut()
92 .copy_from_slice(bytemuck::cast_slice(&vertices));
93 vertex_buffer.unmap();
94
95 Self {
96 vertex_buffer,
97 vertex_count: vertices.len() as u32,
98 }
99 }
100}