|
|
@@ -1,15 +1,9 @@
|
|
1
|
|
-use ::std::ffi::CStr;
|
|
2
|
|
-use buffer::{Buffer, DrawMode};
|
|
3
|
1
|
use context::Context;
|
|
4
|
|
-use coord::{MapCoord, ScreenCoord, View};
|
|
|
2
|
+use coord::{MapCoord, ScreenCoord};
|
|
5
|
3
|
use map_view::MapView;
|
|
6
|
4
|
use marker_layer::MarkerLayer;
|
|
7
|
|
-use program::Program;
|
|
8
|
|
-use texture::{Texture, TextureFormat};
|
|
9
|
|
-use tile_atlas::TileAtlas;
|
|
10
|
|
-use tile_cache::TileCache;
|
|
|
5
|
+use tile_layer::TileLayer;
|
|
11
|
6
|
use tile_source::TileSource;
|
|
12
|
|
-use vertex_attrib::VertexAttribParams;
|
|
13
|
7
|
|
|
14
|
8
|
|
|
15
|
9
|
const MIN_ZOOM_LEVEL: f64 = 0.0;
|
|
|
@@ -19,10 +13,7 @@ const MAX_ZOOM_LEVEL: f64 = 22.0;
|
|
19
|
13
|
pub struct MapViewGl {
|
|
20
|
14
|
map_view: MapView,
|
|
21
|
15
|
viewport_size: (u32, u32),
|
|
22
|
|
- tile_program: Program,
|
|
23
|
|
- tile_buffer: Buffer,
|
|
24
|
|
- tile_cache: TileCache,
|
|
25
|
|
- tile_atlas: TileAtlas,
|
|
|
16
|
+ tile_layer: TileLayer,
|
|
26
|
17
|
marker_layer: MarkerLayer,
|
|
27
|
18
|
last_draw_type: DrawType,
|
|
28
|
19
|
}
|
|
|
@@ -52,61 +43,10 @@ impl MapViewGl {
|
|
52
|
43
|
map_view.zoom = MIN_ZOOM_LEVEL;
|
|
53
|
44
|
}
|
|
54
|
45
|
|
|
55
|
|
- let tile_buffer = Buffer::new(cx, &[], 0);
|
|
56
|
|
- check_gl_errors!(cx);
|
|
57
|
|
- cx.bind_buffer(tile_buffer.id());
|
|
58
|
|
-
|
|
59
|
|
- let mut tile_program = Program::new(
|
|
60
|
|
- cx,
|
|
61
|
|
- include_bytes!("../shader/map.vert"),
|
|
62
|
|
- include_bytes!("../shader/map.frag"),
|
|
63
|
|
- ).unwrap();
|
|
64
|
|
- check_gl_errors!(cx);
|
|
65
|
|
-
|
|
66
|
|
- let atlas_size = {
|
|
67
|
|
- let default_size = 2048;
|
|
68
|
|
- let max_size = cx.max_texture_size() as u32;
|
|
69
|
|
- if default_size <= max_size {
|
|
70
|
|
- default_size
|
|
71
|
|
- } else {
|
|
72
|
|
- if tile_size * 3 > max_size {
|
|
73
|
|
- error!("maximal tile size ({}) is too small", max_size);
|
|
74
|
|
- }
|
|
75
|
|
-
|
|
76
|
|
- max_size
|
|
77
|
|
- }
|
|
78
|
|
- };
|
|
79
|
|
-
|
|
80
|
|
- let atlas_tex = Texture::empty(cx, atlas_size, atlas_size, TextureFormat::Rgb8);
|
|
81
|
|
- check_gl_errors!(cx);
|
|
82
|
|
-
|
|
83
|
|
- tile_program.add_texture(cx, &atlas_tex, CStr::from_bytes_with_nul(b"tex_map\0").unwrap());
|
|
84
|
|
- check_gl_errors!(cx);
|
|
85
|
|
-
|
|
86
|
|
- tile_program.add_attribute(
|
|
87
|
|
- cx,
|
|
88
|
|
- CStr::from_bytes_with_nul(b"position\0").unwrap(),
|
|
89
|
|
- &VertexAttribParams::new(2, 8, 0)
|
|
90
|
|
- );
|
|
91
|
|
- tile_program.add_attribute(
|
|
92
|
|
- cx,
|
|
93
|
|
- CStr::from_bytes_with_nul(b"tex_coord\0").unwrap(),
|
|
94
|
|
- &VertexAttribParams::new(2, 8, 2)
|
|
95
|
|
- );
|
|
96
|
|
- tile_program.add_attribute(
|
|
97
|
|
- cx,
|
|
98
|
|
- CStr::from_bytes_with_nul(b"tex_minmax\0").unwrap(),
|
|
99
|
|
- &VertexAttribParams::new(4, 8, 4)
|
|
100
|
|
- );
|
|
101
|
|
- check_gl_errors!(cx);
|
|
102
|
|
-
|
|
103
|
46
|
MapViewGl {
|
|
104
|
47
|
map_view,
|
|
105
|
48
|
viewport_size: initial_size,
|
|
106
|
|
- tile_program,
|
|
107
|
|
- tile_buffer,
|
|
108
|
|
- tile_cache: TileCache::new(move |_tile| update_func(), use_network),
|
|
109
|
|
- tile_atlas: TileAtlas::new(cx, atlas_tex, 256, use_async),
|
|
|
49
|
+ tile_layer: TileLayer::new(cx, move |_| update_func(), tile_size, use_network, use_async),
|
|
110
|
50
|
marker_layer: MarkerLayer::new(cx),
|
|
111
|
51
|
last_draw_type: DrawType::Null,
|
|
112
|
52
|
}
|
|
|
@@ -127,117 +67,18 @@ impl MapViewGl {
|
|
127
|
67
|
}
|
|
128
|
68
|
|
|
129
|
69
|
pub fn increase_atlas_size(&mut self, cx: &mut Context) -> Result<(), ()> {
|
|
130
|
|
- self.tile_atlas.double_texture_size(cx)
|
|
|
70
|
+ self.tile_layer.double_atlas_size(cx)
|
|
131
|
71
|
}
|
|
132
|
72
|
|
|
133
|
|
- fn draw_tiles(&mut self, cx: &mut Context, source: &TileSource, snap_to_pixel: bool) -> Result<usize, usize> {
|
|
|
73
|
+ fn draw_tiles(&mut self, cx: &mut Context, source: &TileSource, snap_to_pixel: bool)
|
|
|
74
|
+ -> Result<usize, usize>
|
|
|
75
|
+ {
|
|
134
|
76
|
if self.last_draw_type != DrawType::Tiles {
|
|
135
|
77
|
self.last_draw_type = DrawType::Tiles;
|
|
136
|
|
- self.tile_program.enable_vertex_attribs(cx);
|
|
137
|
|
- self.tile_program.set_vertex_attribs(cx, &self.tile_buffer);
|
|
138
|
|
- cx.set_active_texture_unit(self.tile_atlas.texture().unit());
|
|
|
78
|
+ self.tile_layer.prepare_draw(cx);
|
|
139
|
79
|
}
|
|
140
|
80
|
|
|
141
|
|
- self.tile_cache.set_view_location(View {
|
|
142
|
|
- source_id: source.id(),
|
|
143
|
|
- zoom: self.map_view.tile_zoom(),
|
|
144
|
|
- center: self.map_view.center,
|
|
145
|
|
- });
|
|
146
|
|
-
|
|
147
|
|
- let visible_tiles = self.map_view.visible_tiles(snap_to_pixel);
|
|
148
|
|
- let mut remainder = visible_tiles.as_slice();
|
|
149
|
|
- let mut num_draws = 0;
|
|
150
|
|
- let mut max_tiles_to_use = self.tile_cache.max_tiles();
|
|
151
|
|
-
|
|
152
|
|
- loop {
|
|
153
|
|
- let (textured_visible_tiles, remainder_opt, used_tiles) = {
|
|
154
|
|
- self.tile_atlas.textured_visible_tiles(
|
|
155
|
|
- cx,
|
|
156
|
|
- remainder,
|
|
157
|
|
- max_tiles_to_use,
|
|
158
|
|
- source,
|
|
159
|
|
- &mut self.tile_cache,
|
|
160
|
|
- )
|
|
161
|
|
- };
|
|
162
|
|
-
|
|
163
|
|
- max_tiles_to_use -= used_tiles;
|
|
164
|
|
-
|
|
165
|
|
- let mut vertex_data: Vec<f32> = Vec::with_capacity(textured_visible_tiles.len() * (6 * 8));
|
|
166
|
|
- let scale_x = 2.0 / f64::from(self.viewport_size.0);
|
|
167
|
|
- let scale_y = -2.0 / f64::from(self.viewport_size.1);
|
|
168
|
|
- for tvt in &textured_visible_tiles {
|
|
169
|
|
- let minmax = [
|
|
170
|
|
- tvt.tex_minmax.x1 as f32,
|
|
171
|
|
- tvt.tex_minmax.y1 as f32,
|
|
172
|
|
- tvt.tex_minmax.x2 as f32,
|
|
173
|
|
- tvt.tex_minmax.y2 as f32,
|
|
174
|
|
- ];
|
|
175
|
|
- let p1 = [
|
|
176
|
|
- (tvt.screen_rect.x * scale_x - 1.0) as f32,
|
|
177
|
|
- (tvt.screen_rect.y * scale_y + 1.0) as f32,
|
|
178
|
|
- tvt.tex_rect.x1 as f32,
|
|
179
|
|
- tvt.tex_rect.y1 as f32,
|
|
180
|
|
- ];
|
|
181
|
|
- let p2 = [
|
|
182
|
|
- (tvt.screen_rect.x * scale_x - 1.0) as f32,
|
|
183
|
|
- ((tvt.screen_rect.y + tvt.screen_rect.height) * scale_y + 1.0) as f32,
|
|
184
|
|
- tvt.tex_rect.x1 as f32,
|
|
185
|
|
- tvt.tex_rect.y2 as f32,
|
|
186
|
|
- ];
|
|
187
|
|
- let p3 = [
|
|
188
|
|
- ((tvt.screen_rect.x + tvt.screen_rect.width) * scale_x - 1.0) as f32,
|
|
189
|
|
- ((tvt.screen_rect.y + tvt.screen_rect.height) * scale_y + 1.0) as f32,
|
|
190
|
|
- tvt.tex_rect.x2 as f32,
|
|
191
|
|
- tvt.tex_rect.y2 as f32,
|
|
192
|
|
- ];
|
|
193
|
|
- let p4 = [
|
|
194
|
|
- ((tvt.screen_rect.x + tvt.screen_rect.width) * scale_x - 1.0) as f32,
|
|
195
|
|
- (tvt.screen_rect.y * scale_y + 1.0) as f32,
|
|
196
|
|
- tvt.tex_rect.x2 as f32,
|
|
197
|
|
- tvt.tex_rect.y1 as f32,
|
|
198
|
|
- ];
|
|
199
|
|
- vertex_data.extend(&p1);
|
|
200
|
|
- vertex_data.extend(&minmax);
|
|
201
|
|
- vertex_data.extend(&p2);
|
|
202
|
|
- vertex_data.extend(&minmax);
|
|
203
|
|
- vertex_data.extend(&p3);
|
|
204
|
|
- vertex_data.extend(&minmax);
|
|
205
|
|
- vertex_data.extend(&p1);
|
|
206
|
|
- vertex_data.extend(&minmax);
|
|
207
|
|
- vertex_data.extend(&p3);
|
|
208
|
|
- vertex_data.extend(&minmax);
|
|
209
|
|
- vertex_data.extend(&p4);
|
|
210
|
|
- vertex_data.extend(&minmax);
|
|
211
|
|
- }
|
|
212
|
|
-
|
|
213
|
|
- self.tile_buffer.set_data(cx, &vertex_data, vertex_data.len() / 4);
|
|
214
|
|
- self.tile_buffer.draw(cx, &self.tile_program, DrawMode::Triangles);
|
|
215
|
|
-
|
|
216
|
|
- num_draws += 1;
|
|
217
|
|
-
|
|
218
|
|
- debug!("draw #{}: tvt.len() = {}, remainder = {:?}, max_tiles = {}",
|
|
219
|
|
- num_draws,
|
|
220
|
|
- textured_visible_tiles.len(),
|
|
221
|
|
- remainder_opt.map(|r| r.len()),
|
|
222
|
|
- max_tiles_to_use);
|
|
223
|
|
-
|
|
224
|
|
- if max_tiles_to_use == 0 {
|
|
225
|
|
- warn!("tile cache is too small for this view.");
|
|
226
|
|
- return Err(num_draws);
|
|
227
|
|
- }
|
|
228
|
|
-
|
|
229
|
|
- match remainder_opt {
|
|
230
|
|
- None => return Ok(num_draws),
|
|
231
|
|
- Some(new_remainder) => {
|
|
232
|
|
- if new_remainder.len() >= remainder.len() {
|
|
233
|
|
- warn!("failed to draw all tiles. number of remaining tiles did not decrease.");
|
|
234
|
|
- return Err(num_draws);
|
|
235
|
|
- } else {
|
|
236
|
|
- remainder = new_remainder;
|
|
237
|
|
- }
|
|
238
|
|
- },
|
|
239
|
|
- }
|
|
240
|
|
- }
|
|
|
81
|
+ self.tile_layer.draw(cx, &self.map_view, source, self.viewport_size, snap_to_pixel)
|
|
241
|
82
|
}
|
|
242
|
83
|
|
|
243
|
84
|
fn draw_marker(&mut self, cx: &mut Context, snap_to_pixel: bool) {
|