move projection matrix into camera
authoralfadur
Sun, 24 Mar 2019 20:11:55 +0300
changeset 14723 5915a199cb81
parent 14722 16024046d458
child 14724 abc6aaf481c4
move projection matrix into camera
rust/lib-hedgewars-engine/src/render/camera.rs
rust/lib-hedgewars-engine/src/render/map.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/lib-hedgewars-engine/src/render/camera.rs	Sat Mar 23 03:44:11 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/render/camera.rs	Sun Mar 24 20:11:55 2019 +0300
@@ -34,4 +34,31 @@
             self.position.y + half_height,
         )
     }
+
+    pub fn projection(&self) -> [f32; 16] {
+        let viewport = self.viewport();
+        let left = viewport.left() as f32;
+        let width = viewport.width() as f32;
+        let height = viewport.height() as f32;
+        let top = viewport.top() as f32;
+
+        [
+            2f32 / width,
+            0f32,
+            0f32,
+            0f32,
+            0f32,
+            2f32 / -height,
+            0f32,
+            0f32,
+            0f32,
+            0f32,
+            0.5f32,
+            0f32,
+            -(2.0 * left + width) / width,
+            (2.0 * top + height) / height,
+            0.5f32,
+            1f32,
+        ]
+    }
 }
--- a/rust/lib-hedgewars-engine/src/render/map.rs	Sat Mar 23 03:44:11 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/render/map.rs	Sun Mar 24 20:11:55 2019 +0300
@@ -2,8 +2,11 @@
 use land2d::Land2D;
 use vec2d::Vec2D;
 
-use super::gl::{
-    Buffer, InputElement, InputFormat, InputLayout, Shader, Texture2D, VariableBinding,
+use super::{
+    camera::Camera,
+    gl::{
+        Buffer, InputElement, InputFormat, InputLayout, Shader, Texture2D, VariableBinding,
+    }
 };
 
 // TODO: temp
@@ -206,7 +209,8 @@
 
     pub fn update(&mut self, land: &Land2D<u32>, region: Rect) {}
 
-    pub fn render(&mut self, viewport: Rect) {
+    pub fn render(&mut self, camera: &Camera) {
+        let viewport = camera.viewport();
         self.tile_vertices.clear();
         self.tile_indices.clear();
         self.tile_draw_calls.clear();
@@ -270,34 +274,10 @@
             Some(&self.tile_index_buffer),
         );
 
-        let ortho = {
-            let l = viewport.left() as f32;
-            let w = viewport.width() as f32;
-            let h = viewport.height() as f32;
-            let t = viewport.top() as f32;
-
-            [
-                2f32 / w,
-                0f32,
-                0f32,
-                0f32,
-                0f32,
-                2f32 / -h,
-                0f32,
-                0f32,
-                0f32,
-                0f32,
-                0.5f32,
-                0f32,
-                -(2.0 * l + w) / w,
-                (2.0 * t + h) / h,
-                0.5f32,
-                1f32,
-            ]
-        };
+        let projection = camera.projection();
 
         self.tile_shader.bind();
-        self.tile_shader.set_matrix("Projection", ortho.as_ptr());
+        self.tile_shader.set_matrix("Projection", projection.as_ptr());
 
         let mut draw_offset = 0;
         for draw_call in &self.tile_draw_calls {
--- a/rust/lib-hedgewars-engine/src/world.rs	Sat Mar 23 03:44:11 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/world.rs	Sun Mar 24 20:11:55 2019 +0300
@@ -118,7 +118,7 @@
                 gl::Clear(gl::COLOR_BUFFER_BIT);
             }
 
-            renderer.render(self.camera.viewport());
+            renderer.render(&self.camera);
         }
     }