Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hacky model bounding box info to rendering UI #217

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/graphics/config_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use wgpu_glyph::{
GlyphBrush, GlyphBrushBuilder, Section, Text,
};

use crate::math::Aabb;

use super::{draw_config::DrawConfig, COLOR_FORMAT};

#[derive(Debug)]
Expand Down Expand Up @@ -45,6 +47,7 @@ impl ConfigUi {
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
surface_config: &wgpu::SurfaceConfiguration,
aabb: &Aabb<3>,
draw_config: &DrawConfig,
) -> Result<(), String> {
let mut section = Section::new().with_screen_position((50.0, 50.0));
Expand All @@ -62,6 +65,19 @@ impl ConfigUi {
section = section.add_text(text);
}

/* Render size of model bounding box */
let bbsize = aabb.size().components();
let info = format!(
"Model bounding box size: {:0.1} {:0.1} {:0.1}",
bbsize[0].into_f32(),
bbsize[1].into_f32(),
bbsize[2].into_f32()
);
let text = Text::new(&info)
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(50.0);
section = section.add_text(text);

self.glyph_brush.queue(section);
self.glyph_brush.draw_queued(
device,
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/geometries.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::math::Aabb;
use std::convert::TryInto;

use wgpu::util::DeviceExt;
Expand All @@ -8,19 +9,21 @@ use super::vertices::{Vertex, Vertices};
pub struct Geometries {
pub mesh: Geometry,
pub lines: Geometry,
pub aabb: Aabb<3>,
}

impl Geometries {
pub fn new(
device: &wgpu::Device,
mesh: &Vertices,
debug_info: &Vertices,
aabb: Aabb<3>,
) -> Self {
let mesh = Geometry::new(device, mesh.vertices(), mesh.indices());
let lines =
Geometry::new(device, debug_info.vertices(), debug_info.indices());

Self { mesh, lines }
Self { mesh, lines, aabb }
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wgpu::util::DeviceExt as _;
use wgpu_glyph::ab_glyph::InvalidFont;
use winit::dpi::PhysicalSize;

use crate::{camera::Camera, window::Window};
use crate::{camera::Camera, math::Aabb, math::Point, window::Window};

use super::{
config_ui::ConfigUi, draw_config::DrawConfig, drawables::Drawables,
Expand Down Expand Up @@ -114,8 +114,15 @@ impl Renderer {
label: None,
});

let geometries =
Geometries::new(&device, &Vertices::empty(), &Vertices::empty());
let geometries = Geometries::new(
&device,
&Vertices::empty(),
&Vertices::empty(),
Aabb {
min: Point::from([0.0, 0.0, 0.0]),
max: Point::from([0.0, 0.0, 0.0]),
},
);
let pipelines = Pipelines::new(&device, &bind_group_layout);

let config_ui = ConfigUi::new(&device)?;
Expand All @@ -138,8 +145,13 @@ impl Renderer {
})
}

pub fn update_geometry(&mut self, mesh: Vertices, lines: Vertices) {
self.geometries = Geometries::new(&self.device, &mesh, &lines);
pub fn update_geometry(
&mut self,
mesh: Vertices,
lines: Vertices,
aabb: Aabb<3>,
) {
self.geometries = Geometries::new(&self.device, &mesh, &lines, aabb);
}

pub fn handle_resize(&mut self, size: PhysicalSize<u32>) {
Expand Down Expand Up @@ -216,6 +228,7 @@ impl Renderer {
&mut encoder,
&color_view,
&self.surface_config,
&self.geometries.aabb,
config,
)
.map_err(|err| DrawError::Text(err))?;
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn main() -> anyhow::Result<()> {
let mut input_handler = input::Handler::new(previous_time);
let mut renderer = block_on(Renderer::new(&window))?;

renderer.update_geometry((&triangles).into(), (&debug_info).into());
renderer.update_geometry((&triangles).into(), (&debug_info).into(), aabb);

let mut draw_config = DrawConfig::default();
let mut camera = Camera::new(&aabb);
Expand All @@ -218,8 +218,11 @@ fn main() -> anyhow::Result<()> {
aabb = shape.bounding_volume();
faces.triangles(tolerance, &mut triangles, &mut debug_info);

renderer
.update_geometry((&triangles).into(), (&debug_info).into());
renderer.update_geometry(
(&triangles).into(),
(&debug_info).into(),
aabb,
);
}
Err(mpsc::TryRecvError::Empty) => {
// Nothing to receive from the channel. We don't care.
Expand Down