1
0
Fork 0

[CAMERA] Add from tutorial

This commit is contained in:
Florian RICHER 2022-06-12 21:47:11 +02:00
parent 9a4d651c44
commit 9165d8e93f
4 changed files with 221 additions and 7 deletions

View file

@ -35,6 +35,11 @@ pub struct State {
pub size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer,
camera: super::camera::Camera,
camera_uniform: super::camera::CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: super::camera::CameraController,
// num_vertices: u32,
index_buffer: wgpu::Buffer,
num_indices: u32,
@ -160,10 +165,64 @@ impl State {
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
});
let camera = super::camera::Camera {
// position the camera one unit up and 2 units back
// +z is out of the screen
eye: (0.0, 1.0, 2.0).into(),
// have it look at the origin
target: (0.0, 0.0, 0.0).into(),
// which way is "up"
up: cgmath::Vector3::unit_y(),
aspect: config.width as f32 / config.height as f32,
fovy: 45.0,
znear: 0.1,
zfar: 100.0,
};
let mut camera_uniform = super::camera::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,
}
);
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 {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
],
label: Some("camera_bind_group_layout"),
});
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"),
});
let camera_controller = super::camera::CameraController::new(0.2);
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -203,7 +262,7 @@ impl State {
alpha_to_coverage_enabled: false,
},
multiview: None,
});
});
Self {
surface,
@ -213,6 +272,11 @@ impl State {
size,
render_pipeline,
vertex_buffer,
camera,
camera_uniform,
camera_buffer,
camera_bind_group,
camera_controller,
// num_vertices,
index_buffer,
num_indices,
@ -230,12 +294,15 @@ impl State {
}
}
pub fn input(&mut self, _event: &WindowEvent) -> bool {
// log::info!("{:#?}", event);
false
pub fn input(&mut self, event: &WindowEvent) -> bool {
self.camera_controller.process_events(event)
}
pub fn update(&mut self) {}
pub fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[self.camera_uniform]));
}
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?;
@ -273,6 +340,7 @@ impl State {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_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);
// render_pass.draw(0..self.num_vertices, 0..1);