feat(rendering): rendering textures with camera
Co-authored-by: BitSyndicate <contact@bitsyndicate.de>
This commit is contained in:
parent
dfb3b04c9d
commit
23d5023fb4
9 changed files with 1037 additions and 329 deletions
53
src/camera.rs
Normal file
53
src/camera.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
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,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
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);
|
||||
|
||||
OPENGL_TO_WGPU_MATRIX * proj * view
|
||||
}
|
||||
|
||||
pub fn update_aspect(&mut self, aspect: f32) {
|
||||
self.aspect = aspect;
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.5, 0.5,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
);
|
||||
|
||||
unsafe impl bytemuck::Pod for CameraUniform {}
|
||||
unsafe impl bytemuck::Zeroable for CameraUniform {}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct CameraUniform {
|
||||
view_proj: cgmath::Matrix4<f32>,
|
||||
}
|
||||
impl Default for CameraUniform {
|
||||
fn default() -> CameraUniform {
|
||||
use cgmath::SquareMatrix;
|
||||
Self {
|
||||
view_proj: cgmath::Matrix4::identity(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl CameraUniform {
|
||||
pub fn update_view_proj(&mut self, camera: &Camera) {
|
||||
self.view_proj = camera.build_view_projection_matrix();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue