From: Brendan Hansen Date: Wed, 7 Jul 2021 20:18:41 +0000 (-0500) Subject: bugfixes with scissors X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a2882eed6f39620550d137a1503790e672675451;p=onyx.git bugfixes with scissors --- diff --git a/modules/immediate_mode/immediate_renderer.onyx b/modules/immediate_mode/immediate_renderer.onyx index 250ec88c..a8455f69 100644 --- a/modules/immediate_mode/immediate_renderer.onyx +++ b/modules/immediate_mode/immediate_renderer.onyx @@ -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, + }; + } } diff --git a/modules/immediate_mode/transform.onyx b/modules/immediate_mode/transform.onyx index 28b54ccb..56ed1a71 100644 --- a/modules/immediate_mode/transform.onyx +++ b/modules/immediate_mode/transform.onyx @@ -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 ];