Clemsim 1 年間 前
コミット
1f7889ca43
5 ファイル変更104 行追加51 行削除
  1. 47 47
      src/lib.rs
  2. 1 0
      src/main.rs
  3. 2 1
      src/shader.wgsl
  4. 6 0
      src/vertex.rs
  5. 48 3
      src/wave.rs

+ 47 - 47
src/lib.rs

@@ -1,6 +1,10 @@
 use std::iter;
 
 use wgpu::util::DeviceExt;
+
+mod vertex;
+use vertex::Vertex;
+mod wave;
 mod texture;
 use winit::{
     event::*,
@@ -9,12 +13,7 @@ use winit::{
     window::{Window, WindowBuilder},
 };
 
-#[repr(C)]
-#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
-struct Vertex {
-    position: [f32; 3],
-    color: [f32; 3],
-}
+
 
 
 impl Vertex {
@@ -57,55 +56,55 @@ impl Vertex {
 //     2, 3, 4,
 // ];
 
-fn creer_plan_subdivise(largeur: f32, hauteur: f32, niveau_subdivision: usize) -> (Vec<Vertex>, Vec<u32>) {
+// 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();
+//     // 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));
+//     // 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;
+//     // 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];
+//         // 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 });
-      }
-    }
+//         // 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;
+//     // 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 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);
-      }
-    }
+//         // 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)
-  }
+//     // Retourne les vertices et les indices
+//     (vertices, indices)
+//   }
 struct State<'a> {
     surface: wgpu::Surface<'a>,
     device: wgpu::Device,
@@ -295,7 +294,8 @@ impl<'a> State<'a> {
             multiview: None,
         });
 
-        let (vertices, indices) = creer_plan_subdivise(1.0, 1.0, 10);
+        //let (vertices, indices) = creer_plan_subdivise(1.0, 1.0, 10);
+        let (vertices,indices) = wave::generate_mesh(100,100);
         // Create a buffer with the vertex data
         let vertex_buffer = device.create_buffer_init(
             &wgpu::util::BufferInitDescriptor{

+ 1 - 0
src/main.rs

@@ -1,6 +1,7 @@
 use wgpu1::run;
 
 mod wave;
+mod vertex;
 fn main() {
     pollster::block_on(run());
 }

+ 2 - 1
src/shader.wgsl

@@ -30,5 +30,6 @@ var s_diffuse: sampler;
 
 @fragment
 fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
-    return textureSample(t_diffuse, s_diffuse, in.tex_coords);
+    //return textureSample(t_diffuse, s_diffuse, in.tex_coords);
+    return vec4<f32>(1.0, 1.0, 0.0, 1.0);
 }

+ 6 - 0
src/vertex.rs

@@ -0,0 +1,6 @@
+#[repr(C)]
+#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
+pub struct Vertex {
+    pub position: [f32; 3],
+    pub color: [f32; 3],
+}

+ 48 - 3
src/wave.rs

@@ -1,4 +1,6 @@
-use std::f64::consts::PI;
+use std::{arch::x86_64, f64::consts::PI};
+
+use crate::vertex::Vertex;
 
 const ALPHA : f64 = 8.1e-3;
 
@@ -25,16 +27,59 @@ const LAMBDA: f64 = 0.1;
 const K : f64 = 2.0 * PI / LAMBDA;
 
 
-fn X_wave(a:f64,b:f64,t:f64)->f64{
+fn x_wave(a:f64,b:f64,t:f64)->f64{
     let C : f64 = (G / K).sqrt();
     let result = a + (((K*b).exp()) / K) * (K*(a+C*t)).sin();
 
     return result
 }
 
-fn Y_wave(a:f64,b:f64,t:f64)->f64{
+fn y_wave(a:f64,b:f64,t:f64)->f64{
     let C : f64 = (G / K).sqrt();
     let result = b - (((K*b).exp()) / K) * (K*(a+C*t)).cos();
 
     return result
+}
+
+pub fn generate_mesh(num_vertices_x:u32, numvertices_y:u32)->(Vec<Vertex>,Vec<u32>){
+    let mut vertices = Vec::new();
+    for i in 0..num_vertices_x{
+        for j in 0..numvertices_y{
+            let tx = i as f32 / (num_vertices_x-1) as f32;
+            let ty = j as f32 / (numvertices_y-1) as f32;
+
+            vertices.push(
+                Vertex{
+                    position: [-0.5 * tx,-0.5 * ty, 0.0],
+                    color: [1., 1., 1.],
+                }
+            )
+        }
+    }
+
+    let mut indices: Vec<u32> = Vec::new();
+
+    let mut y = 0;
+    let mut x = 0;
+    
+    while y < (numvertices_y - 1) {
+      while x < (num_vertices_x - 1) {
+
+        let quad = y * num_vertices_x + x;
+
+        indices.push(quad);
+        indices.push(quad + num_vertices_x);
+        indices.push(quad + num_vertices_x + 1);
+
+        indices.push(quad);
+        indices.push(quad + num_vertices_x + 1);
+        indices.push(quad + 1);
+
+        x += 1;
+      }
+      y += 1;
+    }
+    
+    return (vertices,indices);
+
 }