Compare commits

..

No commits in common. "e89c1e4c3da1ceae3556cd99836d6d477c3c1383" and "b870e03812649ffcf1fd9249dbeacbf81cb022c5" have entirely different histories.

3 changed files with 21 additions and 73 deletions

View File

@ -12,8 +12,6 @@ pub struct DemRenderer {
vertex_buffer: wgpu::Buffer, vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer, index_buffer: wgpu::Buffer,
index_count: usize, index_count: usize,
uniforms: Uniforms,
animation_start: std::time::Instant,
} }
#[repr(C)] #[repr(C)]
@ -22,51 +20,6 @@ struct Vertex {
position: [f32; 4], position: [f32; 4],
} }
struct Uniforms {
projection_matrix: glam::Mat4,
view_matrix: glam::Mat4,
buffer: wgpu::Buffer,
}
impl Uniforms {
fn new(device: &wgpu::Device, aspect_ratio: f32) -> Self {
let projection_matrix =
glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0);
let view_matrix = glam::Mat4::look_at_rh(glam::Vec3::ZERO, glam::Vec3::ZERO, glam::Vec3::Z);
let camera_matrix = projection_matrix * view_matrix;
let camera_matrix_ref: &[f32; 16] = camera_matrix.as_ref();
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(camera_matrix_ref),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
Self {
projection_matrix,
view_matrix,
buffer,
}
}
fn set_camera_position(&mut self, position: glam::Vec3) {
self.view_matrix = glam::Mat4::look_at_rh(position, glam::Vec3::ZERO, glam::Vec3::Z);
}
fn set_aspect_ratio(&mut self, ratio: f32) {
self.projection_matrix =
glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, ratio, 1.0, 10.0);
}
fn update_buffer(&self, queue: &wgpu::Queue) {
let camera_matrix = self.projection_matrix * self.view_matrix;
let camera_matrix_ref: &[f32; 16] = camera_matrix.as_ref();
queue.write_buffer(&self.buffer, 0, bytemuck::cast_slice(camera_matrix_ref))
}
fn binding(&self) -> wgpu::BindingResource<'_> {
self.buffer.as_entire_binding()
}
}
impl DemRenderer { impl DemRenderer {
pub fn new( pub fn new(
source: Rc<Dem>, source: Rc<Dem>,
@ -89,10 +42,16 @@ impl DemRenderer {
let index_count = index_data.len(); let index_count = index_data.len();
let uniforms = Uniforms::new( let uniform_buf = {
device, let mx_total =
surface_config.width as f32 / surface_config.height as f32, generate_matrix(surface_config.width as f32 / surface_config.height as f32);
); let mx_ref: &[f32; 16] = mx_total.as_ref();
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(mx_ref),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
})
};
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None, label: None,
@ -124,7 +83,7 @@ impl DemRenderer {
layout: &bind_group_layout, layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry { entries: &[wgpu::BindGroupEntry {
binding: 0, binding: 0,
resource: uniforms.binding(), resource: uniform_buf.as_entire_binding(),
}], }],
label: Some("DemRendererBindGroup"), label: Some("DemRendererBindGroup"),
}); });
@ -181,8 +140,6 @@ impl DemRenderer {
vertex_buffer, vertex_buffer,
index_buffer, index_buffer,
index_count, index_count,
animation_start: std::time::Instant::now(),
uniforms,
} }
} }
@ -190,9 +147,6 @@ impl DemRenderer {
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("DemRendererCommandEncoder"), label: Some("DemRendererCommandEncoder"),
}); });
self.uniforms
.set_camera_position(get_animated_camera_position(self.animation_start.elapsed()));
self.uniforms.update_buffer(queue);
{ {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("DemRendererRenderPass"), label: Some("DemRendererRenderPass"),
@ -270,11 +224,13 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
(vertex_data.to_vec(), index_data.to_vec()) (vertex_data.to_vec(), index_data.to_vec())
} }
fn get_animated_camera_position(animation_time: std::time::Duration) -> glam::Vec3 { fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 {
let animation_phase = 2.0 * std::f32::consts::PI * (animation_time.as_secs_f32() % 10.0) / 10.0; let projection =
glam::Vec3::new( glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0);
5.0 * f32::sin(animation_phase), let view = glam::Mat4::look_at_rh(
5.0 * f32::cos(animation_phase), glam::Vec3::new(1.5f32, -5.0, 3.0),
3.0, glam::Vec3::ZERO,
) glam::Vec3::Z,
);
projection * view
} }

View File

@ -106,7 +106,6 @@ impl MvuApp<Model> for App {
let mut config = surface let mut config = surface
.get_default_config(&adapter, size.width, size.height) .get_default_config(&adapter, size.width, size.height)
.unwrap(); .unwrap();
config.present_mode = wgpu::PresentMode::AutoVsync;
config.view_formats.push(config.format); config.view_formats.push(config.format);
surface.configure(&device, &config); surface.configure(&device, &config);

View File

@ -45,10 +45,6 @@ where
block_on(self.app.init(&instance, surface, Size2i { width, height })); block_on(self.app.init(&instance, surface, Size2i { width, height }));
} }
// TODO: The idea with these `block_on()` calls is that eventually I'll
// write an async executor that runs on top of the winit even loop and then
// this stuff can be properly async.
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
match event { match event {
WindowEvent::Resized(new_size) => block_on(self.app.resize(Size2i { WindowEvent::Resized(new_size) => block_on(self.app.resize(Size2i {
@ -59,10 +55,7 @@ where
println!("The close button was pressed; stopping"); println!("The close button was pressed; stopping");
event_loop.exit(); event_loop.exit();
} }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => block_on(self.app.view(self.model.clone())).unwrap(),
block_on(self.app.view(self.model.clone())).unwrap();
self.window.as_ref().unwrap().request_redraw();
},
WindowEvent::DroppedFile(file_path) => { WindowEvent::DroppedFile(file_path) => {
self.model = block_on( self.model = block_on(
self.app self.app