@@ -4,31 +4,31 @@ use nalgebra_glm as glm;
44#[ derive( Debug , Clone ) ]
55pub struct Screen {
66 pub center : Point ,
7- pub width : u32 ,
8- pub height : u32 ,
9- tile_size : u32 ,
7+ pub width : f32 ,
8+ pub height : f32 ,
9+ tile_size : f32 ,
1010}
1111
1212impl Screen {
13- pub fn new ( center : Point , width : u32 , height : u32 , tile_size : u32 , hidpi_factor : f64 ) -> Self {
13+ pub fn new ( center : Point , width : f32 , height : f32 , tile_size : f32 , hidpi_factor : f32 ) -> Self {
1414 Self {
1515 center,
1616 width,
1717 height,
18- tile_size : ( tile_size as f64 * hidpi_factor) as u32 ,
18+ tile_size : tile_size * hidpi_factor,
1919 }
2020 }
2121
22- pub fn tile_size ( & self ) -> u32 {
22+ pub fn tile_size ( & self ) -> f32 {
2323 self . tile_size
2424 }
2525
2626 pub fn get_tile_boundaries_for_zoom_level ( & self , z : f32 , scale : u32 ) -> TileField {
2727 let z = z. min ( 14.0 ) ;
2828 let px_to_world =
29- self . width as f32 / self . tile_size ( ) as f32 / 2.0 / 2f32 . powi ( z as i32 ) / scale as f32 ;
29+ self . width as f32 / self . tile_size ( ) / 2.0 / 2f32 . powi ( z as i32 ) / scale as f32 ;
3030 let py_to_world =
31- self . height as f32 / self . tile_size ( ) as f32 / 2.0 / 2f32 . powi ( z as i32 ) / scale as f32 ;
31+ self . height as f32 / self . tile_size ( ) / 2.0 / 2f32 . powi ( z as i32 ) / scale as f32 ;
3232
3333 let top_left: TileId =
3434 world_to_tile_space ( & ( self . center - vector ( px_to_world, py_to_world) ) , z as u32 ) . into ( ) ;
@@ -37,18 +37,64 @@ impl Screen {
3737 TileField :: new ( top_left, bottom_right + TileId :: new ( z as u32 , 1 , 0 ) )
3838 }
3939
40- pub fn tile_to_global_space ( & self , z : f32 , coordinate : & TileId ) -> glm:: TMat4 < f32 > {
40+ pub fn tile_to_screen ( & self , z : f32 , coordinate : & TileId ) -> glm:: TMat4 < f32 > {
4141 let zoom = 1.0 / 2f32 . powi ( coordinate. z as i32 ) ;
4242 let zoom = glm:: scaling ( & glm:: vec3 ( zoom, zoom, 1.0 ) ) ;
4343 let pos = glm:: translation ( & glm:: vec3 ( coordinate. x as f32 , coordinate. y as f32 , 0.0 ) ) ;
4444 self . global_to_screen ( z) * zoom * pos
4545 }
4646
4747 pub fn global_to_screen ( & self , z : f32 ) -> glm:: TMat4 < f32 > {
48- let zoom_x = 2.0f32 . powf ( z) / ( self . width as f32 / 2.0 ) * self . tile_size ( ) as f32 ;
49- let zoom_y = 2.0f32 . powf ( z) / ( self . height as f32 / 2.0 ) * self . tile_size ( ) as f32 ;
48+ let zoom_x = 2.0f32 . powf ( z) / ( self . width as f32 / 2.0 ) * self . tile_size ( ) ;
49+ let zoom_y = 2.0f32 . powf ( z) / ( self . height as f32 / 2.0 ) * self . tile_size ( ) ;
5050 let zoom = glm:: scaling ( & glm:: vec3 ( zoom_x, zoom_y, 1.0 ) ) ;
5151 let position = glm:: translation ( & glm:: vec3 ( -self . center . x , -self . center . y , 0.0 ) ) ;
5252 zoom * position
5353 }
54+
55+ pub fn screen_to_gpu ( & self ) -> glm:: TMat4 < f32 > {
56+ let scale = glm:: vec3 ( 1.0 , 1.0 , 1.0 ) . component_div ( & glm:: vec3 (
57+ self . width / 2.0 ,
58+ self . height / 2.0 ,
59+ 1.0 ,
60+ ) ) ;
61+ glm:: scaling ( & scale)
62+ }
63+
64+ pub fn screen_to_world ( & self , z : f32 ) -> glm:: TMat4 < f32 > {
65+ // TODO: make logical width instead of phyiscal.
66+ glm:: translation2d ( & glm:: vec2 ( dbg ! ( self . center. x) , self . center . y ) )
67+ * glm:: scaling2d ( & glm:: vec2 (
68+ 1.0 / 2.0 * f32:: powf ( 2.0 , -z) / self . tile_size ( ) ,
69+ 1.0 / 2.0 * f32:: powf ( 2.0 , -z) / self . tile_size ( ) ,
70+ ) ) ;
71+
72+ let matrix = self . global_to_screen ( z) ;
73+ let screen_to_global = nalgebra_glm:: inverse ( & matrix) ;
74+
75+ let pixel_to_screen = glm:: translation ( & glm:: vec3 ( -1.0 , -1.0 , 0.0 ) )
76+ * glm:: scaling ( & glm:: vec3 (
77+ 1.0 / ( self . width / 2.0 ) ,
78+ 1.0 / ( self . height / 2.0 ) ,
79+ 1.0 ,
80+ ) ) ;
81+
82+ screen_to_global * pixel_to_screen
83+ }
84+
85+ pub fn global_to_tile_space ( & self , z : f32 , coordinate : & TileId ) -> glm:: TMat4 < f32 > {
86+ self . tile_to_screen ( z, coordinate) . try_inverse ( ) . unwrap ( )
87+ }
88+
89+ // pub fn screen_to_global(&self, z: f32) -> glm::TMat4<f32> {
90+ // // self.global_to_screen(z).try_inverse().unwrap()
91+ // let zoom_x = 2.0f32.powf(z) / (self.width / 2.0) * self.tile_size() * 2.0;
92+ // let zoom_y = 2.0f32.powf(z) / (self.height / 2.0) * self.tile_size() * 2.0;
93+ // let zoom = glm::scaling(&glm::vec3(zoom_x, zoom_y, 1.0));
94+ // (zoom).try_inverse().unwrap()
95+
96+ // glm::
97+ // }
98+
99+ // screen (px distorted) -> screen (square normalized coordinates) -> world (square coordinates) -> mercator lat lon
54100}
0 commit comments