|
|
@@ -0,0 +1,80 @@
|
|
|
+use bytemuck::{Pod, Zeroable};
|
|
|
+
|
|
|
+#[repr(C)]
|
|
|
+#[derive(Debug, Copy, Clone, Pod, Zeroable)]
|
|
|
+pub struct Vertex3 {
|
|
|
+ pub position: [f32; 3],
|
|
|
+ // You can add more Vertex3 attributes here if needed, like normal or texture coordinates
|
|
|
+}
|
|
|
+
|
|
|
+impl Vertex3 {
|
|
|
+ pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
|
|
|
+ wgpu::VertexBufferLayout {
|
|
|
+ array_stride: std::mem::size_of::<Vertex3>() as u64,
|
|
|
+ attributes: &[
|
|
|
+ wgpu::VertexAttribute {
|
|
|
+ offset: 0,
|
|
|
+ shader_location: 0,
|
|
|
+ format: wgpu::VertexFormat::Float32x3,
|
|
|
+ // You can add more Vertex3 attributes here if needed
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ step_mode: wgpu::VertexStepMode::Vertex,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn subdivide_plane2(
|
|
|
+ device: &wgpu::Device,
|
|
|
+ initial_vertices: &[Vertex3],
|
|
|
+ x_min: f32,
|
|
|
+ x_max: f32,
|
|
|
+ y_min: f32,
|
|
|
+ y_max: f32,
|
|
|
+ level: u32,
|
|
|
+) -> (Vec<u8>, u32) {
|
|
|
+ if level == 0 {
|
|
|
+ // Create quad vertices based on (x_min, y_min) and (x_max, y_max)
|
|
|
+ let tl = Vertex3 { position: [x_min, y_min, 0.0] };
|
|
|
+ let tr = Vertex3 { position: [x_max, y_min, 0.0] };
|
|
|
+ let bl = Vertex3 { position: [x_min, y_max, 0.0] };
|
|
|
+ let br = Vertex3 { position: [x_max, y_max, 0.0] };
|
|
|
+ let quad_vertices = vec![tl, tr, br, bl];
|
|
|
+
|
|
|
+ let quad_bytes = bytemuck::cast_slice(&quad_vertices);
|
|
|
+ return (quad_bytes.to_vec(), quad_vertices.len() as u32);
|
|
|
+ }
|
|
|
+
|
|
|
+ let mid_x = (x_min + x_max) / 2.0;
|
|
|
+ let mid_y = (y_min + y_max) / 2.0;
|
|
|
+
|
|
|
+ let (bottom_left, bottom_left_count) = subdivide_plane2(
|
|
|
+ device, initial_vertices, x_min, mid_x, y_min, mid_y, level - 1,
|
|
|
+ );
|
|
|
+ let (bottom_right, bottom_right_count) = subdivide_plane2(
|
|
|
+ device, initial_vertices, mid_x, x_max, y_min, mid_y, level - 1,
|
|
|
+ );
|
|
|
+ let (top_left, top_left_count) = subdivide_plane2(
|
|
|
+ device, initial_vertices, x_min, mid_x, mid_y, y_max, level - 1,
|
|
|
+ );
|
|
|
+ let (top_right, top_right_count) = subdivide_plane2(
|
|
|
+ device, initial_vertices, mid_x, x_max, mid_y, y_max, level - 1,
|
|
|
+ );
|
|
|
+
|
|
|
+ let mut combined_vertices = Vec::with_capacity(
|
|
|
+ bottom_left_count as usize
|
|
|
+ + bottom_right_count as usize
|
|
|
+ + top_left_count as usize
|
|
|
+ + top_right_count as usize,
|
|
|
+ );
|
|
|
+ combined_vertices.extend_from_slice(&bottom_left);
|
|
|
+ combined_vertices.extend_from_slice(&bottom_right);
|
|
|
+ combined_vertices.extend_from_slice(&top_left);
|
|
|
+ combined_vertices.extend_from_slice(&top_right);
|
|
|
+
|
|
|
+ let combined_bytes = bytemuck::cast_slice(&combined_vertices);
|
|
|
+
|
|
|
+ let total_Vertex3_count = bottom_left_count + bottom_right_count + top_left_count + top_right_count;
|
|
|
+
|
|
|
+ (combined_bytes.to_vec(), total_Vertex3_count)
|
|
|
+}
|