render work
This commit is contained in:
parent
a4b72133ec
commit
b6bf4cdcd8
9 changed files with 355 additions and 225 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -5111,9 +5111,9 @@ dependencies = [
|
||||||
"egui",
|
"egui",
|
||||||
"egui_glow",
|
"egui_glow",
|
||||||
"ehttp",
|
"ehttp",
|
||||||
|
"glow",
|
||||||
"image",
|
"image",
|
||||||
"poll-promise",
|
"poll-promise",
|
||||||
"serde",
|
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"tracing-web",
|
"tracing-web",
|
||||||
|
|
BIN
crates/client/0.png
Normal file
BIN
crates/client/0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
|
@ -7,9 +7,9 @@ edition = "2021"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
eframe = { version = "0.31" }
|
eframe = { version = "0.31" }
|
||||||
egui = "0.31"
|
glow = "*"
|
||||||
egui_glow = "0.31"
|
egui_glow = "0.31"
|
||||||
serde = { version = "1", features = ["derive"] }
|
egui = "0.31"
|
||||||
poll-promise = "0.3"
|
poll-promise = "0.3"
|
||||||
ehttp = "0.5"
|
ehttp = "0.5"
|
||||||
image = "0.25"
|
image = "0.25"
|
||||||
|
|
6
crates/client/dist/index.html
vendored
6
crates/client/dist/index.html
vendored
|
@ -94,7 +94,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<link rel="modulepreload" href="/wxbox-client-5dd7964ac85d800f.js" crossorigin="anonymous" integrity="sha384-THKbMzgvXjd63RE7bglp3RwijS0ny12ZwnqzI9Pidk1fDApKpehVPRwasSPwF4GV"><link rel="preload" href="/wxbox-client-5dd7964ac85d800f_bg.wasm" crossorigin="anonymous" integrity="sha384-ULmySDdbjrxZ77rmXQ2ZPFV0NVjvw9RhIuKxBP9ftAQbL4eIL9qoaW6phgsM9lMR" as="fetch" type="application/wasm"></head>
|
<link rel="modulepreload" href="/wxbox-client-416c2e642fbec6fa.js" crossorigin="anonymous" integrity="sha384-A3JOZ15bmE6xydSHdMZU5eIbrIJ8XY/DNJ5CtuoUisRN+ij0kXCl7eikCAcPsitl"><link rel="preload" href="/wxbox-client-416c2e642fbec6fa_bg.wasm" crossorigin="anonymous" integrity="sha384-ghOMvgGvT2pknRdTnVTjp4/1WJZM+FFxDmgzsy58m+J4sq4DnU1OY0c1JpIKbjhb" as="fetch" type="application/wasm"></head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<!-- The WASM code will resize the canvas dynamically -->
|
<!-- The WASM code will resize the canvas dynamically -->
|
||||||
|
@ -111,8 +111,8 @@
|
||||||
|
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import init, * as bindings from '/wxbox-client-5dd7964ac85d800f.js';
|
import init, * as bindings from '/wxbox-client-416c2e642fbec6fa.js';
|
||||||
const wasm = await init({ module_or_path: '/wxbox-client-5dd7964ac85d800f_bg.wasm' });
|
const wasm = await init({ module_or_path: '/wxbox-client-416c2e642fbec6fa_bg.wasm' });
|
||||||
|
|
||||||
|
|
||||||
window.wasmBindings = bindings;
|
window.wasmBindings = bindings;
|
||||||
|
|
|
@ -551,12 +551,6 @@ function __wbg_get_imports() {
|
||||||
imports.wbg.__wbg_deleteTexture_eaf729f97b59aaf4 = function(arg0, arg1) {
|
imports.wbg.__wbg_deleteTexture_eaf729f97b59aaf4 = function(arg0, arg1) {
|
||||||
arg0.deleteTexture(arg1);
|
arg0.deleteTexture(arg1);
|
||||||
};
|
};
|
||||||
imports.wbg.__wbg_deleteVertexArrayOES_0de32bd8adddeecb = function(arg0, arg1) {
|
|
||||||
arg0.deleteVertexArrayOES(arg1);
|
|
||||||
};
|
|
||||||
imports.wbg.__wbg_deleteVertexArray_cff2c6ab55f2c8b6 = function(arg0, arg1) {
|
|
||||||
arg0.deleteVertexArray(arg1);
|
|
||||||
};
|
|
||||||
imports.wbg.__wbg_deltaMode_b2e9bb0dca5cf196 = function(arg0) {
|
imports.wbg.__wbg_deltaMode_b2e9bb0dca5cf196 = function(arg0) {
|
||||||
const ret = arg0.deltaMode;
|
const ret = arg0.deltaMode;
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -1529,12 +1523,6 @@ function __wbg_get_imports() {
|
||||||
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
||||||
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
||||||
};
|
};
|
||||||
imports.wbg.__wbg_uniform1f_355e1f6a61fccb63 = function(arg0, arg1, arg2) {
|
|
||||||
arg0.uniform1f(arg1, arg2);
|
|
||||||
};
|
|
||||||
imports.wbg.__wbg_uniform1f_a8765c5b2bedff99 = function(arg0, arg1, arg2) {
|
|
||||||
arg0.uniform1f(arg1, arg2);
|
|
||||||
};
|
|
||||||
imports.wbg.__wbg_uniform1i_33a6ced29e8c7297 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbg_uniform1i_33a6ced29e8c7297 = function(arg0, arg1, arg2) {
|
||||||
arg0.uniform1i(arg1, arg2);
|
arg0.uniform1i(arg1, arg2);
|
||||||
};
|
};
|
||||||
|
@ -1626,19 +1614,19 @@ function __wbg_get_imports() {
|
||||||
const ret = false;
|
const ret = false;
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
imports.wbg.__wbindgen_closure_wrapper3066 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbindgen_closure_wrapper3076 = function(arg0, arg1, arg2) {
|
||||||
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_32);
|
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_32);
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
imports.wbg.__wbindgen_closure_wrapper3068 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbindgen_closure_wrapper3078 = function(arg0, arg1, arg2) {
|
||||||
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_35);
|
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_35);
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
imports.wbg.__wbindgen_closure_wrapper3070 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbindgen_closure_wrapper3080 = function(arg0, arg1, arg2) {
|
||||||
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_35);
|
const ret = makeMutClosure(arg0, arg1, 783, __wbg_adapter_35);
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
imports.wbg.__wbindgen_closure_wrapper5606 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbindgen_closure_wrapper5619 = function(arg0, arg1, arg2) {
|
||||||
const ret = makeMutClosure(arg0, arg1, 1467, __wbg_adapter_40);
|
const ret = makeMutClosure(arg0, arg1, 1467, __wbg_adapter_40);
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
Binary file not shown.
|
@ -2,13 +2,12 @@ use std::sync::{Arc, Mutex};
|
||||||
use eframe::{Frame, Storage};
|
use eframe::{Frame, Storage};
|
||||||
use egui::{Context, Event, MouseWheelUnit, Sense, Vec2};
|
use egui::{Context, Event, MouseWheelUnit, Sense, Vec2};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use crate::map::Map;
|
use crate::map::{Map};
|
||||||
use crate::map::osm::OSMBaselayer;
|
use crate::map::osm::OSMBaselayer;
|
||||||
use crate::map::tiles::{LayerManager, LayerSource};
|
use crate::map::tiles::{LayerManager, LayerSource};
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
map: Arc<Mutex<Map>>,
|
map: Arc<Mutex<Map>>
|
||||||
angle: f32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
|
@ -16,10 +15,8 @@ impl App {
|
||||||
let mut layer_manager = LayerManager::default();
|
let mut layer_manager = LayerManager::default();
|
||||||
layer_manager.layers.insert(0, OSMBaselayer::SOURCE_ID);
|
layer_manager.layers.insert(0, OSMBaselayer::SOURCE_ID);
|
||||||
|
|
||||||
let gl = cc.gl.as_ref().unwrap();
|
|
||||||
Self {
|
Self {
|
||||||
map: Arc::new(Mutex::new(Map::new(layer_manager, gl).unwrap())),
|
map: Arc::new(Mutex::new(Map::new(layer_manager, cc).unwrap())),
|
||||||
angle: 0.0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,58 +31,16 @@ impl eframe::App for App {
|
||||||
|
|
||||||
egui::CentralPanel::default()
|
egui::CentralPanel::default()
|
||||||
.show(ctx, |ui| {
|
.show(ctx, |ui| {
|
||||||
let (rect, response) = ui.allocate_exact_size(
|
|
||||||
ui.available_size(),
|
|
||||||
Sense::all()
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut deltas = [0.0, 0.0, 0.0];
|
|
||||||
|
|
||||||
deltas[0] = response.drag_motion().x;
|
|
||||||
deltas[1] = response.drag_motion().y;
|
|
||||||
if ui.rect_contains_pointer(rect) {
|
|
||||||
ctx.input(|i| {
|
|
||||||
for event in &i.events {
|
|
||||||
match event {
|
|
||||||
Event::MouseWheel { unit, delta, .. } => {
|
|
||||||
deltas[2] = match unit {
|
|
||||||
// TODO: scaling of this
|
|
||||||
MouseWheelUnit::Point => { delta.y },
|
|
||||||
MouseWheelUnit::Line => { delta.y },
|
|
||||||
MouseWheelUnit::Page => { delta.y },
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("{:?}", deltas);
|
|
||||||
self.angle += response.drag_motion().x * 0.01;
|
|
||||||
|
|
||||||
let angle = self.angle;
|
|
||||||
let map = self.map.clone();
|
|
||||||
|
|
||||||
{
|
{
|
||||||
map.lock().unwrap().update_view_data(&ctx);
|
let map = self.map.clone();
|
||||||
|
map.lock().unwrap().custom_painting(&ctx, ui);
|
||||||
}
|
}
|
||||||
|
|
||||||
let cb = egui_glow::CallbackFn::new(move |_info, painter| {
|
|
||||||
map.lock().unwrap().paint(painter.gl(), angle);
|
|
||||||
});
|
|
||||||
|
|
||||||
let callback = egui::PaintCallback {
|
|
||||||
rect,
|
|
||||||
callback: Arc::new(cb)
|
|
||||||
};
|
|
||||||
|
|
||||||
ui.painter().add(callback);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
fn on_exit(&mut self, gl: Option<&eframe::glow::Context>) {
|
|
||||||
|
fn on_exit(&mut self, gl: Option<&glow::Context>) {
|
||||||
if let Some(gl) = gl {
|
if let Some(gl) = gl {
|
||||||
self.map.lock().unwrap().destroy(gl);
|
self.map.lock().unwrap().render.lock().unwrap().destroy(gl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,144 +1,131 @@
|
||||||
pub mod tiles;
|
pub mod tiles;
|
||||||
pub mod osm;
|
pub mod osm;
|
||||||
|
mod render;
|
||||||
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use eframe::glow;
|
use std::num::{NonZeroU16, NonZeroU64};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use egui::{Event, MouseWheelUnit, Rect, Sense, Ui};
|
||||||
use image::{DynamicImage, ImageReader};
|
use image::{DynamicImage, ImageReader};
|
||||||
use poll_promise::Promise;
|
use poll_promise::Promise;
|
||||||
use tracing::warn;
|
use tracing::{debug, warn};
|
||||||
use crate::map::osm::OSMBaselayer;
|
use crate::map::osm::OSMBaselayer;
|
||||||
|
use crate::map::render::MapRender;
|
||||||
use crate::map::tiles::{LayerManager, LayerSource, Tile};
|
use crate::map::tiles::{LayerManager, LayerSource, Tile};
|
||||||
|
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub layer_manager: LayerManager,
|
pub layer_manager: LayerManager,
|
||||||
program: glow::Program,
|
pub lat: f64,
|
||||||
vertex_array: glow::VertexArray
|
pub long: f64,
|
||||||
|
pub zoom: f64,
|
||||||
|
pub render: Arc<Mutex<MapRender>>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)] // we need unsafe code to use glow
|
|
||||||
impl Map {
|
impl Map {
|
||||||
pub fn new(layer_manager: LayerManager, gl: &glow::Context) -> Option<Self> {
|
pub fn new<'a>(layer_manager: LayerManager, cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
|
||||||
use glow::HasContext as _;
|
let gl = cc.gl.as_ref().unwrap();
|
||||||
|
|
||||||
let shader_version = egui_glow::ShaderVersion::get(gl);
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
let program = gl.create_program().expect("Cannot create program");
|
|
||||||
|
|
||||||
if !shader_version.is_new_shader_interface() {
|
|
||||||
warn!(
|
|
||||||
"Custom 3D painting hasn't been ported to {:?}",
|
|
||||||
shader_version
|
|
||||||
);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (vertex_shader_source, fragment_shader_source) = (
|
|
||||||
r#"
|
|
||||||
const vec2 verts[6] = vec2[6](
|
|
||||||
vec2(-1.0, 1.0),
|
|
||||||
vec2(1.0, 1.0),
|
|
||||||
vec2(-1.0, -1.0),
|
|
||||||
|
|
||||||
vec2(-1.0, -1.0),
|
|
||||||
vec2(1.0, 1.0),
|
|
||||||
vec2(1.0, -1.0)
|
|
||||||
);
|
|
||||||
const vec4 colors[6] = vec4[6](
|
|
||||||
vec4(1.0, 0.0, 0.0, 1.0),
|
|
||||||
vec4(0.0, 1.0, 0.0, 1.0),
|
|
||||||
vec4(0.0, 0.0, 1.0, 1.0),
|
|
||||||
vec4(1.0, 0.0, 1.0, 1.0),
|
|
||||||
vec4(1.0, 0.0, 0.0, 1.0),
|
|
||||||
vec4(0.0, 1.0, 0.0, 1.0)
|
|
||||||
);
|
|
||||||
out vec4 v_color;
|
|
||||||
uniform float u_angle;
|
|
||||||
void main() {
|
|
||||||
v_color = colors[gl_VertexID];
|
|
||||||
gl_Position = vec4(verts[gl_VertexID], 0.0, 1.0);
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
r#"
|
|
||||||
precision mediump float;
|
|
||||||
in vec4 v_color;
|
|
||||||
out vec4 out_color;
|
|
||||||
void main() {
|
|
||||||
out_color = v_color;
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
);
|
|
||||||
|
|
||||||
let shader_sources = [
|
|
||||||
(glow::VERTEX_SHADER, vertex_shader_source),
|
|
||||||
(glow::FRAGMENT_SHADER, fragment_shader_source),
|
|
||||||
];
|
|
||||||
|
|
||||||
let shaders: Vec<_> = shader_sources
|
|
||||||
.iter()
|
|
||||||
.map(|(shader_type, shader_source)| {
|
|
||||||
let shader = gl
|
|
||||||
.create_shader(*shader_type)
|
|
||||||
.expect("Cannot create shader");
|
|
||||||
gl.shader_source(
|
|
||||||
shader,
|
|
||||||
&format!(
|
|
||||||
"{}\n{}",
|
|
||||||
shader_version.version_declaration(),
|
|
||||||
shader_source
|
|
||||||
),
|
|
||||||
);
|
|
||||||
gl.compile_shader(shader);
|
|
||||||
assert!(
|
|
||||||
gl.get_shader_compile_status(shader),
|
|
||||||
"Failed to compile custom_3d_glow {shader_type}: {}",
|
|
||||||
gl.get_shader_info_log(shader)
|
|
||||||
);
|
|
||||||
|
|
||||||
gl.attach_shader(program, shader);
|
|
||||||
shader
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
gl.link_program(program);
|
|
||||||
assert!(
|
|
||||||
gl.get_program_link_status(program),
|
|
||||||
"{}",
|
|
||||||
gl.get_program_info_log(program)
|
|
||||||
);
|
|
||||||
|
|
||||||
for shader in shaders {
|
|
||||||
gl.detach_shader(program, shader);
|
|
||||||
gl.delete_shader(shader);
|
|
||||||
}
|
|
||||||
|
|
||||||
let vertex_array = gl
|
|
||||||
.create_vertex_array()
|
|
||||||
.expect("Cannot create vertex array");
|
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
program,
|
layer_manager: layer_manager,
|
||||||
vertex_array,
|
lat: 35.227085,
|
||||||
layer_manager
|
long: -80.843124,
|
||||||
|
zoom: 12.0,
|
||||||
|
render: Arc::new(Mutex::new(MapRender::new(gl)?))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
pub fn process_input(&mut self, ctx: &egui::Context, ui: &mut Ui, rect: Rect, response: egui::Response) {
|
||||||
|
let xy_delta = response.drag_motion();
|
||||||
|
let z_delta = if ui.rect_contains_pointer(rect) {
|
||||||
|
ctx.input(|i| {
|
||||||
|
let mut yd_sum = 0.0;
|
||||||
|
for event in &i.events {
|
||||||
|
yd_sum += match event {
|
||||||
|
Event::MouseWheel { unit, delta, .. } => {
|
||||||
|
match unit {
|
||||||
|
// TODO: scaling of this
|
||||||
|
MouseWheelUnit::Point => { delta.y },
|
||||||
|
MouseWheelUnit::Line => { delta.y },
|
||||||
|
MouseWheelUnit::Page => { delta.y },
|
||||||
}
|
}
|
||||||
|
},
|
||||||
pub fn destroy(&self, gl: &glow::Context) {
|
_ => 0.0
|
||||||
use glow::HasContext as _;
|
|
||||||
unsafe {
|
|
||||||
gl.delete_program(self.program);
|
|
||||||
gl.delete_vertex_array(self.vertex_array);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
yd_sum
|
||||||
|
})
|
||||||
|
} else { 0.0 };
|
||||||
|
|
||||||
pub fn update_view_data(&mut self, ctx: &egui::Context) {
|
self.lat += xy_delta.y as f64 * 0.001;
|
||||||
// test: download 0/0/0 if not already there
|
self.long += xy_delta.x as f64 * 0.001;
|
||||||
|
self.zoom += z_delta as f64 * 0.01;
|
||||||
|
if self.zoom < 3.0 { self.zoom = 3.0 };
|
||||||
|
if self.zoom > 19.0 { self.zoom = 19.0 };
|
||||||
|
|
||||||
let tile = self.layer_manager.tiles.entry((0, 0, 0, OSMBaselayer::SOURCE_ID))
|
if self.long < -180.0 { self.long = -180.0 };
|
||||||
|
if self.long > 180.0 { self.lat = 180.0 };
|
||||||
|
|
||||||
|
if self.lat < -85.051129 { self.lat = -85.051129 };
|
||||||
|
if self.lat > 85.051129 { self.lat = 85.051129 };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_tiles(&mut self, ctx: &egui::Context, viewport_width: usize, viewport_height: usize) -> Tileset {
|
||||||
|
// lat, long is top left
|
||||||
|
// tiles are rendered at 256x256. how many can we fit?
|
||||||
|
let tiles_x = viewport_width / 256;
|
||||||
|
let tiles_y = viewport_height / 256;
|
||||||
|
|
||||||
|
let mut tiles = vec![];
|
||||||
|
|
||||||
|
|
||||||
|
// determine start/end tiles
|
||||||
|
let x = self.long.to_radians();
|
||||||
|
let y = self.lat.to_radians().tan().asinh();
|
||||||
|
|
||||||
|
let x = (1.0 + x/std::f64::consts::PI) / 2.0;
|
||||||
|
let y= (1.0 - y/std::f64::consts::PI) / 2.0;
|
||||||
|
|
||||||
|
let z_clamped = self.zoom.floor();
|
||||||
|
let tiles_across = 2usize.pow(z_clamped as u32);
|
||||||
|
|
||||||
|
let x_coordinate = x * tiles_across as f64;
|
||||||
|
let y_coordinate = y * tiles_across as f64;
|
||||||
|
|
||||||
|
let x_tile_frac = x_coordinate.fract() * 256.0;
|
||||||
|
let y_tile_frac = y_coordinate.fract() * 256.0;
|
||||||
|
|
||||||
|
|
||||||
|
let tilex = x_coordinate.floor() as usize;
|
||||||
|
let tiley = y_coordinate.floor() as usize;
|
||||||
|
|
||||||
|
let xrange = tilex..(tilex + tiles_x);
|
||||||
|
let yrange = tiley..(tiley + tiles_y);
|
||||||
|
|
||||||
|
|
||||||
|
// for each tile, determine pixel coordinates, then offset by the tile fractionals
|
||||||
|
for x in 0..tiles_x {
|
||||||
|
let tile_x = tilex + x;
|
||||||
|
for y in 0..tiles_y {
|
||||||
|
let tile_y = tiley + y;
|
||||||
|
let start_pixel_x = x as isize * 256 - x_tile_frac as isize;
|
||||||
|
let start_pixel_y = y as isize * 256 - y_tile_frac as isize;
|
||||||
|
let end_pixel_x = start_pixel_x + 256 - x_tile_frac as isize;
|
||||||
|
let end_pixel_y = start_pixel_y + 256 -y_tile_frac as isize;
|
||||||
|
|
||||||
|
let startx_zerotoone = start_pixel_x as f64 / viewport_width as f64;
|
||||||
|
let starty_zerotoone = start_pixel_y as f64 / viewport_height as f64;
|
||||||
|
let endx_zerotoone = end_pixel_x as f64 / viewport_width as f64;
|
||||||
|
let endy_zerotoone = end_pixel_y as f64 / viewport_height as f64;
|
||||||
|
|
||||||
|
let startx_opengl = startx_zerotoone * 2.0 - 1.0;
|
||||||
|
let starty_opengl = starty_zerotoone * 2.0 - 1.0;
|
||||||
|
let endx_opengl = endx_zerotoone * 2.0 - 1.0;
|
||||||
|
let endy_opengl = endy_zerotoone * 2.0 - 1.0;
|
||||||
|
|
||||||
|
// download the tile
|
||||||
|
let tile = self.layer_manager.tiles.entry((z_clamped as usize, tile_x, tile_y, OSMBaselayer::SOURCE_ID))
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
|
debug!("fetching {}/{}/{}", z_clamped, tile_x, tile_y);
|
||||||
let (sender, promise) = Promise::new();
|
let (sender, promise) = Promise::new();
|
||||||
let request = ehttp::Request::get("https://tile.openstreetmap.org/0/0/0.png");
|
let request = ehttp::Request::get(format!("https://tile.openstreetmap.org/{}/{tile_x}/{tile_y}.png", z_clamped as usize));
|
||||||
let ctx = ctx.clone();
|
let ctx = ctx.clone();
|
||||||
ehttp::fetch(request, move | response | {
|
ehttp::fetch(request, move | response | {
|
||||||
sender.send(match response {
|
sender.send(match response {
|
||||||
|
@ -159,18 +146,65 @@ impl Map {
|
||||||
promise
|
promise
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tiles.push(TilesetTile {
|
||||||
|
sp_x: start_pixel_x as f32,
|
||||||
|
sp_xclip: startx_opengl as f32,
|
||||||
|
sp_y: start_pixel_y as f32,
|
||||||
|
sp_yclip: starty_opengl as f32,
|
||||||
|
ep_x: end_pixel_x as f32,
|
||||||
|
ep_xclip: endy_opengl as f32,
|
||||||
|
ep_y: end_pixel_y as f32,
|
||||||
|
ep_yclip: endy_opengl as f32,
|
||||||
|
tile: match tile.promise.ready() {
|
||||||
|
None => None,
|
||||||
|
Some(r) => match r {
|
||||||
|
Ok(i) => Some(i.clone()),
|
||||||
|
Err(_) => None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paint(&self, gl: &glow::Context, angle: f32) {
|
Tileset {
|
||||||
use glow::HasContext as _;
|
tiles: Arc::new(Mutex::new(tiles)),
|
||||||
unsafe {
|
}
|
||||||
gl.use_program(Some(self.program));
|
}
|
||||||
gl.uniform_1_f32(
|
|
||||||
gl.get_uniform_location(self.program, "u_angle").as_ref(),
|
pub fn custom_painting(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {
|
||||||
angle,
|
let (rect, response) = ui.allocate_exact_size(
|
||||||
|
ui.available_size(),
|
||||||
|
Sense::all()
|
||||||
);
|
);
|
||||||
gl.bind_vertex_array(Some(self.vertex_array));
|
self.process_input(ctx, ui, rect, response);
|
||||||
gl.draw_arrays(glow::TRIANGLES, 0, 6);
|
let tileset = self.load_tiles(ctx, rect.width() as usize, rect.height() as usize);
|
||||||
|
let render = self.render.clone();
|
||||||
|
let cb = egui_glow::CallbackFn::new(move |_info, painter| {
|
||||||
|
let tileset = tileset.clone();
|
||||||
|
render.lock().unwrap().paint(painter.gl(), tileset);
|
||||||
|
});
|
||||||
|
ui.painter().add(egui::PaintCallback {
|
||||||
|
rect,
|
||||||
|
callback: Arc::new(cb)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Tileset {
|
||||||
|
tiles: Arc<Mutex<Vec<TilesetTile>>>
|
||||||
}
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct TilesetTile {
|
||||||
|
sp_x: f32,
|
||||||
|
sp_xclip: f32,
|
||||||
|
sp_y: f32,
|
||||||
|
sp_yclip: f32,
|
||||||
|
ep_x: f32,
|
||||||
|
ep_xclip: f32,
|
||||||
|
ep_y: f32,
|
||||||
|
ep_yclip: f32,
|
||||||
|
tile: Option<DynamicImage>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
153
crates/client/src/map/render.rs
Normal file
153
crates/client/src/map/render.rs
Normal file
|
@ -0,0 +1,153 @@
|
||||||
|
use tracing::{debug, warn};
|
||||||
|
use crate::map::Tileset;
|
||||||
|
|
||||||
|
pub struct MapRender {
|
||||||
|
program: glow::Program,
|
||||||
|
vertex_buffer: glow::Buffer,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
impl MapRender {
|
||||||
|
pub fn new(gl: &glow::Context) -> Option<Self> {
|
||||||
|
use glow::HasContext as _;
|
||||||
|
|
||||||
|
let shader_version = egui_glow::ShaderVersion::get(gl);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let program = gl.create_program().expect("Cannot create program");
|
||||||
|
|
||||||
|
if !shader_version.is_new_shader_interface() {
|
||||||
|
warn!(
|
||||||
|
"Custom 3D painting hasn't been ported to {:?}",
|
||||||
|
shader_version
|
||||||
|
);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (vertex_shader_source, fragment_shader_source) = (
|
||||||
|
r#"
|
||||||
|
layout(location = 0) in vec2 pos;
|
||||||
|
const vec4 colors[6] = vec4[6](
|
||||||
|
vec4(1.0, 0.0, 0.0, 1.0),
|
||||||
|
vec4(0.0, 1.0, 0.0, 1.0),
|
||||||
|
vec4(0.0, 0.0, 1.0, 1.0),
|
||||||
|
vec4(1.0, 0.0, 0.0, 1.0),
|
||||||
|
vec4(0.0, 1.0, 0.0, 1.0),
|
||||||
|
vec4(0.0, 0.0, 1.0, 1.0)
|
||||||
|
);
|
||||||
|
out vec4 v_color;
|
||||||
|
void main() {
|
||||||
|
v_color = colors[gl_VertexID];
|
||||||
|
gl_Position = vec4(pos, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
precision mediump float;
|
||||||
|
in vec4 v_color;
|
||||||
|
out vec4 out_color;
|
||||||
|
void main() {
|
||||||
|
out_color = v_color;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let shader_sources = [
|
||||||
|
(glow::VERTEX_SHADER, vertex_shader_source),
|
||||||
|
(glow::FRAGMENT_SHADER, fragment_shader_source),
|
||||||
|
];
|
||||||
|
|
||||||
|
let shaders: Vec<_> = shader_sources
|
||||||
|
.iter()
|
||||||
|
.map(|(shader_type, shader_source)| {
|
||||||
|
let shader = gl
|
||||||
|
.create_shader(*shader_type)
|
||||||
|
.expect("Cannot create shader");
|
||||||
|
gl.shader_source(
|
||||||
|
shader,
|
||||||
|
&format!(
|
||||||
|
"{}\n{}",
|
||||||
|
shader_version.version_declaration(),
|
||||||
|
shader_source
|
||||||
|
),
|
||||||
|
);
|
||||||
|
gl.compile_shader(shader);
|
||||||
|
assert!(
|
||||||
|
gl.get_shader_compile_status(shader),
|
||||||
|
"Failed to compile custom_3d_glow {shader_type} ({} {}): {}",
|
||||||
|
glow::VERTEX_SHADER,
|
||||||
|
glow::FRAGMENT_SHADER,
|
||||||
|
gl.get_shader_info_log(shader)
|
||||||
|
);
|
||||||
|
|
||||||
|
gl.attach_shader(program, shader);
|
||||||
|
shader
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
gl.link_program(program);
|
||||||
|
assert!(
|
||||||
|
gl.get_program_link_status(program),
|
||||||
|
"{}",
|
||||||
|
gl.get_program_info_log(program)
|
||||||
|
);
|
||||||
|
|
||||||
|
for shader in shaders {
|
||||||
|
gl.detach_shader(program, shader);
|
||||||
|
gl.delete_shader(shader);
|
||||||
|
}
|
||||||
|
|
||||||
|
let vertex_buffer = gl.create_buffer().expect("Cannot create vertex buffer");
|
||||||
|
gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, (2 * size_of::<f32>()) as i32, 0);
|
||||||
|
gl.enable_vertex_attrib_array(0);
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
program,
|
||||||
|
vertex_buffer
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn destroy(&self, gl: &glow::Context) {
|
||||||
|
use glow::HasContext as _;
|
||||||
|
unsafe {
|
||||||
|
gl.delete_program(self.program);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn paint(&self, gl: &glow::Context, tileset: Tileset) {
|
||||||
|
use glow::HasContext as _;
|
||||||
|
unsafe {
|
||||||
|
gl.use_program(Some(self.program));
|
||||||
|
|
||||||
|
gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer));
|
||||||
|
|
||||||
|
let tiles = tileset.tiles.lock().unwrap();
|
||||||
|
for tile in tiles.iter() {
|
||||||
|
let mut data = vec![];
|
||||||
|
// A-B
|
||||||
|
// | |
|
||||||
|
// C-D
|
||||||
|
// ABC
|
||||||
|
// BDC
|
||||||
|
// a: (sp_xclip, sp_yclip)
|
||||||
|
// b: (ep_xclip, sp_yclip)
|
||||||
|
// c: (sp_xclip, ep_yclip)
|
||||||
|
// d: (ep_xclip, ep_yclip)
|
||||||
|
// sp_xclip sp_yclip ep_xclip sp_yclip sp_xclip ep_yclip // ABC
|
||||||
|
// ep_xclip sp_yclip ep_xclip ep_yclip sp_xclip ep_yclip // BDC
|
||||||
|
debug!("A {:?} - B {:?}", (tile.sp_xclip, tile.sp_yclip), (tile.ep_xclip, tile.sp_yclip));
|
||||||
|
debug!("C {:?} - D {:?}", (tile.sp_xclip, tile.ep_yclip), (tile.ep_xclip, tile.ep_yclip));
|
||||||
|
for v in [tile.sp_xclip, tile.sp_yclip, tile.ep_xclip, tile.sp_yclip, tile.sp_xclip, tile.ep_yclip] {
|
||||||
|
data.extend(v.to_be_bytes());
|
||||||
|
}
|
||||||
|
for v in [tile.ep_xclip, tile.sp_yclip, tile.ep_xclip, tile.ep_yclip, tile.sp_xclip, tile.ep_yclip] {
|
||||||
|
data.extend(v.to_be_bytes());
|
||||||
|
}
|
||||||
|
gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, &data, glow::DYNAMIC_DRAW);
|
||||||
|
gl.draw_arrays(glow::TRIANGLES, 0, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue