Skip to content

Commit

Permalink
Fix picking functions when framebuffer resolution differs from screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jmert committed Jan 4, 2023
1 parent 41e4e8d commit 91e5509
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions GLMakie/src/picking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
function pick_native(screen::Screen, rect::Rect2i)
isopen(screen) || return Matrix{SelectionID{Int}}(undef, 0, 0)
ShaderAbstractions.switch_context!(screen.glscreen)
window_size = size(screen)
fb = screen.framebuffer
buff = fb.buffers[:objectid]
glBindFramebuffer(GL_FRAMEBUFFER, fb.id[1])
glReadBuffer(GL_COLOR_ATTACHMENT1)
rx, ry = minimum(rect)
rw, rh = widths(rect)
w, h = window_size
sid = zeros(SelectionID{UInt32}, widths(rect)...)
w, h = size(screen)
if rx > 0 && ry > 0 && rx + rw <= w && ry + rh <= h
rx, ry, rw, rh = round.(Int, screen.px_per_unit[] .* (rx, ry, rw, rh))
sid = zeros(SelectionID{UInt32}, rw, rh)
glReadPixels(rx, ry, rw, rh, buff.format, buff.pixeltype, sid)
return sid
else
Expand All @@ -26,15 +26,15 @@ end
function pick_native(screen::Screen, xy::Vec{2, Float64})
isopen(screen) || return SelectionID{Int}(0, 0)
ShaderAbstractions.switch_context!(screen.glscreen)
sid = Base.RefValue{SelectionID{UInt32}}()
window_size = size(screen)
fb = screen.framebuffer
buff = fb.buffers[:objectid]
glBindFramebuffer(GL_FRAMEBUFFER, fb.id[1])
glReadBuffer(GL_COLOR_ATTACHMENT1)
x, y = floor.(Int, xy)
w, h = window_size
w, h = size(screen)
if x > 0 && y > 0 && x <= w && y <= h
x, y = round.(Int, screen.px_per_unit[] .* (x, y))
sid = Base.RefValue{SelectionID{UInt32}}()
glReadPixels(x, y, 1, 1, buff.format, buff.pixeltype, sid)
return convert(SelectionID{Int}, sid[])
end
Expand Down

0 comments on commit 91e5509

Please sign in to comment.