Parcourir la source

Last commit before leaving

Ghastrod il y a 1 an
Parent
commit
e3d65ae58f
5 fichiers modifiés avec 148 ajouts et 20 suppressions
  1. 80 0
      src/geometry_utils.rs
  2. 1 0
      src/main.rs
  3. 38 11
      src/shader.wgsl
  4. 28 8
      src/state.rs
  5. 1 1
      src/vertex_buffer.rs

+ 80 - 0
src/geometry_utils.rs

@@ -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)
+}

+ 1 - 0
src/main.rs

@@ -8,6 +8,7 @@ mod camera_uniform;
 mod camera_controller2;
 mod fps;
 mod fps_counter;
+mod geometry_utils;
 
 fn main() {
     pollster::block_on(window::run());

+ 38 - 11
src/shader.wgsl

@@ -1,9 +1,36 @@
-// Fragment shader
+// // Fragment shader
 
-@fragment
-fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
-    return vec4<f32>(0.0,0.0,1.0, 1.0);
-}
+// @fragment
+// fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
+//     return vec4<f32>(0.0,0.0,1.0, 1.0);
+// }
+
+// // Vertex shader
+// struct CameraUniform {
+//     view_proj: mat4x4<f32>,
+// };
+// @group(0) @binding(0) // 1.
+// var<uniform> camera: CameraUniform;
+
+// struct VertexInput {
+//     @location(0) position: vec3<f32>,
+//     @location(1) tex_coords: vec2<f32>,
+// }
+
+// struct VertexOutput {
+//     @builtin(position) clip_position: vec4<f32>,
+//     @location(0) tex_coords: vec2<f32>,
+// }
+
+// @vertex
+// fn vs_main(
+//     model: VertexInput,
+// ) -> VertexOutput {
+//     var out: VertexOutput;
+//     out.tex_coords = model.tex_coords;
+//     out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0); // 2.
+//     return out;
+// }
 
 // Vertex shader
 struct CameraUniform {
@@ -14,20 +41,20 @@ var<uniform> camera: CameraUniform;
 
 struct VertexInput {
     @location(0) position: vec3<f32>,
-    @location(1) tex_coords: vec2<f32>,
 }
 
 struct VertexOutput {
     @builtin(position) clip_position: vec4<f32>,
-    @location(0) tex_coords: vec2<f32>,
 }
-
 @vertex
 fn vs_main(
     model: VertexInput,
-) -> VertexOutput {
+){
     var out: VertexOutput;
-    out.tex_coords = model.tex_coords;
-    out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0); // 2.
+    out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
     return out;
 }
+@fragment
+fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
+    return vec4<f32>(0.0,0.0,1.0, 1.0);
+}

+ 28 - 8
src/state.rs

@@ -3,7 +3,7 @@ use std::iter;
 use wgpu::util::DeviceExt;
 use winit::{event::WindowEvent, window::Window};
 
-use crate::{camera::{self, Camera}, camera_controller, camera_uniform, fps::Fps, vertex::Vertex, vertex_buffer::{VERT2, VERT3, VERTICES}};
+use crate::{camera::{self, Camera}, camera_controller, camera_uniform, fps::Fps, geometry_utils::{subdivide_plane2, Vertex3}, vertex::{Vertex, Vertex2}, vertex_buffer::{subdivide_plane, VERT2, VERT3, VERTICES}};
 
 
 pub struct State<'a> {
@@ -92,14 +92,34 @@ impl<'a> State<'a> {
         surface.configure(&device, &config);
         //All the above is just for the surface
 
-
-        let vert = VERT3;
-        let num_vertices = vert.len() as u32;
+        const INITIAL_VERTICES: &[Vertex3] = &[
+            Vertex3 { position: [-1.0, -1.0, 0.0] }, // Bottom-left corner
+            Vertex3 { position: [1.0, -1.0, 0.0] }, // Bottom-right corner
+            Vertex3 { position: [-1.0, 1.0, 0.0] }, // Top-left corner
+            Vertex3 { position: [1.0, 1.0, 0.0] }, // Top-right corner
+        ];
+        let (vertex_data, num_vertices) = subdivide_plane2(
+            &device,
+            INITIAL_VERTICES,
+            -1.0, // x_min
+            1.0,  // x_max
+            -1.0, // y_min
+            1.0,  // y_max
+            4,    // subdivision level (adjust as needed)
+        );
+        //let vert = VERT3;
+        //let vert = subdivide_plane(&device, vertices, x_min, x_max, y_min, y_max, level);
+        //let num_vertices = vert.len() as u32;
         //Now the vertex buffer
+        // let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
+        //     label: Some("Vertex Buffer"),
+        //     usage: wgpu::BufferUsages::VERTEX,
+        //     contents: bytemuck::cast_slice(vert)
+        // });
         let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
             label: Some("Vertex Buffer"),
-            usage: wgpu::BufferUsages::VERTEX,
-            contents: bytemuck::cast_slice(vert)
+            contents: bytemuck::cast_slice(&vertex_data),
+            usage: wgpu::BufferUsages::VERTEX
         });
 
         //Camera creation
@@ -183,7 +203,7 @@ impl<'a> State<'a> {
                 module: &shader_module,
                 entry_point: "vs_main",
                 buffers: &[
-                    Vertex::desc()
+                    Vertex3::desc()
                 ],
             },
             fragment: Some(wgpu::FragmentState {
@@ -201,7 +221,7 @@ impl<'a> State<'a> {
             primitive: wgpu::PrimitiveState {
                 topology: wgpu::PrimitiveTopology::TriangleList,
                 strip_index_format: None,
-                front_face: wgpu::FrontFace::Ccw,
+                front_face: wgpu::FrontFace::Cw,
                 cull_mode: Some(wgpu::Face::Back),
                 // Setting this to anything other than Fill requires Features::POLYGON_MODE_LINE
                 // or Features::POLYGON_MODE_POINT

+ 1 - 1
src/vertex_buffer.rs

@@ -30,7 +30,7 @@ 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) {
+pub 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;