PDA

View Full Version : Picking when using glDrawArraysInstanced



walkins
11-02-2011, 10:36 AM
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(<model1 name>)
DrawModel(<model1>)

glLoadName(<model2 name>)
DrawModel(<model2>)

to:


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(...)?

Thanks!

BionicBytes
11-02-2011, 03:17 PM
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.

walkins
11-03-2011, 12:12 PM
Thanks for your answer.

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?

Thanks for your help!

BionicBytes
11-03-2011, 01:29 PM
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.

V-man
11-03-2011, 03:04 PM
http://www.opengl.org/wiki/Common_Mistakes#Selection_and_Picking_and_Feedback _Mode

BionicBytes
11-04-2011, 02:07 AM
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.

V-man
11-04-2011, 04:04 AM
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.


That article suffers the good old mistake
http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

This is part of the code from his page


// Create the FBO
glGenFramebuffers(1, &amp;m_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);

// Create the texture object for the primitive information buffer
glGenTextures(1, &amp;m_pickingTexture);
glBindTexture(GL_TEXTURE_2D, m_pickingTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32UI, WindowWidth, WindowHeight,
0, GL_RGB_INTEGER, GL_UNSIGNED_INT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
m_pickingTexture, 0);


and I don't see him creating a mipmap chain anywhere.