Initial commit
This commit is contained in:
parent
9f00540b55
commit
6784b10e3c
|
@ -0,0 +1 @@
|
||||||
|
/target
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "cyborg"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bytemuck = { version="1.7", features=["derive"] }
|
||||||
|
glam = "0.20"
|
||||||
|
pollster = "0.2"
|
||||||
|
slab = "0.4"
|
||||||
|
wgpu = "0.12"
|
||||||
|
winit = "0.26"
|
|
@ -0,0 +1,159 @@
|
||||||
|
use glam::{Mat4, Quat, Vec2, Vec3};
|
||||||
|
use std::time::Instant;
|
||||||
|
use winit::event::{ElementState, VirtualKeyCode};
|
||||||
|
|
||||||
|
pub trait Camera {
|
||||||
|
fn get_vp(&self) -> [[f32; 4]; 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Flycam {
|
||||||
|
// input
|
||||||
|
is_up_pressed: bool,
|
||||||
|
is_down_pressed: bool,
|
||||||
|
is_forward_pressed: bool,
|
||||||
|
is_backward_pressed: bool,
|
||||||
|
is_left_pressed: bool,
|
||||||
|
is_right_pressed: bool,
|
||||||
|
mouse_dx: f32,
|
||||||
|
mouse_dy: f32,
|
||||||
|
// state
|
||||||
|
last_update: Instant,
|
||||||
|
pan: f32,
|
||||||
|
tilt: f32,
|
||||||
|
position: Vec3,
|
||||||
|
// constants
|
||||||
|
speed: f32,
|
||||||
|
turn_speed: f32,
|
||||||
|
aspect: f32,
|
||||||
|
fovy: f32,
|
||||||
|
znear: f32,
|
||||||
|
zfar: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flycam {
|
||||||
|
pub fn new(speed: f32, turn_speed: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
is_up_pressed: false,
|
||||||
|
is_down_pressed: false,
|
||||||
|
is_forward_pressed: false,
|
||||||
|
is_backward_pressed: false,
|
||||||
|
is_left_pressed: false,
|
||||||
|
is_right_pressed: false,
|
||||||
|
mouse_dx: 0.0,
|
||||||
|
mouse_dy: 0.0,
|
||||||
|
last_update: Instant::now(),
|
||||||
|
pan: 0.0,
|
||||||
|
tilt: 0.0,
|
||||||
|
position: Vec3::new(0.0, 0.5, 1.0),
|
||||||
|
speed,
|
||||||
|
turn_speed,
|
||||||
|
aspect: 1.0, // TODO compute from size
|
||||||
|
fovy: std::f32::consts::FRAC_PI_2,
|
||||||
|
znear: 0.1,
|
||||||
|
zfar: 100.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flycam {
|
||||||
|
pub fn process_keyboard(&mut self, key: VirtualKeyCode, state: ElementState) {
|
||||||
|
let is_pressed = state == ElementState::Pressed;
|
||||||
|
match key {
|
||||||
|
VirtualKeyCode::Space => {
|
||||||
|
self.is_up_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::LShift => {
|
||||||
|
self.is_down_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::W | VirtualKeyCode::Up => {
|
||||||
|
self.is_forward_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::A | VirtualKeyCode::Left => {
|
||||||
|
self.is_left_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::S | VirtualKeyCode::Down => {
|
||||||
|
self.is_backward_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::D | VirtualKeyCode::Right => {
|
||||||
|
self.is_right_pressed = is_pressed;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_mouse(&mut self, mouse_dx: f64, mouse_dy: f64) {
|
||||||
|
self.mouse_dx += mouse_dx as f32;
|
||||||
|
self.mouse_dy += mouse_dy as f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resize(&mut self, width: u32, height: u32) {
|
||||||
|
self.aspect = (width as f32) / (height as f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
let dt = self.last_update.elapsed();
|
||||||
|
self.last_update = Instant::now();
|
||||||
|
let dt = dt.as_micros() as f32 / 1_000_000.0;
|
||||||
|
|
||||||
|
let t = self.turn_speed;
|
||||||
|
self.pan += t * self.mouse_dx;
|
||||||
|
self.tilt += t * self.mouse_dy;
|
||||||
|
self.mouse_dx = 0.0;
|
||||||
|
self.mouse_dy = 0.0;
|
||||||
|
|
||||||
|
let tilt_limit = std::f32::consts::FRAC_PI_2;
|
||||||
|
if self.tilt < -tilt_limit {
|
||||||
|
self.tilt = -tilt_limit;
|
||||||
|
} else if self.tilt > tilt_limit {
|
||||||
|
self.tilt = tilt_limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
let s = dt * self.speed;
|
||||||
|
let axis = Self::key_axis;
|
||||||
|
let truck = s * axis(self.is_backward_pressed, self.is_forward_pressed);
|
||||||
|
let dolly = s * axis(self.is_right_pressed, self.is_left_pressed);
|
||||||
|
let boom = s * axis(self.is_down_pressed, self.is_up_pressed);
|
||||||
|
self.move_position(truck, dolly, boom);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_axis(negative: bool, positive: bool) -> f32 {
|
||||||
|
if negative {
|
||||||
|
if positive {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
-1.0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if positive {
|
||||||
|
1.0
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_position(&mut self, truck: f32, dolly: f32, boom: f32) {
|
||||||
|
// truck direction from straight down
|
||||||
|
let h = Vec2::new(self.pan.sin(), -self.pan.cos());
|
||||||
|
// truck direction from the side
|
||||||
|
let v = Vec2::new(self.tilt.cos(), -self.tilt.sin());
|
||||||
|
// composite to get forward direction
|
||||||
|
let truck_to = Vec3::new(h.x * v.x, v.y, h.y * v.x);
|
||||||
|
|
||||||
|
let dolly_to = Vec3::new(-self.pan.cos(), 0.0, -self.pan.sin());
|
||||||
|
|
||||||
|
self.position += (truck_to * truck) + (dolly_to * dolly);
|
||||||
|
self.position.y += boom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Camera for Flycam {
|
||||||
|
fn get_vp(&self) -> [[f32; 4]; 4] {
|
||||||
|
let orientation = Quat::from_euler(glam::EulerRot::XYZ, self.tilt, self.pan, 0.0);
|
||||||
|
let rotation = Mat4::from_quat(orientation);
|
||||||
|
let view = rotation * Mat4::from_translation(-self.position);
|
||||||
|
let proj = Mat4::perspective_rh_gl(self.fovy, self.aspect, self.znear, self.zfar);
|
||||||
|
let vp = proj * view;
|
||||||
|
vp.to_cols_array_2d()
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,243 @@
|
||||||
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
event::*,
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
mod camera;
|
||||||
|
|
||||||
|
use camera::*;
|
||||||
|
|
||||||
|
struct Renderer {
|
||||||
|
pub device: wgpu::Device,
|
||||||
|
pub mesh_pool: MeshPool,
|
||||||
|
pub size: winit::dpi::PhysicalSize<u32>,
|
||||||
|
surface: wgpu::Surface,
|
||||||
|
queue: wgpu::Queue,
|
||||||
|
config: wgpu::SurfaceConfiguration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Renderer {
|
||||||
|
pub async fn new(window: &winit::window::Window) -> Self {
|
||||||
|
let size = window.inner_size();
|
||||||
|
|
||||||
|
let instance = wgpu::Instance::new(wgpu::Backends::all());
|
||||||
|
let surface = unsafe { instance.create_surface(window) };
|
||||||
|
let adapter = instance
|
||||||
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
||||||
|
compatible_surface: Some(&surface),
|
||||||
|
force_fallback_adapter: false,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let (device, queue) = adapter
|
||||||
|
.request_device(
|
||||||
|
&wgpu::DeviceDescriptor {
|
||||||
|
features: wgpu::Features::empty(),
|
||||||
|
limits: wgpu::Limits::default(),
|
||||||
|
label: None,
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let config = wgpu::SurfaceConfiguration {
|
||||||
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
|
format: surface.get_preferred_format(&adapter).unwrap(),
|
||||||
|
width: size.width,
|
||||||
|
height: size.height,
|
||||||
|
present_mode: wgpu::PresentMode::Fifo,
|
||||||
|
};
|
||||||
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
|
let mesh_pool = MeshPool::default();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
size,
|
||||||
|
surface,
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
config,
|
||||||
|
mesh_pool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||||
|
if new_size.width > 0 && new_size.height > 0 {
|
||||||
|
self.size = new_size;
|
||||||
|
self.config.width = new_size.width;
|
||||||
|
self.config.height = new_size.height;
|
||||||
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(
|
||||||
|
&mut self,
|
||||||
|
camera: &impl Camera,
|
||||||
|
meshes: &MeshCommands,
|
||||||
|
) -> Result<(), wgpu::SurfaceError> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MeshGroup {
|
||||||
|
vertices: wgpu::Buffer,
|
||||||
|
vertex_capacity: usize,
|
||||||
|
indices: wgpu::Buffer,
|
||||||
|
index_capacity: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeshGroup {
|
||||||
|
pub fn new(device: &wgpu::Device, data: &MeshData) -> Self {
|
||||||
|
let vertex_capacity = data.vertices.len();
|
||||||
|
let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Vertex Buffer"),
|
||||||
|
contents: bytemuck::cast_slice(&data.vertices),
|
||||||
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
|
});
|
||||||
|
|
||||||
|
let index_capacity = data.indices.len();
|
||||||
|
let indices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Index Buffer"),
|
||||||
|
contents: bytemuck::cast_slice(&data.indices),
|
||||||
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
|
});
|
||||||
|
|
||||||
|
Self {
|
||||||
|
vertex_capacity,
|
||||||
|
vertices,
|
||||||
|
index_capacity,
|
||||||
|
indices,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct MeshPool {
|
||||||
|
groups: slab::Slab<MeshGroup>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeshPool {
|
||||||
|
pub fn allocate(&mut self, device: &wgpu::Device, data: &MeshData) -> MeshHandle {
|
||||||
|
let group = MeshGroup::new(device, data);
|
||||||
|
let group_id = self.groups.insert(group);
|
||||||
|
let sub_id = 0;
|
||||||
|
MeshHandle { group_id, sub_id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
struct CameraUniform {
|
||||||
|
vp: [[f32; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
struct Vertex {
|
||||||
|
position: [f32; 3],
|
||||||
|
}
|
||||||
|
|
||||||
|
type Index = u32;
|
||||||
|
|
||||||
|
struct MeshData {
|
||||||
|
vertices: Vec<Vertex>,
|
||||||
|
indices: Vec<Index>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
|
||||||
|
struct MeshHandle {
|
||||||
|
group_id: usize,
|
||||||
|
// unused for now, since each group contains only one mesh
|
||||||
|
sub_id: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
struct MeshInstance {
|
||||||
|
pub handle: MeshHandle,
|
||||||
|
pub transform: glam::Mat4,
|
||||||
|
}
|
||||||
|
|
||||||
|
type MeshCommands = Vec<MeshInstance>;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
let mut camera = Flycam::new(10.0, 0.002);
|
||||||
|
let mut is_grabbed = false;
|
||||||
|
let mut ren = pollster::block_on(Renderer::new(&window));
|
||||||
|
let commands = MeshCommands::new();
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| match event {
|
||||||
|
Event::RedrawRequested(_) => match ren.render(&camera, &commands) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(wgpu::SurfaceError::Lost) => ren.resize(ren.size),
|
||||||
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||||
|
Err(e) => println!("error: {:?}", e),
|
||||||
|
},
|
||||||
|
Event::MainEventsCleared => {
|
||||||
|
camera.update();
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
Event::DeviceEvent { ref event, .. } => match event {
|
||||||
|
DeviceEvent::MouseMotion { delta } => {
|
||||||
|
if is_grabbed {
|
||||||
|
camera.process_mouse(delta.0, delta.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Event::WindowEvent {
|
||||||
|
ref event,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
KeyboardInput {
|
||||||
|
virtual_keycode: Some(key),
|
||||||
|
state,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if *state == ElementState::Pressed && *key == VirtualKeyCode::Escape {
|
||||||
|
if is_grabbed {
|
||||||
|
window.set_cursor_grab(false).unwrap();
|
||||||
|
window.set_cursor_visible(true);
|
||||||
|
is_grabbed = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
camera.process_keyboard(*key, *state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
button: MouseButton::Left,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if !is_grabbed {
|
||||||
|
window.set_cursor_grab(true).unwrap();
|
||||||
|
window.set_cursor_visible(false);
|
||||||
|
is_grabbed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||||
|
WindowEvent::Resized(physical_size) => {
|
||||||
|
ren.resize(*physical_size);
|
||||||
|
camera.resize(physical_size.width, physical_size.height);
|
||||||
|
}
|
||||||
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
|
ren.resize(**new_inner_size);
|
||||||
|
camera.resize(new_inner_size.width, new_inner_size.height);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue