From ca26586047932512f7cb38b4e47588449cef2868 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 16 Jun 2022 14:15:12 +0200 Subject: [PATCH] [CAMERA] Fix no updated --- src/render/camera.rs | 66 ++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/src/render/camera.rs b/src/render/camera.rs index d25213b..1f7cfd6 100644 --- a/src/render/camera.rs +++ b/src/render/camera.rs @@ -13,16 +13,26 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( 0.0, 0.0, 0.5, 1.0, ); +// 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, Default)] +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 struct Camera { - pub eye: cgmath::Point3, - pub target: cgmath::Point3, - pub up: cgmath::Vector3, - pub aspect: f32, - pub fovy: f32, - pub znear: f32, - pub zfar: f32, + eye: cgmath::Point3, + target: cgmath::Point3, + up: cgmath::Vector3, + aspect: f32, + fovy: f32, + znear: f32, + zfar: f32, controller: CameraController, - uniform: Option, + uniform: CameraUniform, bind_group: Option, bind_group_layout: Option, buffer: Option, @@ -41,23 +51,23 @@ impl Camera { controller: CameraController::new(speed), bind_group: None, bind_group_layout: None, - uniform: None, + uniform: CameraUniform::default(), buffer: None, } } - fn build_view_projection_matrix(&self) -> cgmath::Matrix4 { + fn update_uniform(&mut self) { 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; + self.uniform.view_proj = (OPENGL_TO_WGPU_MATRIX * proj * view).into(); } pub fn get_bind_group_layout(&self) -> &wgpu::BindGroupLayout { &self.bind_group_layout.as_ref().unwrap() } - pub fn update_camera(&mut self) { + fn update_camera(&mut self) { use cgmath::InnerSpace; let forward = self.target - self.eye; let forward_norm = forward.normalize(); @@ -86,12 +96,12 @@ impl Camera { impl Renderable for Camera { fn prepare(&mut self, device: &wgpu::Device) { - self.uniform = Some(CameraUniform::from(self)); + self.update_uniform(); self.buffer = Some( device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Camera Buffer"), - contents: bytemuck::cast_slice(&[self.uniform.unwrap()]), + contents: bytemuck::cast_slice(&[self.uniform]), usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, }), ); @@ -124,11 +134,11 @@ impl Renderable for Camera { fn update_instances(&mut self, queue: &wgpu::Queue) { self.update_camera(); - self.uniform.unwrap().update_view_proj(self); + self.update_uniform(); queue.write_buffer( &self.buffer.as_ref().unwrap(), 0, - bytemuck::cast_slice(&[self.uniform.unwrap()]), + bytemuck::cast_slice(&[self.uniform]), ); } @@ -197,26 +207,4 @@ impl CameraController { _ => false, } } -} - -// 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], -} - -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(); - } -} +} \ No newline at end of file