Factor out DEM texture creation into separate function

This commit is contained in:
Matthew Gordon 2024-11-22 20:40:56 -04:00
parent e7180e57bf
commit ab2e366e7f
1 changed files with 44 additions and 38 deletions

View File

@ -143,44 +143,7 @@ impl DemRenderer {
let index_count = index_data.len(); let index_count = index_data.len();
let (dem_texture_view, dem_texture_size) = { let (dem_texture_view, dem_texture_size) = load_dem_texture(&source, device, queue);
let texture_size = wgpu::Extent3d {
width: source.num_cells_x,
height: source.num_cells_y,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Dem Texture"),
size: texture_size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R16Uint,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(&source.grid[..]),
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(std::mem::size_of::<u16>() as u32 * source.num_cells_x),
rows_per_image: Some(source.num_cells_y),
},
texture_size,
);
(
texture.create_view(&wgpu::TextureViewDescriptor::default()),
glam::UVec2::new(source.num_cells_x, source.num_cells_y),
)
};
let camera = Camera::new(surface_config.width as f32 / surface_config.height as f32); let camera = Camera::new(surface_config.width as f32 / surface_config.height as f32);
@ -391,6 +354,49 @@ fn create_vertices(dem: &Rc<Dem>) -> (Vec<Vertex>, Vec<u16>) {
(vertex_data.to_vec(), index_data.to_vec()) (vertex_data.to_vec(), index_data.to_vec())
} }
fn load_dem_texture(
source: &Dem,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> (wgpu::TextureView, glam::UVec2) {
let texture_size = wgpu::Extent3d {
width: source.num_cells_x,
height: source.num_cells_y,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Dem Texture"),
size: texture_size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R16Uint,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(&source.grid[..]),
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(std::mem::size_of::<u16>() as u32 * source.num_cells_x),
rows_per_image: Some(source.num_cells_y),
},
texture_size,
);
(
texture.create_view(&wgpu::TextureViewDescriptor::default()),
glam::UVec2::new(source.num_cells_x, source.num_cells_y),
)
}
fn get_animated_camera_position(animation_time: std::time::Duration, dem_size: f32) -> glam::Vec3 { fn get_animated_camera_position(animation_time: std::time::Duration, dem_size: f32) -> glam::Vec3 {
let animation_phase = 2.0 * std::f32::consts::PI * (animation_time.as_secs_f32() % 10.0) / 10.0; let animation_phase = 2.0 * std::f32::consts::PI * (animation_time.as_secs_f32() % 10.0) / 10.0;
glam::Vec3::new( glam::Vec3::new(