1
0
Fork 0

Rotating cubes + deltatime

This commit is contained in:
Adrian Hedqvist 2020-11-25 20:14:08 +01:00
parent 51e8263503
commit ab9d965630
2 changed files with 27 additions and 12 deletions

View file

@ -87,7 +87,7 @@ impl CameraController {
}
}
pub fn update_camera(&self, camera: &mut Camera) {
pub fn update_camera(&self, camera: &mut Camera, dt: f32) {
let mut forward = camera.target - camera.eye;
let mut forward_norm = forward.normalize();
let mut forward_mag = forward.magnitude();
@ -95,11 +95,12 @@ impl CameraController {
if self.is_forward_pressed != self.is_backward_pressed {
// Prevents glitching when camera gets too close to the
// center of the scene.
if self.is_forward_pressed && forward_mag > self.speed {
camera.eye += forward_norm * self.speed;
let f_spd = (self.speed * dt).min(forward_mag - 8.0);
if self.is_forward_pressed && forward_mag > f_spd {
camera.eye += forward_norm * f_spd;
}
else if self.is_backward_pressed {
camera.eye -= forward_norm * self.speed;
camera.eye -= forward_norm * self.speed * dt;
}
forward = camera.target - camera.eye;
forward_norm = forward.normalize();
@ -110,10 +111,10 @@ impl CameraController {
if self.is_right_pressed != self.is_left_pressed {
if self.is_right_pressed {
camera.eye = camera.target - (forward - right * self.speed).normalize() * forward_mag;
camera.eye = camera.target - (forward - right * self.speed * dt).normalize() * forward_mag;
}
else if self.is_left_pressed {
camera.eye = camera.target - (forward + right * self.speed).normalize() * forward_mag;
camera.eye = camera.target - (forward + right * self.speed * dt).normalize() * forward_mag;
}
}
}

View file

@ -33,6 +33,8 @@ fn main() {
.expect("Failed to create window");
let mut state = futures::executor::block_on(State::new(&window));
let mut previous_frame = std::time::Instant::now();
let mut dt = std::time::Duration::new(0, 0);
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
@ -74,7 +76,10 @@ fn main() {
};
}
Event::MainEventsCleared => {
state.update();
state.update(dt.as_secs_f32());
let now = std::time::Instant::now();
dt = now - previous_frame;
previous_frame = now;
window.request_redraw();
}
_ => {}
@ -303,7 +308,7 @@ impl State {
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("instance_buffer"),
contents: bytemuck::cast_slice(&instance_data),
usage: wgpu::BufferUsage::VERTEX,
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
});
let camera = camera::Camera {
@ -316,7 +321,7 @@ impl State {
zfar: 100.0,
};
let camera_controller = camera::CameraController::new(0.2);
let camera_controller = camera::CameraController::new(20.0);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
@ -465,8 +470,8 @@ impl State {
self.camera_controller.process_events(event)
}
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
fn update(&mut self, dt: f32) {
self.camera_controller.update_camera(&mut self.camera, dt);
self.uniforms.update_view_proj(&self.camera);
let mut encoder = self
@ -493,12 +498,21 @@ impl State {
let old_pos: cgmath::Vector3<f32> = self.light.position.into();
self.light.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(90.0 * dt))
* old_pos)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
for instance in &mut self.instances {
let deg = cgmath::Deg(30.0 * dt);
let rot = cgmath::Quaternion::from(cgmath::Euler::new(deg, deg, deg));
instance.rotation = instance.rotation * rot;
}
let instance_data: Vec<_> = self.instances.iter().map(Instance::to_raw).collect();
self.queue.write_buffer(&self.instance_buffer, 0, bytemuck::cast_slice(&instance_data));
self.queue.submit(Some(encoder.finish()));
}