![]() |
ImFusion SDK 4.3
|
#include <ImFusion/Core/GL/ObjectPicking.h>
Helper class to consolidate functionality for implementing object picking of rendered geometry. More...
Helper class to consolidate functionality for implementing object picking of rendered geometry.
The term "object picking" refers to the process of attaching an additional helper texture to the used FBO in which you can encode per-pixel object ownership information. Then, in the fragment shader in addition to the regular color output you write to this texture to encode the currently rendered object/element. The OpenGL rendering pipeline will use the same per-fragment processing for all attachments so that the picking texture is correctly filled wrt. the scissor, stencil, and depth tests. Finally, after the rendering you can use the picking texture to lookup the object identity for each pixel, for instance to determine on which object a user has clicked.
GL::ObjectPicking implements this using a 2-channel texture to store picking information in terms of Object ID
(to distinguish multiple objects rendered into the same scene) and Element ID
(to distinguish individual parts of a single rendered object):
Object ID
is managed by the ObjectPicking instance: all known objects must first be registered either during reset() or using registerObject(). This will generate a unique ID for each object so that it can be assigned automatically inside the picking shader include.Element ID
is managed by the user and has to be passed to the shader manually. Depending on the use case you can decide if you use a constant ID for the entire object or if you need to distinguish different parts within one object (e.g. faces of a mesh).Multiple actors collaborate in order to implement the functionality:
picking_writeIndex(uint elementId)
: In the ImFusion view framework, step 1 will be implemented for instance by a GlVolumeView. Step 2 is implemented by selected GlObjects. Since the GlView is passed to void GlObject::draw()
it can access the view's ObjectPicking instance in order to support writing picking info.
Classes | |
struct | PickingInfo |
Structure encoding picking information for a single pixel. More... | |
Public Member Functions | |
void | reset (GL::Framebuffer &fbo, vec2i viewportSize, std::vector< const void * > knownObjects) |
Initializes and resets object picking data structures. | |
void | registerObject (const void *object) |
Register an object instance so that it receives its own unique ID and is known to the picking backend. | |
void | enable (GL::Program &program, const void *object) const |
Bind all object picking structures to the given shader so that you can use the shader include. | |
void | enable (GL::Program &program, uint32_t objectId) const |
Bind all object picking structures to the given shader so that you can use the shader include. | |
void | disable () const |
Disable object picking by detaching the picking texture from the FBO passed during reset() and issuing a glDrawBuffers() call. | |
uint32_t | objectId (const void *object) const |
Return the object ID for the given void. Unknown objects have an ID of 0xFFFFFFFF. | |
const void * | objectWithId (uint32_t id) const |
Return void for the given ID. Returns nullptr for unknown id. | |
std::vector< PickingInfo > | getPickingInfo (const std::vector< vec2i > &pixels, const void *object=nullptr) const |
Return the picking information for the given pixels with optional filtering. | |
const Texture * | pickingTexture () const |
Getter for m_pickingTexture. | |
void reset | ( | GL::Framebuffer & | fbo, |
vec2i | viewportSize, | ||
std::vector< const void * > | knownObjects ) |
Initializes and resets object picking data structures.
This function must be called every time at the beginning of the render loop.
fbo | Framebuffer object to attach the picking texture to. ObjectPicking will keep a reference to it and use it during enable()/disable(). |
viewportSize | Viewport size in pixels |
knownObjects | Set of all objects that are known to render, used to assign unique object ids. If you don't know all object yet you can later call registerObject(). |
void enable | ( | GL::Program & | program, |
const void * | object ) const |
Bind all object picking structures to the given shader so that you can use the shader include.
program | Shader to prepare for writing object picking info. |
object | Pointer to the object that the shader belongs to, will use objectId() to generate the ID for it. |
void enable | ( | GL::Program & | program, |
uint32_t | objectId ) const |
Bind all object picking structures to the given shader so that you can use the shader include.
This overload lets the user choose an arbitrary ID to be written. For objects that were registered during reset() or with registerObject() it can be used in connection with objectId() to get a valid ID. Otherwise the user has to track the used IDs and map the return value of getPickingInfo() themselves.
program | Shader to prepare for writing object picking info. |
objectId | A numeric ID used for rendering this object |
std::vector< PickingInfo > getPickingInfo | ( | const std::vector< vec2i > & | pixels, |
const void * | object = nullptr ) const |
Return the picking information for the given pixels with optional filtering.
pixels | Set of pixels to query picking information for. |
object | Optional object to filter picking information for; no filtering will be performed if this is null. |
const Texture * pickingTexture | ( | ) | const |
Getter for m_pickingTexture.