diff --git a/src/bin/wireframe/main.rs b/src/bin/wireframe/main.rs index 8c6eceb..8b29463 100644 --- a/src/bin/wireframe/main.rs +++ b/src/bin/wireframe/main.rs @@ -157,6 +157,8 @@ static KEYS_ROTATE: &'static [(PhysicalKey, Vec3)] = &[ #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct CameraUniform { mvp: [[f32; 4]; 4], + scale: [f32; 2], + pad: [u32; 2], } #[repr(C)] @@ -178,7 +180,6 @@ struct State<'a> { cam: camctl::CameraLocation, t1: Instant, - camera_uniform: CameraUniform, camera_buffer: wgpu::Buffer, camera_bind_group: wgpu::BindGroup, @@ -242,13 +243,11 @@ impl<'a> State<'a> { let cam = camctl::CameraLocation::new(); let t1 = Instant::now(); - let camera_uniform = CameraUniform { - mvp: cam.view_mtx().to_cols_array_2d(), - }; - let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + let camera_buffer = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Camera Buffer"), - contents: bytemuck::cast_slice(&[camera_uniform]), usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + size: mem::size_of::() as u64, + mapped_at_creation: false, }); let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -357,7 +356,6 @@ impl<'a> State<'a> { scene, camera_buffer, camera_bind_group, - camera_uniform, window, } } @@ -398,12 +396,13 @@ impl<'a> State<'a> { ); let view = my_to_gl * self.cam.view_mtx(); let mvp = proj * view; - self.camera_uniform.mvp = mvp.to_cols_array_2d(); - self.queue.write_buffer( - &self.camera_buffer, - 0, - bytemuck::cast_slice(&[self.camera_uniform]), - ); + let camera_uniform = CameraUniform { + mvp: mvp.to_cols_array_2d(), + scale: (1. / size).to_array(), + pad: [0; 2], + }; + self.queue + .write_buffer(&self.camera_buffer, 0, bytemuck::bytes_of(&camera_uniform)); } fn render(&mut self) -> Result<(), wgpu::SurfaceError> { diff --git a/src/bin/wireframe/ray.wgsl b/src/bin/wireframe/ray.wgsl index 4d5cc4e..e837f01 100644 --- a/src/bin/wireframe/ray.wgsl +++ b/src/bin/wireframe/ray.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { mvp: mat4x4, + scale: vec2, } @group(0) @binding(0) var camera: CameraUniform; @@ -30,7 +31,8 @@ fn vs_main(seg: SegmentInput, off: OffsetInput) -> VertexOutput { let a = camera.mvp * vec4(seg.a, 1.); let b = camera.mvp * vec4(seg.b, 1.); let dir = normalize(b.xy / b.w - a.xy / a.w); - let d = width * vec4(-dir.y, dir.x, 0., 0.); + let ortho = width * camera.scale * vec2(-dir.y, dir.x); + let d = vec4(ortho, 0., 0.); switch (off.idx) { case 0u: { out.clip_position = a - d; } case 1u: { out.clip_position = a + d; }