Camera + basic controller, very WIP still

master
Wynd 2024-11-04 22:09:33 +02:00
parent 136d965f5e
commit d1cd09b52a
9 changed files with 499 additions and 533 deletions

768
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -13,13 +13,17 @@ path = "src/lib.rs"
unsafe_code = { level = "forbid" } unsafe_code = { level = "forbid" }
[dependencies] [dependencies]
ashpd = "0.9.2" ashpd = { version = "0.9.2" }
winit = "0.30.5" winit = "0.30.5"
image = "0.25.4" image = { version = "0.25.4", default-features = false, features = [
"png",
"rayon",
] }
wgpu = "23.0.0" wgpu = "23.0.0"
bytemuck = { version = "1.19.0", features = ["derive"] } bytemuck = { version = "1.19.0", features = ["derive"] }
anyhow = "1.0.92" anyhow = "1.0.92"
pollster = "0.4.0" pollster = "0.4.0"
cgmath = "0.18"
[profile.dev] [profile.dev]
codegen-backend = "cranelift" codegen-backend = "cranelift"

View File

@ -30,13 +30,15 @@ impl<'a> ApplicationHandler for App<'a> {
let path = response.uri().to_file_path().unwrap(); let path = response.uri().to_file_path().unwrap();
let file = File::open(&path).unwrap(); let file = File::open(&path).unwrap();
let buffer = BufReader::new(file); let buffer = BufReader::new(file);
let image = image::load(buffer, image::ImageFormat::Png).unwrap(); let mut image = image::load(buffer, image::ImageFormat::Png).unwrap();
let image = image.crop(0, 0, 1920, 1080);
let image = image.to_rgba8();
let window = event_loop let window = event_loop
.create_window( .create_window(
Window::default_attributes().with_fullscreen(Some(Fullscreen::Borderless(None))), Window::default_attributes().with_fullscreen(Some(Fullscreen::Borderless(None))),
) )
.unwrap(); .expect("could not create winit winodw");
self.state = Some(State::new(Arc::new(window), image)); self.state = Some(State::new(Arc::new(window), image));
@ -46,10 +48,12 @@ impl<'a> ApplicationHandler for App<'a> {
fn window_event( fn window_event(
&mut self, &mut self,
event_loop: &winit::event_loop::ActiveEventLoop, event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId, _window_id: winit::window::WindowId,
event: winit::event::WindowEvent, event: winit::event::WindowEvent,
) { ) {
let state = self.state.as_mut().unwrap(); let state = self.state.as_mut().unwrap();
state.input(&event);
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
event_loop.exit(); event_loop.exit();
@ -63,29 +67,11 @@ impl<'a> ApplicationHandler for App<'a> {
state.resize(new_size); state.resize(new_size);
} }
WindowEvent::KeyboardInput { event, .. } => { WindowEvent::KeyboardInput { event, .. } => {
state.key_input(&event);
let key = event.logical_key; let key = event.logical_key;
if key == Key::Named(NamedKey::Escape) { if key == Key::Named(NamedKey::Escape) {
event_loop.exit(); event_loop.exit();
} }
} }
WindowEvent::MouseWheel {
device_id,
delta,
phase,
} => match delta {
MouseScrollDelta::LineDelta(x, y) => {
if y > 0.0 {
state.zoom += 0.1;
state.zoom = state.zoom.clamp(0.0, 2.0);
}
else if y < 0.0 {
state.zoom -= 0.1;
state.zoom = state.zoom.clamp(0.0, 2.0);
}
}
_ => (),
},
_ => (), _ => (),
} }
} }

106
src/camera.rs 100644
View File

@ -0,0 +1,106 @@
use wgpu::util::DeviceExt;
#[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,
);
pub struct Camera {
pub eye: cgmath::Point3<f32>,
pub target: cgmath::Point3<f32>,
pub up: cgmath::Vector3<f32>,
aspect: f32,
fovy: f32,
znear: f32,
zfar: f32,
}
pub struct CameraInfo {
pub uniform: CameraUniform,
pub buffer: wgpu::Buffer,
pub layout: wgpu::BindGroupLayout,
pub bind_group: wgpu::BindGroup,
}
impl Camera {
pub fn new(config: &wgpu::SurfaceConfiguration, device: &wgpu::Device) -> (Self, CameraInfo) {
let camera = Self {
eye: (0.0, 0.0, 1.7).into(),
target: (0.0, 0.0, 0.0).into(),
up: cgmath::Vector3::unit_y(),
aspect: config.width as f32 / config.height as f32,
fovy: 45.0,
znear: 0.1,
zfar: 100.0,
};
let layout = 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"),
});
let mut uniform = CameraUniform::new();
uniform.update_view_proj(&camera);
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
let info = CameraInfo {
uniform,
buffer,
layout,
bind_group,
};
(camera, info)
}
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), 1.0, self.znear, self.zfar);
OPENGL_TO_WGPU_MATRIX * proj * view
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl CameraUniform {
fn new() -> Self {
use cgmath::SquareMatrix;
Self {
view_proj: cgmath::Matrix4::identity().into(),
}
}
pub fn update_view_proj(&mut self, camera: &Camera) {
self.view_proj = camera.build_view_projection_matrix().into();
}
}

61
src/controller.rs 100644
View File

@ -0,0 +1,61 @@
use winit::{
event::{ElementState, KeyEvent, MouseScrollDelta, WindowEvent},
keyboard::{KeyCode, PhysicalKey},
};
use crate::camera::Camera;
pub struct CameraController {
speed: f32,
cursor: (f64, f64),
forward: f32,
}
impl CameraController {
pub fn new(speed: f32) -> Self {
Self {
speed,
cursor: (0.0, 0.0),
forward: 0.0,
}
}
pub fn process_events(&mut self, event: &WindowEvent) -> bool {
match event {
WindowEvent::CursorMoved { position, .. } => {
self.cursor = (position.x, position.y);
true
}
WindowEvent::MouseWheel { delta, .. } => match delta {
MouseScrollDelta::LineDelta(_, y) => {
self.forward = *y;
true
}
_ => false,
},
_ => false,
}
}
pub fn update_camera(&mut self, camera: &mut Camera) {
use cgmath::InnerSpace;
let forward = camera.target - camera.eye;
let forward_norm = forward.normalize();
if self.forward > 0.0 {
camera.eye += forward_norm * self.speed;
}
else if self.forward < 0.0 {
camera.eye -= forward_norm * self.speed;
}
camera.eye.x = (self.cursor.0 as f32 / 1920.0) - 0.5;
camera.eye.y = (-self.cursor.1 as f32 / 1080.0) + 0.5;
camera.target.x = (self.cursor.0 as f32 / 1920.0) - 0.5;
camera.target.y = (-self.cursor.1 as f32 / 1080.0) + 0.5;
self.forward = 0.0;
}
}

View File

@ -2,6 +2,8 @@ use app::App;
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
pub mod app; pub mod app;
pub mod camera;
pub mod controller;
pub mod state; pub mod state;
pub mod texture; pub mod texture;
pub mod vertex; pub mod vertex;
@ -12,7 +14,5 @@ pub fn capture() -> anyhow::Result<()> {
let mut app = App::default(); let mut app = App::default();
event_loop.run_app(&mut app)?; Ok(event_loop.run_app(&mut app)?)
Ok(())
} }

View File

@ -1,5 +1,12 @@
// Vertex shader // Vertex shader
struct CameraUniform {
view_proj: mat4x4<f32>,
};
@group(1) @binding(0)
var<uniform> camera: CameraUniform;
struct VertexInput { struct VertexInput {
@location(0) position: vec2<f32>, @location(0) position: vec2<f32>,
@location(1) tex_coords: vec2<f32>, @location(1) tex_coords: vec2<f32>,
@ -16,7 +23,7 @@ fn vs_main(
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coords = model.tex_coords; out.tex_coords = model.tex_coords;
out.position = vec4<f32>(model.position, 0.0, 1.0); out.position = camera.view_proj * vec4<f32>(model.position, 0.0, 1.0);
return out; return out;
} }

View File

@ -4,7 +4,12 @@ use pollster::FutureExt;
use wgpu::util::DeviceExt; use wgpu::util::DeviceExt;
use winit::window::Window; use winit::window::Window;
use crate::{texture::Texture, vertex::Vertex}; use crate::{
camera::{Camera, CameraInfo},
controller::CameraController,
texture::Texture,
vertex::Vertex,
};
const VERTICES: &[Vertex] = &[ const VERTICES: &[Vertex] = &[
Vertex { Vertex {
@ -25,7 +30,11 @@ const VERTICES: &[Vertex] = &[
}, },
]; ];
const INDICES: &[u16] = &[0, 1, 2, 2, 3, 0]; #[rustfmt::skip]
const INDICES: &[u16] = &[
0, 1, 2,
2, 3, 0
];
pub struct State<'a> { pub struct State<'a> {
surface: wgpu::Surface<'a>, surface: wgpu::Surface<'a>,
@ -38,11 +47,14 @@ pub struct State<'a> {
vertex_buffer: wgpu::Buffer, vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer, index_buffer: wgpu::Buffer,
bind_group: wgpu::BindGroup, bind_group: wgpu::BindGroup,
pub camera: Camera,
pub camera_info: CameraInfo,
pub zoom: f32, pub zoom: f32,
pub camera_controller: CameraController,
} }
impl<'a> State<'a> { impl<'a> State<'a> {
pub fn new(window: Arc<Window>, image: image::DynamicImage) -> Self { pub fn new(window: Arc<Window>, image: image::RgbaImage) -> Self {
let window_size = window.inner_size(); let window_size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
@ -93,6 +105,9 @@ impl<'a> State<'a> {
desired_maximum_frame_latency: 2, desired_maximum_frame_latency: 2,
}; };
let (camera, camera_info) = Camera::new(&config, &device);
let camera_controller = CameraController::new(0.1);
let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));
let texture = Texture::new(image, &device, &queue); let texture = Texture::new(image, &device, &queue);
@ -100,7 +115,7 @@ impl<'a> State<'a> {
let render_pipeline_layout = let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"), label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture.layout], bind_group_layouts: &[&texture.layout, &camera_info.layout],
push_constant_ranges: &[], push_constant_ranges: &[],
}); });
@ -168,6 +183,9 @@ impl<'a> State<'a> {
vertex_buffer, vertex_buffer,
index_buffer, index_buffer,
bind_group: texture.group, bind_group: texture.group,
camera,
camera_info,
camera_controller,
zoom: 1.0, zoom: 1.0,
} }
} }
@ -181,11 +199,20 @@ impl<'a> State<'a> {
} }
} }
pub fn key_input(&mut self, event: &winit::event::KeyEvent) -> bool { pub fn input(&mut self, event: &winit::event::WindowEvent) -> bool {
self.camera_controller.process_events(&event);
false false
} }
pub fn update(&mut self) {} pub fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_info.uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.camera_info.buffer,
0,
bytemuck::cast_slice(&[self.camera_info.uniform]),
);
}
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> { pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?; let output = self.surface.get_current_texture()?;
@ -221,6 +248,7 @@ impl<'a> State<'a> {
render_pass.set_pipeline(&self.render_pipeline); render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.bind_group, &[]); render_pass.set_bind_group(0, &self.bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_info.bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..)); render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16); render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);

View File

@ -6,9 +6,7 @@ pub struct Texture {
} }
impl Texture { impl Texture {
pub fn new(mut image: image::DynamicImage, device: &wgpu::Device, queue: &wgpu::Queue) -> Self { pub fn new(image: image::RgbaImage, device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
let image = image.crop(0, 0, 1920, 1080);
let image_rgba = image.to_rgba8();
let image_size = image.dimensions(); let image_size = image.dimensions();
let texture_size = wgpu::Extent3d { let texture_size = wgpu::Extent3d {
@ -35,7 +33,7 @@ impl Texture {
origin: wgpu::Origin3d::ZERO, origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All, aspect: wgpu::TextureAspect::All,
}, },
&image_rgba, &image,
wgpu::ImageDataLayout { wgpu::ImageDataLayout {
offset: 0, offset: 0,
bytes_per_row: Some(4 * image_size.0), bytes_per_row: Some(4 * image_size.0),