|
|
@@ -1,4 +1,4 @@
|
|
|
-use crate::vertex::Vertex;
|
|
|
+use crate::vertex::{Vertex, Vertex2};
|
|
|
//Debug Triangle
|
|
|
pub const VERTICES: &[Vertex] = &[
|
|
|
Vertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
|
|
|
@@ -30,3 +30,39 @@ pub const VERT3 : &[Vertex] = &[
|
|
|
Vertex { position: [-0.5, 0.5, 0.0], color: [0.0, 0.0, 1.0] },
|
|
|
];
|
|
|
|
|
|
+fn subdivide_plane(device: &wgpu::Device, vertices: &mut Vec<Vertex2>, x_min: f32, x_max: f32, y_min: f32, y_max: f32, level: u32) {
|
|
|
+ if level == 0 {
|
|
|
+ let half_width = (x_max - x_min) / 2.0;
|
|
|
+ let half_height = (y_max - y_min) / 2.0;
|
|
|
+
|
|
|
+ // Generate vertices for a single quad based on (x_min, y_min) and (x_max, y_max)
|
|
|
+ vertices.push(Vertex2 { position: [x_min, y_max, 0.0], uv: [0.0, 0.0] }); // Top-left
|
|
|
+ vertices.push(Vertex2 { position: [x_max, y_max, 0.0], uv: [1.0, 0.0] }); // Top-right
|
|
|
+ vertices.push(Vertex2 { position: [x_max, y_min, 0.0], uv: [1.0, 1.0] }); // Bottom-right
|
|
|
+ vertices.push(Vertex2 { position: [x_min, y_min, 0.0], uv: [0.0, 1.0] }); // Bottom-left
|
|
|
+ } else {
|
|
|
+ let mid_x = (x_min + x_max) / 2.0;
|
|
|
+ let mid_y = (y_min + y_max) / 2.0;
|
|
|
+
|
|
|
+ subdivide_plane(device, vertices, x_min, mid_x, y_min, mid_y, level - 1);
|
|
|
+ subdivide_plane(device, vertices, mid_x, x_max, y_min, mid_y, level - 1);
|
|
|
+ subdivide_plane(device, vertices, x_min, mid_x, mid_y, y_max, level - 1);
|
|
|
+ subdivide_plane(device, vertices, mid_x, x_max, mid_y, y_max, level - 1);
|
|
|
+
|
|
|
+ // Alternatively, to create vertex buffer on each recursive call (less memory usage on CPU):
|
|
|
+ // let mut sub_vertices = Vec::new();
|
|
|
+ // subdivide_plane(device, &mut sub_vertices, x_min, mid_x, y_min, mid_y, level - 1);
|
|
|
+ // // ... repeat for other quadrants
|
|
|
+ //
|
|
|
+ // // Create vertex buffer on GPU from sub_vertices
|
|
|
+ // let vertex_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
+ // usage: wgpu::BufferUsages::VERTEX,
|
|
|
+ // mapping: wgpu::BufferMapping::WRITE,
|
|
|
+ // size: sub_vertices.len() * std::mem::size_of::<Vertex>(),
|
|
|
+ // });
|
|
|
+ // let mut buffer_slice = vertex_buffer.slice(..);
|
|
|
+ // buffer_slice.write(&sub_vertices);
|
|
|
+ // // ... (use vertex_buffer for rendering)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|