1
0
Fork 0

[CAMERA] Fix no updated

This commit is contained in:
Florian RICHER 2022-06-16 14:15:12 +02:00
parent 67cd53b91e
commit ca26586047

View file

@ -13,16 +13,26 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = 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<f32>,
pub target: cgmath::Point3<f32>,
pub up: cgmath::Vector3<f32>,
pub aspect: f32,
pub fovy: f32,
pub znear: f32,
pub zfar: f32,
eye: cgmath::Point3<f32>,
target: cgmath::Point3<f32>,
up: cgmath::Vector3<f32>,
aspect: f32,
fovy: f32,
znear: f32,
zfar: f32,
controller: CameraController,
uniform: Option<CameraUniform>,
uniform: CameraUniform,
bind_group: Option<wgpu::BindGroup>,
bind_group_layout: Option<wgpu::BindGroupLayout>,
buffer: Option<wgpu::Buffer>,
@ -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<f32> {
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();
}
}
}