Skip to content

Commit

Permalink
Use viewport width and height in projection calcs (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl authored Mar 31, 2022
1 parent 36215a4 commit 08115a9
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions orthographic/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,26 @@ 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)
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)
Expand Down

0 comments on commit 08115a9

Please sign in to comment.