diff --git a/nexrad-browser/Cargo.toml b/nexrad-browser/Cargo.toml index 7bcb64e..c9906cf 100644 --- a/nexrad-browser/Cargo.toml +++ b/nexrad-browser/Cargo.toml @@ -28,6 +28,9 @@ raw-window-handle = "0.5" wasm-bindgen-futures = "0.4" bytemuck = { version = "1", features = ["derive"]} +# Text rendering # +wgpu_glyph = { git = "https://github.com/hecrj/wgpu_glyph" } + [dependencies.js-sys] version = "0.3" diff --git a/nexrad-browser/src/equirectangular.rs b/nexrad-browser/src/equirectangular.rs deleted file mode 100644 index f385bb3..0000000 --- a/nexrad-browser/src/equirectangular.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub const EARTH_RADIUS_MILES: f64 = 3958.8; - -pub fn latlong_to_xy(lat: f64, long: f64) -> (f64, f64) { - // x = r λ cos(φ0) - // y = r φ - ( - EARTH_RADIUS_MILES * long * lat.cos(), - EARTH_RADIUS_MILES * lat, - ) -} diff --git a/nexrad-browser/src/fonts/Hermit-Regular.otf b/nexrad-browser/src/fonts/Hermit-Regular.otf new file mode 100644 index 0000000..feb1937 Binary files /dev/null and b/nexrad-browser/src/fonts/Hermit-Regular.otf differ diff --git a/nexrad-browser/src/lib.rs b/nexrad-browser/src/lib.rs index ef2416c..2be61e0 100644 --- a/nexrad-browser/src/lib.rs +++ b/nexrad-browser/src/lib.rs @@ -1,6 +1,4 @@ -pub mod colors; pub mod command; -pub mod equirectangular; pub mod mode; pub mod scope; pub mod sites; diff --git a/nexrad-browser/src/colors.rs b/nexrad-browser/src/rendering/colors.rs similarity index 98% rename from nexrad-browser/src/colors.rs rename to nexrad-browser/src/rendering/colors.rs index ddc44df..976e492 100644 --- a/nexrad-browser/src/colors.rs +++ b/nexrad-browser/src/rendering/colors.rs @@ -211,7 +211,7 @@ pub fn color_scheme(product: Mode, value: f32) -> &'static str { } } -pub fn rgb_to_srgb(c: u32) -> [f32; 3] { +pub fn rgb_to_srgb(c: u32, a: f32) -> [f32; 4] { let f = |xu: u32| { let x = (xu & 0xFF) as f32 / 255.0; if x > 0.04045 { @@ -220,5 +220,5 @@ pub fn rgb_to_srgb(c: u32) -> [f32; 3] { x / 12.92 } }; - [f(c >> 16), f(c >> 8), f(c)] + [f(c >> 16), f(c >> 8), f(c), a] } \ No newline at end of file diff --git a/nexrad-browser/src/rendering/mod.rs b/nexrad-browser/src/rendering/mod.rs index 348fb7a..222b41d 100644 --- a/nexrad-browser/src/rendering/mod.rs +++ b/nexrad-browser/src/rendering/mod.rs @@ -1 +1,2 @@ -pub mod vertex; \ No newline at end of file +pub mod vertex; +pub mod colors; diff --git a/nexrad-browser/src/scope.rs b/nexrad-browser/src/scope.rs index 4882dcf..4931cb4 100644 --- a/nexrad-browser/src/scope.rs +++ b/nexrad-browser/src/scope.rs @@ -13,10 +13,13 @@ use wgpu::{ RenderPassDescriptor, StoreOp, Surface, SurfaceConfiguration, SurfaceError, TextureUsages, TextureViewDescriptor, }; -use wgpu::util::DeviceExt; +use wgpu::util::{DeviceExt, StagingBelt}; +use wgpu_glyph::ab_glyph::FontArc; +use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, HorizontalAlign, Layout, Section, Text}; use winit::dpi::PhysicalSize; use winit::event::WindowEvent; use winit::window::Window; +use crate::rendering::colors::rgb_to_srgb; use crate::rendering::vertex::{VERTICES}; pub struct ScopeState { @@ -42,7 +45,9 @@ pub struct WgpuState { pub size: PhysicalSize, pub window: Window, pub vertex_buffer: wgpu::Buffer, - pub pipeline: RenderPipeline + pub pipeline: RenderPipeline, + pub staging_belt: StagingBelt, + pub glyph_brush: GlyphBrush<()> } impl WgpuState { @@ -151,6 +156,10 @@ impl WgpuState { multiview: None, }); + let staging_belt = StagingBelt::new(1024); + let font = FontArc::try_from_slice(include_bytes!("./fonts/Hermit-Regular.otf")).unwrap(); + let mut glyph_brush = GlyphBrushBuilder::using_font(font).build(&device, surface_config.format); + Self { window, surface, @@ -159,7 +168,9 @@ impl WgpuState { surface_config, size, vertex_buffer, - pipeline: render_pipeline + pipeline: render_pipeline, + staging_belt, + glyph_brush } } @@ -193,10 +204,12 @@ impl WgpuState { label: Some("Render Encoder"), }); - let _physical_width = (self.surface_config.width as f64 * self.window.scale_factor()) as f32; - let _physical_height = + let physical_width = (self.surface_config.width as f64 * self.window.scale_factor()) as f32; + let physical_height = (self.surface_config.height as f64 * self.window.scale_factor()) as f32; + + // LOCK BLOCK { let mut render_pass = encoder.begin_render_pass(&RenderPassDescriptor { @@ -224,9 +237,50 @@ impl WgpuState { } + self.glyph_brush.queue(Section { + screen_position: (10.0, 10.0), + bounds: (self.surface_config.width as f32, self.surface_config.height as f32), + text: vec![Text::new("NEXRAD INOP 00:00:00Z") + .with_color(rgb_to_srgb(0x32cd32, 1.0)) + .with_scale(20.0)], + ..Section::default() + }); + + self.glyph_brush.queue(Section { + screen_position: (10.0, 32.0), + bounds: (self.surface_config.width as f32, self.surface_config.height as f32), + text: vec![Text::new("RADAR INOPERATIVE NO DATA LOADED") + .with_color(rgb_to_srgb(0xff0000, 1.0)) + .with_scale(26.0)], + ..Section::default() + }); + + self.glyph_brush.queue(Section { + screen_position: (self.surface_config.width as f32 - 10.0, 10.0), + bounds: (self.surface_config.width as f32, self.surface_config.height as f32), + text: vec![Text::new("RADAR SITE") + .with_color(rgb_to_srgb(0x32cd32, 1.0)) + .with_scale(12.0)], + ..Section::default() + }.with_layout(Layout::default().h_align(HorizontalAlign::Right))); + + self.glyph_brush + .draw_queued( + &self.device, + &mut self.staging_belt, + &mut encoder, + &view, + self.surface_config.width, + self.surface_config.height, + ) + .expect("Draw queued"); + + self.staging_belt.finish(); self.queue.submit(iter::once(encoder.finish())); output.present(); + self.staging_belt.recall(); + Ok(()) } } diff --git a/nexrad-browser/www/NXRD.ttf b/nexrad-browser/www/NXRD.ttf new file mode 100644 index 0000000..aae20dc Binary files /dev/null and b/nexrad-browser/www/NXRD.ttf differ diff --git a/nexrad-browser/www/index.html b/nexrad-browser/www/index.html index 6c95dc0..e2c7943 100644 --- a/nexrad-browser/www/index.html +++ b/nexrad-browser/www/index.html @@ -23,6 +23,11 @@ padding: 0; overflow: hidden; } + + @font-face { + font-family: NXRD; + src: url(./NXRD.ttf); + }