font rendering

This commit is contained in:
core 2023-11-08 15:05:11 -05:00
parent 32c84ad613
commit ebf1f35eaa
Signed by: core
GPG Key ID: FDBF740DADDCEECF
9 changed files with 71 additions and 20 deletions

View File

@ -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"

View File

@ -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,
)
}

Binary file not shown.

View File

@ -1,6 +1,4 @@
pub mod colors;
pub mod command;
pub mod equirectangular;
pub mod mode;
pub mod scope;
pub mod sites;

View File

@ -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]
}

View File

@ -1 +1,2 @@
pub mod vertex;
pub mod vertex;
pub mod colors;

View File

@ -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<u32>,
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(())
}
}

BIN
nexrad-browser/www/NXRD.ttf Normal file

Binary file not shown.

View File

@ -23,6 +23,11 @@
padding: 0;
overflow: hidden;
}
@font-face {
font-family: NXRD;
src: url(./NXRD.ttf);
}
</style>
</head>
<body>