Skip to content

Commit

Permalink
Toggle hidden-line drawing with the X key in the viewer app (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschwind authored Jan 21, 2024
1 parent 8db00f6 commit 7ed1ba8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 19 additions & 10 deletions crates/viewer/src/edge_drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct EdgeDrawer {
bind_groups: BindGroups,
screen_width: u32,
screen_height: u32,
draw_back_edges: bool,
}

impl EdgeDrawer {
Expand Down Expand Up @@ -61,6 +62,7 @@ impl EdgeDrawer {
bind_groups,
screen_width,
screen_height,
draw_back_edges: false,
}
}

Expand All @@ -69,6 +71,10 @@ impl EdgeDrawer {
self.screen_height = screen_height;
}

pub fn toggle_back_edge_drawing(&mut self) {
self.draw_back_edges = !self.draw_back_edges;
}

#[allow(clippy::too_many_arguments)]
pub fn draw(
&self,
Expand Down Expand Up @@ -125,19 +131,22 @@ impl EdgeDrawer {
timestamp_writes: None,
});

// Render dashed line strips
render_pass.set_pipeline(&self.dashed_line_strip_pipeline);
render_pass.set_vertex_buffer(0, self.buffers.round_strip_geometry.slice(..));
render_pass.set_vertex_buffer(1, rendered_line.vertex_buf.slice(..));
render_pass.set_bind_group(0, &self.bind_groups.dashed_vertex_uniform, &[]);

let mut offset = 0usize;
let vertex_count = self.buffers.round_strip_geometry_len as u32;

for line_strip_size in &rendered_line.line_sizes {
let range = (offset as u32)..(offset + line_strip_size - 1) as u32;
offset += line_strip_size;
render_pass.draw(0..vertex_count, range);
// Render dashed line strips
if self.draw_back_edges {
render_pass.set_pipeline(&self.dashed_line_strip_pipeline);
render_pass.set_bind_group(0, &self.bind_groups.dashed_vertex_uniform, &[]);

let mut offset = 0usize;
let vertex_count = self.buffers.round_strip_geometry_len as u32;

for line_strip_size in &rendered_line.line_sizes {
let range = (offset as u32)..(offset + line_strip_size - 1) as u32;
offset += line_strip_size;
render_pass.draw(0..vertex_count, range);
}
}

// Render solid line strips
Expand Down
8 changes: 7 additions & 1 deletion crates/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,18 @@ impl GameApp for ViewerApp {
self.camera.zoom(-zoom_delta);
},
WindowEvent::KeyboardInput {
event: KeyEvent { physical_key: PhysicalKey::Code(key_code), .. },
event:
KeyEvent {
physical_key: PhysicalKey::Code(key_code),
state: ElementState::Pressed,
..
},
..
} => match key_code {
KeyCode::Escape => window_target.exit(),
KeyCode::KeyP => self.camera.use_perspective(),
KeyCode::KeyO => self.camera.use_orthographic(),
KeyCode::KeyX => self.line_drawer.toggle_back_edge_drawing(),
_ => {},
},
_ => {},
Expand Down

0 comments on commit 7ed1ba8

Please sign in to comment.