added camera struct
This commit is contained in:
+9
-2
@@ -4,6 +4,7 @@ use futures::executor::block_on;
|
||||
|
||||
use crate::vertex::Vertex;
|
||||
use crate::texture::Texture;
|
||||
use crate::camera::Camera;
|
||||
|
||||
const VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [-1.0, 1.0, 0.0], uv: [0.0, 0.0] },
|
||||
@@ -28,7 +29,9 @@ pub struct App {
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
index_buffer: wgpu::Buffer,
|
||||
// Texture
|
||||
bind_group: wgpu::BindGroup
|
||||
bind_group: wgpu::BindGroup,
|
||||
// Camera
|
||||
camera: Camera
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -179,9 +182,13 @@ impl App {
|
||||
let vertex_buffer = Self::create_buffer(&device, wgpu::BufferUsage::VERTEX, bytemuck::cast_slice(VERTICES));
|
||||
let index_buffer = Self::create_buffer(&device, wgpu::BufferUsage::INDEX, bytemuck::cast_slice(INDICES));
|
||||
|
||||
// Cam
|
||||
let aspect = swapchain_desc.width as f32 / swapchain_desc.height as f32;
|
||||
let camera = Camera::new((0.0, 1.0, 2.0).into(), (0.0, 0.0, 0.0).into(), aspect);
|
||||
|
||||
// Save values in app
|
||||
println!("Initialized");
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer, bind_group }
|
||||
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, vertex_buffer, index_buffer, bind_group, camera }
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
use cgmath::{Point3, Vector3, Matrix4, Deg};
|
||||
|
||||
#[rustfmt::skip]
|
||||
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.0,
|
||||
0.0, 0.0, 0.5, 1.0,
|
||||
);
|
||||
|
||||
pub struct Camera {
|
||||
pos: Point3<f32>,
|
||||
target: Point3<f32>,
|
||||
up: Vector3<f32>,
|
||||
aspect: f32,
|
||||
fovy: f32,
|
||||
znear: f32,
|
||||
zfar: f32,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
|
||||
pub fn new(pos: Point3<f32>, target: Point3<f32>, aspect: f32) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
target,
|
||||
up: Vector3::unit_y(),
|
||||
aspect,
|
||||
fovy: 45.0,
|
||||
znear: 0.1,
|
||||
zfar: 100.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn projection_matrix(&self) -> Matrix4<f32> {
|
||||
let view = Matrix4::look_at_rh(self.pos, self.target, self.up);
|
||||
let proj = cgmath::perspective(Deg(self.fovy), self.aspect, self.znear, self.zfar);
|
||||
return OPENGL_TO_WGPU_MATRIX * proj * view;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use winit::{
|
||||
|
||||
mod vertex;
|
||||
mod texture;
|
||||
mod camera;
|
||||
mod app;
|
||||
use app::App;
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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 Uniforms {
|
||||
// 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 Uniforms {
|
||||
fn new() -> Self {
|
||||
use cgmath::SquareMatrix;
|
||||
Self {
|
||||
view_proj: cgmath::Matrix4::identity().into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update_view_proj(&mut self, camera: &Camera) {
|
||||
self.view_proj = camera.build_view_projection_matrix().into();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user