Explorar o código

Still running, finishing integration of heightmap

Ghastrod hai 1 ano
pai
achega
a36e6764d2
Modificáronse 5 ficheiros con 74 adicións e 18 borrados
  1. BIN=BIN
      src/images/heightmap.jpg
  2. 68 15
      src/lib.rs
  3. 1 0
      src/shaders/plane_shader.wgsl
  4. 3 2
      src/wireframe/wireframe_pipeline.rs
  5. 2 1
      truc

BIN=BIN
src/images/heightmap.jpg


+ 68 - 15
src/lib.rs

@@ -12,6 +12,7 @@ mod camera;
 mod object;
 mod sky;
 mod wireframe;
+mod texture;
 
 #[cfg(target_arch = "wasm32")]
 use wasm_bindgen::prelude::*;
@@ -37,14 +38,16 @@ struct State<'a> {
     // unsafe references to the window's resources.
     window: &'a Window,
     num_vertices: u32,
+    //FPS Stuff, could be transformed to its own module
     last_frame: Instant,
     framecount: u64,
-
     frametime: f64,
     old_frame_times: Vec<f64>,
     max_history_size: usize,
-    //sky_pipeline: wgpu::RenderPipeline,
     last_fps: Instant,
+    //Heightmap Stuff
+    texture_bind_group: wgpu::BindGroup,
+    diffuse_texture: texture::texture_struct::Texture,
 }
 
 impl<'a> State<'a> {
@@ -131,10 +134,63 @@ impl<'a> State<'a> {
         let (camera, camera_uniform, camera_buffer,camera_bind_group, camera_bind_group_layout) = camera_buffer::new(&device, &config);
         let camera_controller = camera_controller::CameraController::new(0.1);
 
+
+        let last_frame: Instant = Instant::now();
+        let delta: f64 = 0.2;
+
+        //let sky_pipeline = sky::sky_pipeline::new_sky_pipeline(&device, &config);
+        let framecount: u64 = 0;
+        let old_frame_times : Vec<f64> = vec![];
+        let max_history: usize = 60;
+        let last_fps = Instant::now();
+
+
+
+        //Heightmap texture
+        let diffuse_bytes = include_bytes!("./images/heightmap.jpg");
+        let diffuse_texture = texture::texture_struct::Texture::from_bytes(&device, &queue, diffuse_bytes, "Heightmap texture").unwrap();
+        let diffuse_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor{
+            label: Some("Heightmap bind group layout"),
+            entries:&[
+                wgpu::BindGroupLayoutEntry{
+                    binding: 0,
+                    visibility: wgpu::ShaderStages::VERTEX,
+                    ty: wgpu::BindingType::Texture{
+                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
+                        view_dimension: wgpu::TextureViewDimension::D2,
+                        multisampled: false
+                    },
+                    count: None
+                },
+                wgpu::BindGroupLayoutEntry{
+                    binding: 1,
+                    visibility: wgpu::ShaderStages::VERTEX,
+                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+                    count: None
+                }
+            ]
+        });
+        let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor{
+            label: Some("Heightmap bind group"),
+            layout: &diffuse_bind_group_layout,
+            entries: &[
+                wgpu::BindGroupEntry{
+                    binding: 0,
+                    resource: wgpu::BindingResource::TextureView(&diffuse_texture.view)
+                },
+                wgpu::BindGroupEntry{
+                    binding: 1,
+                    resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler)
+                }
+            ]
+        });
+
+
         let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor{
             label: Some("Pipeline Layout"),
             bind_group_layouts: &[
                 &camera_bind_group_layout,
+                &diffuse_bind_group_layout,
             ],
             push_constant_ranges: &[]
         });
@@ -176,16 +232,7 @@ impl<'a> State<'a> {
             multiview: None
         });
 
-        let last_frame: Instant = Instant::now();
-        let delta: f64 = 0.2;
-
-        //let sky_pipeline = sky::sky_pipeline::new_sky_pipeline(&device, &config);
-        let framecount: u64 = 0;
-        let old_frame_times : Vec<f64> = vec![];
-        let max_history: usize = 60;
-        let last_fps = Instant::now();
-
-        let wireframe_pipeline = wireframe::wireframe_pipeline::new(&device, &config, &camera_bind_group_layout);
+        let wireframe_pipeline = wireframe::wireframe_pipeline::new(&device, &config, &camera_bind_group_layout, &diffuse_bind_group_layout);
         let wireframe = false;
         Self {
             wireframe,
@@ -204,13 +251,18 @@ impl<'a> State<'a> {
             camera_uniform,
             camera_controller,
             num_vertices: num_vertices,
-            last_frame: last_frame,
-            frametime: delta,
+
             //sky_pipeline: sky_pipeline
+            //FPS Struct, could be transformed to its own module
             framecount,
             old_frame_times: old_frame_times,
             max_history_size: max_history,
-            last_fps: last_fps
+            last_fps: last_fps,
+            last_frame: last_frame,
+            frametime: delta,
+            //Heightmap Stuff
+            diffuse_texture: diffuse_texture,
+            texture_bind_group: diffuse_bind_group,
         }
     }
 
@@ -295,6 +347,7 @@ impl<'a> State<'a> {
 
             render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
             render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
+            render_pass.set_bind_group(1, &self.texture_bind_group, &[]);
             render_pass.draw(0..self.num_vertices, 0..1);
             //render_pass.draws
         }

+ 1 - 0
src/shaders/plane_shader.wgsl

@@ -10,6 +10,7 @@ var<uniform> camera: CameraUniform;
 struct VertexInput {
     @location(0) position: vec3<f32>,
     @location(1) color: vec3<f32>,
+    @location(2) uv: vec2<f32>,
 };
 
 struct VertexOutput {

+ 3 - 2
src/wireframe/wireframe_pipeline.rs

@@ -2,12 +2,13 @@ use wgpu::{include_wgsl, BlendState, ColorTargetState, RenderPipeline, RenderPip
 
 use crate::object;
 
-pub fn new(device: &wgpu::Device, config: &SurfaceConfiguration,camera_bind_group_layout: &wgpu::BindGroupLayout)-> RenderPipeline{
+pub fn new(device: &wgpu::Device, config: &SurfaceConfiguration,camera_bind_group_layout: &wgpu::BindGroupLayout, diffuse_bind_group_layout: &wgpu::BindGroupLayout)-> RenderPipeline{
 
     let wireframe_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor{
         label: Some("Wireframe Pipeline Layout"),
         bind_group_layouts: &[
-            &camera_bind_group_layout
+            &camera_bind_group_layout,
+            &diffuse_bind_group_layout,
         ],
         push_constant_ranges: &[]
     });

+ 2 - 1
truc

@@ -10,6 +10,7 @@ var<uniform> camera: CameraUniform;
 struct VertexInput {
     @location(0) position: vec3<f32>,
     @location(1) color: vec3<f32>,
+    @location(2) uv: vec2<f32>,
 };
 
 struct VertexOutput {
@@ -27,7 +28,7 @@ var s_diffuse: sampler;
 fn vs_main(
     model: VertexInput,
 ) -> VertexOutput {
-    let height_added = texturesample(t_diffuse,s_diffuse,in.tex_coords);
+    let height_added = texturesample(t_diffuse,s_diffuse,model.uv);
     var out: VertexOutput;
     out.color = model.color;
     out.clip_position = camera.view_proj * vec4<f32>(model.position.x,model.position.y, model.position.z + height_added, 1.0);