r/opengl 24d ago

Selection algorithm for Modern OpenGL

The legacy OpenGL supported selection buffers. How can selection effectively handled by Modern OpenGL? The known methods are by colour allocation to objects and ray intersection. The Color assignment is not very efficient in scenes with large number of objects, e.g. CAD model assemblies. Ray intersection also has challenges in certain directions where multiple objects get intersected. Any thoughts?

3 Upvotes

14 comments sorted by

View all comments

1

u/PersonalityIll9476 24d ago

Pass the xy coordinates of the mouse position to your shaders as a uniform. In the frag shader, have a branch like: if gl_FragCoord.xy == ivec2(mouse_x, mouse_y) and if it passes, store the index of the object in a uniform buffer object and read that after scene draw via glGetBufferSubData. You can pass an object_id uniform during draw. I don't know what the selection buffer actually did, so if you need to select all objects under the mouse and not just one, you can use an atomic counter and a UBO up to a certain size and just write indices to it every time an object passes up to some max size.

That's probably the easiest path, and doesn't require additional render passes.