bugfixes with scissors
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 7 Jul 2021 20:18:41 +0000 (15:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 7 Jul 2021 20:18:41 +0000 (15:18 -0500)
modules/immediate_mode/immediate_renderer.onyx
modules/immediate_mode/transform.onyx

index 250ec88c8c4b9798727f3f561d2633b1c5697ad9..a8455f690ff047fd4ea56dc083fd9844f68ad466 100644 (file)
@@ -304,19 +304,18 @@ Immediate_Renderer :: struct {
             gl.enable(gl.SCISSOR_TEST);
         }
 
-        world_matrix := ir->get_transform()
-                        |> transform_to_matrix();
+        transform := ir->get_transform();
 
-        new_x0 := world_matrix[0] * x + world_matrix[1] * y + world_matrix[12] * 1;
-        new_y0 := world_matrix[4] * x + world_matrix[5] * y + world_matrix[13] * 1;
-        new_x1 := world_matrix[0] * (x + w) + world_matrix[1] * (y + h) + world_matrix[12] * 1;
-        new_y1 := world_matrix[4] * (x + w) + world_matrix[5] * (y + h) + world_matrix[13] * 1;
+        new_x0 := x * transform.scale.x + transform.translation.x;
+        new_y0 := y * transform.scale.y + transform.translation.y;
+        new_x1 := (x + w) * transform.scale.x + transform.translation.x;
+        new_y1 := (y + h) * transform.scale.y + transform.translation.y;
         new_w  := new_x1 - new_x0;
         new_h  := new_y1 - new_y0;
 
         array.push(^scissor_stack, .{ new_x0, new_y0, new_w, new_h });
 
-        gl.scissor(~~new_x0, ~~(-new_y0 + new_h * 2), ~~new_w, ~~new_h);
+        gl.scissor(~~new_x0, window_height - ~~new_y0 - ~~new_h, ~~new_w, ~~new_h);
     }
 
     pop_scissor :: (use ir: ^Immediate_Renderer) {
@@ -400,6 +399,24 @@ Immediate_Renderer :: struct {
     get_transform :: (use ir: ^Immediate_Renderer) -> ^Transform {
         return array.get_ptr(^world_transform_stack, -1);
     }
+
+    to_screen_coordinates :: (use ir: ^Immediate_Renderer, use v: Vector2) -> Vector2 {
+        transform := ir->get_transform();
+
+        return .{
+            x * transform.scale.x + transform.translation.x,
+            y * transform.scale.y + transform.translation.y,
+        };
+    }
+
+    to_world_coordinates :: (use ir: ^Immediate_Renderer, use v: Vector2) -> Vector2 {
+        transform := ir->get_transform();
+
+        return .{
+            (x - transform.translation.x) / transform.scale.x,
+            (y - transform.translation.y) / transform.scale.y,
+        };
+    }
 }
 
 
index 28b54ccb7e3c53d0c1a3fdfb045a0a63c7a7724e..56ed1a71c7772cf1bf8d9134d8abcd4c792ef2db 100644 (file)
@@ -4,20 +4,23 @@ package immediate_mode
 Transform :: struct {
     translation: Vector2;
     scale:       Vector2;
-    rotation:    f32;    // In radians
+
+    // No rotation for right now. Not need for GUI stuff.
+    // rotation:    f32;    // In radians
 }
 
 transform_identity :: (use t: ^Transform) {
     translation = .{ 0, 0 };
     scale       = .{ 1, 1 };
-    rotation    = 0;
+
+    // rotation    = 0;
 }
 
 transform_to_matrix :: (use t: ^Transform) -> [16] f32 {
     math :: package core.math
 
-    st := math.sin(rotation);
-    ct := math.cos(rotation);
+    // st := math.sin(rotation);
+    // ct := math.cos(rotation);
 
     sx := scale.x;
     sy := scale.y;
@@ -26,8 +29,8 @@ transform_to_matrix :: (use t: ^Transform) -> [16] f32 {
     ty := translation.y;
 
     return f32.[
-        sx * ct, -sy * st, 0, 0,
-        sx * st,  sy * ct, 0, 0,
+        sx, 0, 0, 0,
+        0,  sy, 0, 0,
         0, 0, 1, 0,
         tx, ty, 0, 1
     ];