Browse Source

horribly fucked

Ghastrod 1 năm trước cách đây
mục cha
commit
67849bb8ab
1 tập tin đã thay đổi với 75 bổ sung27 xóa
  1. 75 27
      src/lib.rs

+ 75 - 27
src/lib.rs

@@ -13,7 +13,7 @@ use winit::{
 #[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
 struct Vertex {
     position: [f32; 3],
-    tex_coords: [f32; 2],
+    color: [f32; 3],
 }
 
 
@@ -31,7 +31,7 @@ impl Vertex {
                 wgpu::VertexAttribute {
                     offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
                     shader_location: 1,
-                    format: wgpu::VertexFormat::Float32x2,
+                    format: wgpu::VertexFormat::Float32x3,
                 },
             ],
         }
@@ -39,26 +39,73 @@ impl Vertex {
 }
 
 
-// Changed
-const VERTICES: &[Vertex] = &[
-    // Changed
-    Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.00759614], }, // A
-    Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.43041354], }, // B
-    Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.949397], }, // C
-    Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.84732914], }, // D
-    Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.2652641], }, // E
-];
-
-
-
-const INDICES: &[u16] = &[
-    0, 1, 4,
-    1, 2, 4,
-    2, 3, 4,
-];
-
-
-
+// // Changed
+// const VERTICES: &[Vertex] = &[
+//     // Changed
+//     Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.00759614], }, // A
+//     Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.43041354], }, // B
+//     Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.949397], }, // C
+//     Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.84732914], }, // D
+//     Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.2652641], }, // E
+// ];
+
+
+
+// const INDICES: &[u16] = &[
+//     0, 1, 4,
+//     1, 2, 4,
+//     2, 3, 4,
+// ];
+
+fn creer_plan_subdivise(largeur: f32, hauteur: f32, niveau_subdivision: usize) -> (Vec<Vertex>, Vec<u32>) {
+
+    // Vecteurs pour stocker les vertices et les indices
+    let mut vertices: Vec<Vertex> = Vec::new();
+    let mut indices: Vec<u32> = Vec::new();
+  
+    // Calcul du pas de subdivision
+    let pas_x = largeur / (2.0f32.powf(niveau_subdivision as f32));
+    let pas_y = hauteur / (2.0f32.powf(niveau_subdivision as f32));
+  
+    // Parcours des points du plan
+    for i in 0..(2usize.pow(niveau_subdivision as u32) + 1) {
+      for j in 0..(2usize.pow(niveau_subdivision as u32) + 1) {
+        // Coordonnées du point
+        let x = i as f32 * pas_x;
+        let y = j as f32 * pas_y;
+  
+        // Couleur par défaut (ajustez selon vos besoins)
+        let couleur = [1.0, 1.0, 1.0];
+  
+        // Création du vertex et ajout au tableau
+        vertices.push(Vertex { position: [x, y, 0.0], color: couleur });
+      }
+    }
+  
+    // Parcours des triangles du plan
+    for i in 0..(2usize.pow(niveau_subdivision as u32)) {
+      for j in 0..(2usize.pow(niveau_subdivision as u32)) {
+        // Calcul des indices des vertices
+        let indice1 = i * (2usize.pow(niveau_subdivision as u32) + 1) + j;
+        let indice2 = indice1 + 1;
+        let indice3 = indice1 + (2usize.pow(niveau_subdivision as u32) + 1);
+        let indice4 = indice3 + 1;
+  
+        // Ajout des indices du premier triangle
+        indices.push(indice1 as u32);
+        indices.push(indice2 as u32);
+        indices.push(indice3 as u32);
+  
+        // Ajout des indices du second triangle
+        indices.push(indice2 as u32);
+        indices.push(indice3 as u32);
+        indices.push(indice4 as u32);
+      }
+    }
+  
+    // Retourne les vertices et les indices
+    (vertices, indices)
+  }
 struct State<'a> {
     surface: wgpu::Surface<'a>,
     device: wgpu::Device,
@@ -248,11 +295,12 @@ impl<'a> State<'a> {
             multiview: None,
         });
 
+        let (vertices, indices) = creer_plan_subdivise(1.0, 1.0, 3);
         // Create a buffer with the vertex data
         let vertex_buffer = device.create_buffer_init(
             &wgpu::util::BufferInitDescriptor{
                 label: Some("Vertex Buffer"),
-                contents: bytemuck::cast_slice(VERTICES),
+                contents: bytemuck::cast_slice(&vertices),
                 usage: wgpu::BufferUsages::VERTEX,
             }
         );
@@ -261,12 +309,12 @@ impl<'a> State<'a> {
             &wgpu::util::BufferInitDescriptor{
                 label: Some("Indice Buffer"),
                 usage: wgpu::BufferUsages::INDEX,
-                contents: bytemuck::cast_slice(INDICES),
+                contents: bytemuck::cast_slice(&indices),
             }
         );
 
         //let num_vertices = VERTICES.len() as u32;
-        let num_indices = INDICES.len() as u32;
+        let num_indices = indices.len() as u32;
         Self {
             surface,
             device,
@@ -285,7 +333,7 @@ impl<'a> State<'a> {
     }
 
     fn window(&self) -> &Window {
-        &self.window
+        self.window
     }
 
     pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
@@ -414,4 +462,4 @@ pub async fn run() {
             }
         })
         .unwrap();
-}
+}