From 59fe34963335f5a23b37cc3f28769e851b8ae7ed Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 21 Feb 2019 14:14:42 -0500 Subject: [PATCH] Resize support for gfx-cube and framework --- gfx-examples/src/cube.rs | 36 ++++++++++++++++++++++------------- gfx-examples/src/framework.rs | 8 ++++++-- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/gfx-examples/src/cube.rs b/gfx-examples/src/cube.rs index 1cc540997..13f839c6f 100644 --- a/gfx-examples/src/cube.rs +++ b/gfx-examples/src/cube.rs @@ -88,9 +88,22 @@ struct Cube { index_buf: wgpu::Buffer, index_count: usize, bind_group: wgpu::BindGroup, + uniform_buf: wgpu::Buffer, pipeline: wgpu::RenderPipeline, } +impl Cube { + fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { + let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0); + let mx_view = cgmath::Matrix4::look_at( + cgmath::Point3::new(1.5f32, -5.0, 3.0), + cgmath::Point3::new(0f32, 0.0, 0.0), + cgmath::Vector3::unit_z(), + ); + mx_projection * mx_view + } +} + impl framework::Example for Cube { fn init(device: &mut wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Self { use std::mem; @@ -196,18 +209,9 @@ impl framework::Example for Cube { size: 64, usage: wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST, }); - { - let aspect_ratio = sc_desc.width as f32 / sc_desc.height as f32; - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0); - let mx_view = cgmath::Matrix4::look_at( - cgmath::Point3::new(1.5f32, -5.0, 3.0), - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_z(), - ); - let mx_total = mx_projection * mx_view; - let mx_raw: &[f32; 16] = mx_total.as_ref(); - uniform_buf.set_sub_data(0, framework::cast_slice(&mx_raw[..])); - } + let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32); + let mx_ref: &[f32; 16] = mx_total.as_ref(); + uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..])); // Create bind group let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -294,11 +298,17 @@ impl framework::Example for Cube { index_buf, index_count: index_data.len(), bind_group, + uniform_buf, pipeline, } } - fn update(&mut self, _event: wgpu::winit::WindowEvent) { + fn update(&mut self, event: wgpu::winit::WindowEvent) { + if let wgpu::winit::WindowEvent::Resized(size) = event { + let mx_total = Self::generate_matrix(size.width as f32 / size.height as f32); + let mx_ref: &[f32; 16] = mx_total.as_ref(); + self.uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..])); + } } fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) { diff --git a/gfx-examples/src/framework.rs b/gfx-examples/src/framework.rs index a0a07479f..ec0514ef7 100644 --- a/gfx-examples/src/framework.rs +++ b/gfx-examples/src/framework.rs @@ -70,7 +70,7 @@ pub fn run(title: &str) { .to_physical(window.get_hidpi_factor()); let surface = instance.create_surface(&window); - let sc_desc = wgpu::SwapChainDescriptor { + let mut sc_desc = wgpu::SwapChainDescriptor { usage: wgpu::TextureUsageFlags::OUTPUT_ATTACHMENT, format: wgpu::TextureFormat::B8g8r8a8Unorm, width: size.width as u32, @@ -91,7 +91,11 @@ pub fn run(title: &str) { .. } => { let physical = size.to_physical(window.get_hidpi_factor()); - info!("Resized to {:?}", physical); + info!("Resizing to {:?}", physical); + sc_desc.width = physical.width as u32; + sc_desc.height = physical.height as u32; + swap_chain = device.create_swap_chain(&surface, &sc_desc); + example.update(WindowEvent::Resized(size)); } Event::WindowEvent { event, .. } => match event { WindowEvent::KeyboardInput {