From 08115a92449ca046d25799132b4608803bdc8547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Thu, 31 Mar 2022 14:51:27 +0200 Subject: [PATCH] Use viewport width and height in projection calcs (#52) --- orthographic/camera.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/orthographic/camera.lua b/orthographic/camera.lua index 34b3d8c..e1d9bfc 100644 --- a/orthographic/camera.lua +++ b/orthographic/camera.lua @@ -79,9 +79,13 @@ end -- setup a fixed aspect ratio projection that zooms in/out to fit the original viewport contents -- regardless of window size projectors[M.PROJECTOR.FIXED_AUTO] = function(camera_id, near_z, far_z, zoom) - local zoom_factor = math.min(WINDOW_WIDTH / DISPLAY_WIDTH, WINDOW_HEIGHT / DISPLAY_HEIGHT) * zoom * dpi_ratio - local projected_width = WINDOW_WIDTH / (zoom_factor / dpi_ratio) - local projected_height = WINDOW_HEIGHT / (zoom_factor / dpi_ratio) + local camera = cameras[camera_id] + local ww = camera.viewport and camera.viewport.z or WINDOW_WIDTH + local wh = camera.viewport and camera.viewport.w or WINDOW_HEIGHT + + local zoom_factor = math.min(ww / DISPLAY_WIDTH, wh / DISPLAY_HEIGHT) * zoom * dpi_ratio + local projected_width = ww / (zoom_factor / dpi_ratio) + local projected_height = wh / (zoom_factor / dpi_ratio) local xoffset = -(projected_width - DISPLAY_WIDTH) / 2 local yoffset = -(projected_height - DISPLAY_HEIGHT) / 2 return vmath.matrix4_orthographic(xoffset, xoffset + projected_width, yoffset, yoffset + projected_height, near_z, far_z) @@ -89,8 +93,12 @@ end -- setup a fixed aspect ratio projection with a fixed zoom projectors[M.PROJECTOR.FIXED_ZOOM] = function(camera_id, near_z, far_z, zoom) - local projected_width = WINDOW_WIDTH / (zoom / dpi_ratio) - local projected_height = WINDOW_HEIGHT / (zoom / dpi_ratio) + local camera = cameras[camera_id] + local ww = camera.viewport and camera.viewport.z or WINDOW_WIDTH + local wh = camera.viewport and camera.viewport.w or WINDOW_HEIGHT + + local projected_width = ww / (zoom / dpi_ratio) + local projected_height = wh / (zoom / dpi_ratio) local xoffset = -(projected_width - DISPLAY_WIDTH) / 2 local yoffset = -(projected_height - DISPLAY_HEIGHT) / 2 return vmath.matrix4_orthographic(xoffset, xoffset + projected_width, yoffset, yoffset + projected_height, near_z, far_z)