A simple map viewer

ortho_tile_layer.rs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. use buffer::{Buffer, DrawMode};
  2. use cgmath::Transform;
  3. use context::Context;
  4. use coord::{LatLonRad, ScreenCoord, View};
  5. use orthografic_view::OrthograficView;
  6. use program::Program;
  7. use std::ffi::CStr;
  8. use tile_atlas::{TileAtlas, VisibleTilesProvider};
  9. use tile_cache::TileCache;
  10. use tile_source::TileSource;
  11. use vertex_attrib::VertexAttribParams;
  12. #[derive(Debug)]
  13. pub struct OrthoTileLayer {
  14. program: Program,
  15. buffer: Buffer,
  16. }
  17. #[derive(Copy, Clone, Debug)]
  18. pub struct LatScreenEllipse {
  19. pub center: ScreenCoord,
  20. pub radius_x: f64,
  21. pub radius_y: f64,
  22. /// longitude angle in radians at center + radius_y
  23. pub ref_angle: f64,
  24. }
  25. impl LatScreenEllipse {
  26. fn new(view_center: LatLonRad, viewport_size: (u32, u32), sphere_radius: f64, lat: f64) -> Self {
  27. LatScreenEllipse {
  28. center: ScreenCoord {
  29. x: f64::from(viewport_size.0) * 0.5,
  30. y: f64::from(viewport_size.1) * 0.5 * (lat - view_center.lat).sin() * sphere_radius,
  31. },
  32. radius_x: lat.cos() * sphere_radius,
  33. radius_y: lat.cos() * -view_center.lat.sin() * sphere_radius,
  34. ref_angle: view_center.lon,
  35. }
  36. }
  37. }
  38. impl OrthoTileLayer {
  39. pub fn new(
  40. cx: &mut Context,
  41. atlas: &TileAtlas,
  42. ) -> OrthoTileLayer
  43. {
  44. let buffer = Buffer::new(cx, &[], 0);
  45. cx.bind_buffer(buffer.id());
  46. let mut program = Program::new(
  47. cx,
  48. include_bytes!("../shader/ortho_tile.vert"),
  49. include_bytes!("../shader/ortho_tile.frag"),
  50. ).unwrap();
  51. program.add_texture(cx, atlas.texture(), CStr::from_bytes_with_nul(b"tex_map\0").unwrap());
  52. program.add_attribute(
  53. cx,
  54. CStr::from_bytes_with_nul(b"position\0").unwrap(),
  55. &VertexAttribParams::new(3, 9, 0)
  56. );
  57. program.add_attribute(
  58. cx,
  59. CStr::from_bytes_with_nul(b"tex_coord\0").unwrap(),
  60. &VertexAttribParams::new(2, 9, 3)
  61. );
  62. program.add_attribute(
  63. cx,
  64. CStr::from_bytes_with_nul(b"tex_minmax\0").unwrap(),
  65. &VertexAttribParams::new(4, 9, 5)
  66. );
  67. OrthoTileLayer {
  68. program,
  69. buffer,
  70. }
  71. }
  72. // Has to be called once before one or multiple calls to `draw`.
  73. pub fn prepare_draw(&mut self, cx: &mut Context, atlas: &TileAtlas) {
  74. self.program.enable_vertex_attribs(cx);
  75. self.program.set_vertex_attribs(cx, &self.buffer);
  76. cx.set_active_texture_unit(atlas.texture().unit());
  77. }
  78. pub fn draw(
  79. &mut self,
  80. cx: &mut Context,
  81. ortho: &OrthograficView,
  82. source: &TileSource,
  83. cache: &mut TileCache,
  84. tile_atlas: &mut TileAtlas,
  85. ) -> Result<usize, usize> {
  86. //TODO Add distance function to TileCache that takes topology of the sphere into account.
  87. cache.set_view_location(View {
  88. source_id: source.id(),
  89. zoom: ortho.tile_zoom(),
  90. center: ortho.center,
  91. });
  92. let transform = ortho.transformation_matrix();
  93. let visible_tiles = ortho.visible_tiles();
  94. let mut remainder = visible_tiles.as_slice();
  95. let mut num_draws = 0;
  96. let mut max_tiles_to_use = cache.max_tiles();
  97. loop {
  98. let (textured_visible_tiles, remainder_opt, used_tiles) = {
  99. tile_atlas.textured_visible_tiles(
  100. cx,
  101. remainder,
  102. max_tiles_to_use,
  103. source,
  104. cache,
  105. )
  106. };
  107. max_tiles_to_use = max_tiles_to_use.saturating_sub(used_tiles);
  108. // A low guess for the capacity
  109. let mut vertex_data = Vec::with_capacity(textured_visible_tiles.len() * 9 * 16);
  110. for tvt in &textured_visible_tiles {
  111. let minmax = [
  112. tvt.tex_minmax.x1 as f32,
  113. tvt.tex_minmax.y1 as f32,
  114. tvt.tex_minmax.x2 as f32,
  115. tvt.tex_minmax.y2 as f32,
  116. ];
  117. let subdivision = 6u32.saturating_sub(tvt.tile_coord.zoom).max(2);
  118. for (tc, sub_tile) in tvt.tile_coord.children_iter(subdivision) {
  119. let ll_nw = tc.latlon_rad_north_west();
  120. let ll_se = tc.latlon_rad_south_east();
  121. let ll_ne = LatLonRad::new(ll_nw.lat, ll_se.lon);
  122. let ll_sw = LatLonRad::new(ll_se.lat, ll_nw.lon);
  123. let p1 = transform.transform_point(ll_nw.to_sphere_point3());
  124. let p2 = transform.transform_point(ll_ne.to_sphere_point3());
  125. let p3 = transform.transform_point(ll_se.to_sphere_point3());
  126. let p4 = transform.transform_point(ll_sw.to_sphere_point3());
  127. // Discard tiles/subtiles that are facing backwards
  128. if (p1.z + p3.z) * 0.5 > 0.0 {
  129. continue;
  130. }
  131. let texrect = tvt.tex_rect.subdivide(&sub_tile);
  132. let p1 = [p1.x as f32, p1.y as f32, p1.z as f32, texrect.x1 as f32, texrect.y1 as f32];
  133. let p2 = [p2.x as f32, p2.y as f32, p2.z as f32, texrect.x2 as f32, texrect.y1 as f32];
  134. let p3 = [p3.x as f32, p3.y as f32, p3.z as f32, texrect.x2 as f32, texrect.y2 as f32];
  135. let p4 = [p4.x as f32, p4.y as f32, p4.z as f32, texrect.x1 as f32, texrect.y2 as f32];
  136. vertex_data.extend(&p1);
  137. vertex_data.extend(&minmax);
  138. vertex_data.extend(&p2);
  139. vertex_data.extend(&minmax);
  140. vertex_data.extend(&p3);
  141. vertex_data.extend(&minmax);
  142. vertex_data.extend(&p1);
  143. vertex_data.extend(&minmax);
  144. vertex_data.extend(&p3);
  145. vertex_data.extend(&minmax);
  146. vertex_data.extend(&p4);
  147. vertex_data.extend(&minmax);
  148. }
  149. }
  150. self.buffer.set_data(cx, &vertex_data, vertex_data.len() / 4);
  151. self.buffer.draw(cx, &self.program, DrawMode::Triangles);
  152. num_draws += 1;
  153. debug!("draw #{}: tvt.len() = {}, remainder = {:?}, max_tiles = {}",
  154. num_draws,
  155. textured_visible_tiles.len(),
  156. remainder_opt.map(|r| r.len()),
  157. max_tiles_to_use);
  158. if max_tiles_to_use == 0 {
  159. warn!("tile cache is too small for this view.");
  160. return Err(num_draws);
  161. }
  162. match remainder_opt {
  163. None => return Ok(num_draws),
  164. Some(new_remainder) => {
  165. if new_remainder.len() >= remainder.len() {
  166. warn!("failed to draw all tiles. number of remaining tiles did not decrease.");
  167. return Err(num_draws);
  168. } else {
  169. remainder = new_remainder;
  170. }
  171. },
  172. }
  173. }
  174. }
  175. }