|
|
@@ -4,6 +4,13 @@ use wgpu::util::DeviceExt;
|
|
|
|
|
|
mod vertex;
|
|
|
use vertex::Vertex;
|
|
|
+mod simple_plane;
|
|
|
+use simple_plane::generate_simple_plane;
|
|
|
+
|
|
|
+mod camera;
|
|
|
+use camera::Camera;
|
|
|
+use camera::CameraUniform;
|
|
|
+
|
|
|
mod wave;
|
|
|
mod texture;
|
|
|
use winit::{
|
|
|
@@ -96,8 +103,13 @@ struct State<'a> {
|
|
|
num_vertices: u32,
|
|
|
//num_indices: u32,
|
|
|
|
|
|
- //diffuse_bind_group: wgpu::BindGroup,
|
|
|
- //diffuse_texture: texture::Texture,
|
|
|
+ diffuse_bind_group: wgpu::BindGroup,
|
|
|
+ diffuse_texture: texture::Texture,
|
|
|
+
|
|
|
+ camera: Camera,
|
|
|
+ camera_uniform: CameraUniform,
|
|
|
+ camera_buffer: wgpu::Buffer,
|
|
|
+ camera_bind_group: wgpu::BindGroup,
|
|
|
}
|
|
|
|
|
|
impl<'a> State<'a> {
|
|
|
@@ -163,6 +175,65 @@ impl<'a> State<'a> {
|
|
|
view_formats: vec![],
|
|
|
};
|
|
|
surface.configure(&device, &config);
|
|
|
+ //Camera
|
|
|
+
|
|
|
+ let camera = Camera{
|
|
|
+ eye: glam::Vec3::new(0.0, 1.0, 2.0),
|
|
|
+ target: glam::Vec3::new(0.0, 0.0, 0.0),
|
|
|
+ up: glam::Vec3::new(0.0, 1.0, 0.0),
|
|
|
+ aspect: config.width as f32 / config.height as f32,
|
|
|
+ fovy: 45.0,
|
|
|
+ znear: 0.1,
|
|
|
+ zfar: 100.0,
|
|
|
+ };
|
|
|
+
|
|
|
+ //Camera buffer
|
|
|
+ let mut camera_uniform = CameraUniform::new();
|
|
|
+
|
|
|
+ camera_uniform.update_view_proj(&camera);
|
|
|
+
|
|
|
+ let camera_buffer = device.create_buffer_init(
|
|
|
+ &wgpu::util::BufferInitDescriptor{
|
|
|
+ label: Some("Camera Buffer"),
|
|
|
+ contents: bytemuck::cast_slice(&[camera_uniform]),
|
|
|
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ //Camera Bind Group Layout
|
|
|
+
|
|
|
+ let camera_bind_group_layout = device.create_bind_group_layout(
|
|
|
+ &wgpu::BindGroupLayoutDescriptor{
|
|
|
+ entries: &[
|
|
|
+ wgpu::BindGroupLayoutEntry{
|
|
|
+ binding: 0,
|
|
|
+ visibility: wgpu::ShaderStages::VERTEX,
|
|
|
+ ty: wgpu::BindingType::Buffer{
|
|
|
+ min_binding_size: None,
|
|
|
+ ty: wgpu::BufferBindingType::Uniform,
|
|
|
+ has_dynamic_offset: false,
|
|
|
+ },
|
|
|
+ count: None,
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ label: Some("camera_bind_group_layout"),
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ //Camera Bind Group
|
|
|
+
|
|
|
+ let camera_bind_group = device.create_bind_group(
|
|
|
+ &wgpu::BindGroupDescriptor{
|
|
|
+ layout: &camera_bind_group_layout,
|
|
|
+ entries: &[
|
|
|
+ wgpu::BindGroupEntry{
|
|
|
+ binding: 0,
|
|
|
+ resource: camera_buffer.as_entire_binding(),
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ label: Some("camera_bind_group"),
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
//Texture
|
|
|
let diffuse_bytes = include_bytes!("happy-tree.png");
|
|
|
@@ -227,6 +298,7 @@ impl<'a> State<'a> {
|
|
|
label: Some("Render Pipeline Layout"),
|
|
|
bind_group_layouts: &[
|
|
|
&texture_bind_group_layout,
|
|
|
+ &camera_bind_group_layout,
|
|
|
],
|
|
|
push_constant_ranges: &[],
|
|
|
});
|
|
|
@@ -269,7 +341,9 @@ impl<'a> State<'a> {
|
|
|
});
|
|
|
|
|
|
//let (vertices, indices) = creer_plan_subdivise(1.0, 1.0, 10);
|
|
|
- let vert = creer_plan_simple(1.0, 1.0);
|
|
|
+ //let vert = creer_plan_simple(1.0, 1.0);
|
|
|
+
|
|
|
+ let vert = generate_simple_plane();
|
|
|
// Create a buffer with the vertex data
|
|
|
let vertex_buffer = device.create_buffer_init(
|
|
|
&wgpu::util::BufferInitDescriptor{
|
|
|
@@ -301,8 +375,13 @@ impl<'a> State<'a> {
|
|
|
//index_buffer,
|
|
|
num_vertices,
|
|
|
//num_indices,
|
|
|
- //diffuse_bind_group,
|
|
|
- //diffuse_texture,
|
|
|
+ diffuse_bind_group,
|
|
|
+ diffuse_texture,
|
|
|
+ //Camera
|
|
|
+ camera,
|
|
|
+ camera_uniform,
|
|
|
+ camera_buffer,
|
|
|
+ camera_bind_group,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -358,9 +437,12 @@ impl<'a> State<'a> {
|
|
|
occlusion_query_set: None,
|
|
|
timestamp_writes: None,
|
|
|
});
|
|
|
-
|
|
|
+ //Add the pipeline
|
|
|
render_pass.set_pipeline(&self.render_pipeline);
|
|
|
- //render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
|
|
+ //Add the bind group for the texture
|
|
|
+ render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
|
|
+ //Add the camera bind group
|
|
|
+ render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
|
|
|
|
|
|
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
|
|
//render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
|
|
@@ -368,8 +450,8 @@ impl<'a> State<'a> {
|
|
|
render_pass.draw(0..self.num_vertices, 0..1);
|
|
|
}
|
|
|
|
|
|
- //self.queue.submit(iter::once(encoder.finish()));
|
|
|
- //output.present();
|
|
|
+ self.queue.submit(iter::once(encoder.finish()));
|
|
|
+ output.present();
|
|
|
|
|
|
Ok(())
|
|
|
}
|