Up until recently I have used the glDrawArrays(…) method to send vertices to the GPU. My drawing contains MANY instances of the same object and in order to optimize I implemented the glDrawArraysInstanced(…) method which accomplishes this. Therefore my code changed from something like:
glLoadName(<do not know what to put here>)
DrawManyModels(<...>)
Therefore, when I attempt to run my picking algorithm to see what the user has clicked on, many models have the same name. Is there a way to resolve this or will this picking algorithm not work with glDrawArraysInstanced(…)?
I don’t know what picking algorithm you are using because you don’t give any details.
I can tell you however, that I have implemented a picking algorithm using drawelementsinstanced.
However my implementation uses a colour texture (rgba16i) bound to a FBO. A second “picking” pass is performed over all interesting objects and the shader emits objectid, primitiveid, materialid and base vertex into the 4 channels.
Reading back the texture yields the exact object, primitive and material per pixel.
Sorry for not being more specific but my picking algorithm utilizes the name stack. It uses the glRenderModel(GL_SELECT) statement to return a buffer which contains information about what was selected.
I am unfamiliar with a technique you are describing here in which you say the “shader emits”. I am unaware of any way to retrieve feedback information from a shader. Also, these values “objectid” and “primitiveid” – are these built-in values in the shader that can be used on the CPU side? Or are they varying values used within the shader?
OpenGL picking mode is depreciated functionality and modern instancing working with this will be at best undefined.
I am talking about using a custom shader and emitting a mix a built in values an custom properties my engine.
An excellent article on Picking the modern way can be found here http://ogldev.atspace.co.uk/www/tutorial29/tutorial29.html
Bear in mind, you don’t have to use an integer texture, a 32-bit float can be used (you’ll have to convert between floats and integers in your own code however, eg round the number down). The choice of texture format really depends on what is supported by your h/w and the range of object IDs you need to pick from.