Render volume of DEM instead of cube
Instead of a +/-1 cube, draw the AABB of the DEM
This commit is contained in:
parent
e89c1e4c3d
commit
e26af202ef
|
|
@ -22,6 +22,12 @@ struct Vertex {
|
|||
position: [f32; 4],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
fn new(x:f32, y:f32, z:f32) -> Self {
|
||||
Self { position: [x, y, z, 1.0] }
|
||||
}
|
||||
}
|
||||
|
||||
struct Uniforms {
|
||||
projection_matrix: glam::Mat4,
|
||||
view_matrix: glam::Mat4,
|
||||
|
|
@ -31,7 +37,7 @@ struct Uniforms {
|
|||
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);
|
||||
glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10000.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();
|
||||
|
|
@ -47,8 +53,8 @@ impl Uniforms {
|
|||
}
|
||||
}
|
||||
|
||||
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_camera_position(&mut self, camera_position: glam::Vec3, camera_look_at: glam::Vec3) {
|
||||
self.view_matrix = glam::Mat4::look_at_rh(camera_position, camera_look_at, glam::Vec3::Z);
|
||||
}
|
||||
|
||||
fn set_aspect_ratio(&mut self, ratio: f32) {
|
||||
|
|
@ -73,7 +79,7 @@ impl DemRenderer {
|
|||
device: &wgpu::Device,
|
||||
surface_config: &wgpu::SurfaceConfiguration,
|
||||
) -> Self {
|
||||
let (vertex_data, index_data) = create_vertices();
|
||||
let (vertex_data, index_data) = create_vertices(&source);
|
||||
|
||||
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("DemRenderer Vertex Buffer"),
|
||||
|
|
@ -190,8 +196,13 @@ impl DemRenderer {
|
|||
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("DemRendererCommandEncoder"),
|
||||
});
|
||||
self.uniforms
|
||||
.set_camera_position(get_animated_camera_position(self.animation_start.elapsed()));
|
||||
self.uniforms.set_camera_position(
|
||||
get_animated_camera_position(
|
||||
self.animation_start.elapsed(),
|
||||
self.get_max_dem_dimension(),
|
||||
),
|
||||
self.get_dem_centre(),
|
||||
);
|
||||
self.uniforms.update_buffer(queue);
|
||||
{
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
|
|
@ -216,46 +227,61 @@ impl DemRenderer {
|
|||
}
|
||||
queue.submit(Some(encoder.finish()));
|
||||
}
|
||||
|
||||
fn get_max_dem_dimension(&self) -> f32 {
|
||||
let dem = &self.source;
|
||||
(dem.max_x - dem.min_x)
|
||||
.max(dem.max_y - dem.max_y)
|
||||
.max(dem.max_z - dem.min_x)
|
||||
}
|
||||
|
||||
fn vertex(pos: [i8; 3]) -> Vertex {
|
||||
Vertex {
|
||||
position: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0],
|
||||
fn get_dem_centre(&self) -> glam::Vec3 {
|
||||
let dem = &self.source;
|
||||
let min_corner = glam::Vec3::new(dem.min_x, dem.min_y, dem.min_z);
|
||||
let max_corner = glam::Vec3::new(dem.max_x, dem.max_y, dem.max_z);
|
||||
min_corner + (max_corner - min_corner) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
|
||||
fn create_vertices(dem: &Rc<Dem>) -> (Vec<Vertex>, Vec<u16>) {
|
||||
let min_x = dem.min_x;
|
||||
let max_x = dem.max_x;
|
||||
let min_y = dem.min_y;
|
||||
let max_y = dem.max_y;
|
||||
let min_z = dem.min_z;
|
||||
let max_z = dem.max_z;
|
||||
|
||||
let vertex_data = [
|
||||
// top (0, 0, 1)
|
||||
vertex([-1, -1, 1]),
|
||||
vertex([1, -1, 1]),
|
||||
vertex([1, 1, 1]),
|
||||
vertex([-1, 1, 1]),
|
||||
Vertex::new(min_x, min_y, max_z),
|
||||
Vertex::new(max_x, min_y, max_z),
|
||||
Vertex::new(max_x, max_y, max_z),
|
||||
Vertex::new(min_x, max_y, max_z),
|
||||
// bottom (0, 0, -1)
|
||||
vertex([-1, 1, -1]),
|
||||
vertex([1, 1, -1]),
|
||||
vertex([1, -1, -1]),
|
||||
vertex([-1, -1, -1]),
|
||||
Vertex::new(min_x, max_y, min_z),
|
||||
Vertex::new(max_x, max_y, min_z),
|
||||
Vertex::new(max_x, min_y, min_z),
|
||||
Vertex::new(min_x, min_y, min_z),
|
||||
// right (1, 0, 0)
|
||||
vertex([1, -1, -1]),
|
||||
vertex([1, 1, -1]),
|
||||
vertex([1, 1, 1]),
|
||||
vertex([1, -1, 1]),
|
||||
Vertex::new(max_x, min_y, min_z),
|
||||
Vertex::new(max_x, max_y, min_z),
|
||||
Vertex::new(max_x, max_y, max_z),
|
||||
Vertex::new(max_x, min_y, max_z),
|
||||
// left (-1, 0, 0)
|
||||
vertex([-1, -1, 1]),
|
||||
vertex([-1, 1, 1]),
|
||||
vertex([-1, 1, -1]),
|
||||
vertex([-1, -1, -1]),
|
||||
Vertex::new(min_x, min_y, max_z),
|
||||
Vertex::new(min_x, max_y, max_z),
|
||||
Vertex::new(min_x, max_y, min_z),
|
||||
Vertex::new(min_x, min_y, min_z),
|
||||
// front (0, 1, 0)
|
||||
vertex([1, 1, -1]),
|
||||
vertex([-1, 1, -1]),
|
||||
vertex([-1, 1, 1]),
|
||||
vertex([1, 1, 1]),
|
||||
Vertex::new(max_x, max_y, min_z),
|
||||
Vertex::new(min_x, max_y, min_z),
|
||||
Vertex::new(min_x, max_y, max_z),
|
||||
Vertex::new(max_x, max_y, max_z),
|
||||
// back (0, -1, 0)
|
||||
vertex([1, -1, 1]),
|
||||
vertex([-1, -1, 1]),
|
||||
vertex([-1, -1, -1]),
|
||||
vertex([1, -1, -1]),
|
||||
Vertex::new(max_x, min_y, max_z),
|
||||
Vertex::new(min_x, min_y, max_z),
|
||||
Vertex::new(min_x, min_y, min_z),
|
||||
Vertex::new(max_x, min_y, min_z),
|
||||
];
|
||||
|
||||
let index_data: &[u16] = &[
|
||||
|
|
@ -270,11 +296,11 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
|
|||
(vertex_data.to_vec(), index_data.to_vec())
|
||||
}
|
||||
|
||||
fn get_animated_camera_position(animation_time: std::time::Duration) -> 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;
|
||||
glam::Vec3::new(
|
||||
5.0 * f32::sin(animation_phase),
|
||||
5.0 * f32::cos(animation_phase),
|
||||
3.0,
|
||||
dem_size * f32::sin(animation_phase),
|
||||
dem_size * f32::cos(animation_phase),
|
||||
dem_size * 0.7,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue