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 4f3e68c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions GLMakie/src/picking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ 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)
sf = screen.px_per_unit[]
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)...)
if rx > 0 && ry > 0 && rx + rw <= w && ry + rh <= h
rx, ry, rw, rh = round.(Int, sf .* (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 +28,17 @@ 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)
sf = screen.px_per_unit[]
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
if x > 0 && y > 0 && x <= w && y <= h
sid = Base.RefValue{SelectionID{UInt32}}()
x, y = round.(Int, sf .* (x, y))
glReadPixels(x, y, 1, 1, buff.format, buff.pixeltype, sid)
return convert(SelectionID{Int}, sid[])
end
Expand Down

0 comments on commit 4f3e68c

Please sign in to comment.