[CAMERA] Begin refactor
This commit is contained in:
parent
8bd4ae11e9
commit
67cd53b91e
5 changed files with 147 additions and 124 deletions
|
@ -1,4 +1,9 @@
|
|||
use winit::event::{WindowEvent, KeyboardInput, ElementState, VirtualKeyCode};
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent};
|
||||
|
||||
use crate::input::Controllable;
|
||||
|
||||
use super::Renderable;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
||||
|
@ -16,41 +21,129 @@ pub struct Camera {
|
|||
pub fovy: f32,
|
||||
pub znear: f32,
|
||||
pub zfar: f32,
|
||||
controller: CameraController,
|
||||
uniform: Option<CameraUniform>,
|
||||
bind_group: Option<wgpu::BindGroup>,
|
||||
bind_group_layout: Option<wgpu::BindGroupLayout>,
|
||||
buffer: Option<wgpu::Buffer>,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
pub fn new(width: f32, height: f32, speed: f32) -> Self {
|
||||
Self {
|
||||
eye: (0.0, 1.0, 2.0).into(),
|
||||
target: (0.0, 0.0, 0.0).into(),
|
||||
up: cgmath::Vector3::unit_y(),
|
||||
aspect: width / height,
|
||||
fovy: 45.0,
|
||||
znear: 0.1,
|
||||
zfar: 100.0,
|
||||
controller: CameraController::new(speed),
|
||||
bind_group: None,
|
||||
bind_group_layout: None,
|
||||
uniform: None,
|
||||
buffer: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_view_projection_matrix(&self) -> cgmath::Matrix4<f32> {
|
||||
let view = cgmath::Matrix4::look_at_rh(self.eye, self.target, self.up);
|
||||
let proj = cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar);
|
||||
|
||||
return OPENGL_TO_WGPU_MATRIX * proj * view;
|
||||
}
|
||||
}
|
||||
|
||||
// We need this for Rust to store our data correctly for the shaders
|
||||
#[repr(C)]
|
||||
// This is so we can store this in a buffer
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct CameraUniform {
|
||||
// We can't use cgmath with bytemuck directly so we'll have
|
||||
// to convert the Matrix4 into a 4x4 f32 array
|
||||
view_proj: [[f32; 4]; 4],
|
||||
}
|
||||
pub fn get_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
|
||||
&self.bind_group_layout.as_ref().unwrap()
|
||||
}
|
||||
|
||||
impl CameraUniform {
|
||||
pub fn new() -> Self {
|
||||
use cgmath::SquareMatrix;
|
||||
Self {
|
||||
view_proj: cgmath::Matrix4::identity().into(),
|
||||
pub fn update_camera(&mut self) {
|
||||
use cgmath::InnerSpace;
|
||||
let forward = self.target - self.eye;
|
||||
let forward_norm = forward.normalize();
|
||||
let forward_mag = forward.magnitude();
|
||||
|
||||
if self.controller.is_forward_pressed && forward_mag > self.controller.speed {
|
||||
self.eye += forward_norm * self.controller.speed;
|
||||
}
|
||||
if self.controller.is_backward_pressed {
|
||||
self.eye -= forward_norm * self.controller.speed;
|
||||
}
|
||||
|
||||
let right = forward_norm.cross(self.up);
|
||||
|
||||
let forward = self.target - self.eye;
|
||||
let forward_mag = forward.magnitude();
|
||||
|
||||
if self.controller.is_right_pressed {
|
||||
self.eye = self.target - (forward + right * self.controller.speed).normalize() * forward_mag;
|
||||
}
|
||||
if self.controller.is_left_pressed {
|
||||
self.eye = self.target - (forward - right * self.controller.speed).normalize() * forward_mag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_view_proj(&mut self, camera: &Camera) {
|
||||
self.view_proj = camera.build_view_projection_matrix().into();
|
||||
impl Renderable for Camera {
|
||||
fn prepare(&mut self, device: &wgpu::Device) {
|
||||
self.uniform = Some(CameraUniform::from(self));
|
||||
|
||||
self.buffer = Some(
|
||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Camera Buffer"),
|
||||
contents: bytemuck::cast_slice(&[self.uniform.unwrap()]),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
}),
|
||||
);
|
||||
|
||||
self.bind_group_layout = Some(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"),
|
||||
},
|
||||
));
|
||||
|
||||
self.bind_group = Some(device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout.as_ref().unwrap(),
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: self.buffer.as_ref().unwrap().as_entire_binding(),
|
||||
}],
|
||||
label: Some("camera_bind_group"),
|
||||
}));
|
||||
}
|
||||
|
||||
fn update_instances(&mut self, queue: &wgpu::Queue) {
|
||||
self.update_camera();
|
||||
self.uniform.unwrap().update_view_proj(self);
|
||||
queue.write_buffer(
|
||||
&self.buffer.as_ref().unwrap(),
|
||||
0,
|
||||
bytemuck::cast_slice(&[self.uniform.unwrap()]),
|
||||
);
|
||||
}
|
||||
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_bind_group(1, &self.bind_group.as_ref().unwrap(), &[]);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CameraController {
|
||||
impl Controllable for Camera {
|
||||
fn process_events(&mut self, event: &WindowEvent) -> bool {
|
||||
self.controller.process_events(event)
|
||||
}
|
||||
}
|
||||
|
||||
struct CameraController {
|
||||
speed: f32,
|
||||
is_forward_pressed: bool,
|
||||
is_backward_pressed: bool,
|
||||
|
@ -104,36 +197,26 @@ impl CameraController {
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_camera(&self, camera: &mut Camera) {
|
||||
use cgmath::InnerSpace;
|
||||
let forward = camera.target - camera.eye;
|
||||
let forward_norm = forward.normalize();
|
||||
let forward_mag = forward.magnitude();
|
||||
// We need this for Rust to store our data correctly for the shaders
|
||||
#[repr(C)]
|
||||
// This is so we can store this in a buffer
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct CameraUniform {
|
||||
// We can't use cgmath with bytemuck directly so we'll have
|
||||
// to convert the Matrix4 into a 4x4 f32 array
|
||||
view_proj: [[f32; 4]; 4],
|
||||
}
|
||||
|
||||
// Prevents glitching when camera gets too close to the
|
||||
// center of the scene.
|
||||
if self.is_forward_pressed && forward_mag > self.speed {
|
||||
camera.eye += forward_norm * self.speed;
|
||||
}
|
||||
if self.is_backward_pressed {
|
||||
camera.eye -= forward_norm * self.speed;
|
||||
}
|
||||
|
||||
let right = forward_norm.cross(camera.up);
|
||||
|
||||
// Redo radius calc in case the fowrard/backward is pressed.
|
||||
let forward = camera.target - camera.eye;
|
||||
let forward_mag = forward.magnitude();
|
||||
|
||||
if self.is_right_pressed {
|
||||
// Rescale the distance between the target and eye so
|
||||
// that it doesn't change. The eye therefore still
|
||||
// lies on the circle made by the target and eye.
|
||||
camera.eye = camera.target - (forward + right * self.speed).normalize() * forward_mag;
|
||||
}
|
||||
if self.is_left_pressed {
|
||||
camera.eye = camera.target - (forward - right * self.speed).normalize() * forward_mag;
|
||||
impl CameraUniform {
|
||||
pub fn from(camera: &Camera) -> Self {
|
||||
Self {
|
||||
view_proj: camera.build_view_projection_matrix().into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_view_proj(&mut self, camera: &Camera) {
|
||||
self.view_proj = camera.build_view_projection_matrix().into();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,7 @@ mod vertex;
|
|||
pub use vertex::Vertex;
|
||||
|
||||
mod camera;
|
||||
pub use camera::{
|
||||
Camera, CameraUniform, CameraController
|
||||
};
|
||||
pub use camera::Camera;
|
||||
|
||||
mod texture;
|
||||
pub use texture::{Texture, TextureManager};
|
||||
|
@ -20,6 +18,6 @@ pub use mesh::Mesh;
|
|||
|
||||
pub trait Renderable {
|
||||
fn prepare(&mut self, device: &Device);
|
||||
fn update_instances(&mut self, device: &Queue);
|
||||
fn update_instances(&mut self, queue: &Queue);
|
||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue