1
0
Fork 0

[CAMERA] Begin refactor

This commit is contained in:
Florian RICHER 2022-06-16 14:06:21 +02:00
parent 8bd4ae11e9
commit 67cd53b91e
5 changed files with 147 additions and 124 deletions

View file

@ -1,9 +1,8 @@
use crate::{meshs::DefaultMesh, render::{Renderable, TextureManager}};
use crate::{meshs::DefaultMesh, render::{Renderable, TextureManager}, input::Controllable};
use super::render::{
Vertex, Camera, CameraUniform, CameraController, Texture, InstanceRaw
Vertex, Camera, Texture, InstanceRaw
};
use wgpu::util::DeviceExt;
use winit::{
event::{KeyboardInput, VirtualKeyCode, WindowEvent, ElementState},
window::Window,
@ -17,10 +16,6 @@ pub struct State {
pub size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline,
camera: Camera,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: CameraController,
depth_texture: Texture,
mesh: DefaultMesh,
#[allow(dead_code)]
@ -88,54 +83,8 @@ impl State {
source: wgpu::ShaderSource::Wgsl(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/shaders/main.wgsl")).into()),
});
let 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 = 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 = CameraController::new(0.2);
let mut camera = Camera::new(config.width as f32, config.height as f32, 0.2);
camera.prepare(&device);
let depth_texture =
Texture::create_depth_texture(&device, &config, "depth_texture");
@ -143,7 +92,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_manager.get_texture_bind_group_layout(), &camera_bind_group_layout],
bind_group_layouts: &[&texture_manager.get_texture_bind_group_layout(), camera.get_bind_group_layout()],
push_constant_ranges: &[],
});
@ -216,10 +165,6 @@ impl State {
size,
render_pipeline,
camera,
camera_uniform,
camera_buffer,
camera_bind_group,
camera_controller,
depth_texture,
mesh,
texture_manager
@ -257,22 +202,15 @@ impl State {
self.mesh.toggle(is_pressed);
true
}
_ => self.camera_controller.process_events(event),
_ => self.camera.process_events(event),
}
}
_ => self.camera_controller.process_events(event),
_ => self.camera.process_events(event),
}
}
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]),
);
self.camera.update_instances(&self.queue);
self.mesh.update_instances(&self.queue);
}
@ -317,9 +255,7 @@ impl State {
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
self.camera.render(&mut render_pass);
self.mesh.render(&mut render_pass);
}