Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking triangles in OpenGL core profile when using glDrawElements

I am drawing a mesh of triangles using glDrawElements and would like to be able to pick/select a triangle using the mouse click. The mesh of triangles can be very big.

In fixed-function OpenGL there is the possibility of using GL_SELECT: http://content.gpwiki.org/index.php/OpenGL:Tutorials:Picking .. However I am only interested in using OpenGL core profile.

Another possibility would be to use 'color coding':

http://www.lighthouse3d.com/opengl/picking/index.php?color1

http://www.opengl.org/resources/faq/technical/selection.htm

.. but as far as I know its not possible to indicate per triangle information yet when using glDrawElements?

Finally I could do CPU based picking by shooting a pick ray through the mouse location, but this would be quite slow since I guess I would have to transform the triangles on the CPU, so I would prefer a GPU based solution.

Does anyone have a suggestion on what is the best way do picking when using glDrawElements in OpenGL core profile?

like image 558
WesDec Avatar asked Dec 28 '22 06:12

WesDec


2 Answers

Try seeing it from this perspective: OpenGL is only meant for drawing things. Picking should be done in another way. If you insist on using OpenGL for it (not unreasonable), use a FBO with a integer 16 bit single channel color buffer attachment, into which you render the object ID. Picking reduces to read the one pixel for the ID.

Finally I could do CPU based picking by shooting a pick ray through the mouse location, but this would be quite slow since I guess I would have to transform the triangles on the CPU, so I would prefer a GPU based solution.

I recommend this. However instead of transforming all the triangles, just inversely transform your one picking ray. You should manage you scene in some spatial subdivision structure anyway, so testing against that should be trivial.

like image 29
datenwolf Avatar answered Feb 03 '23 14:02

datenwolf


About the 'color coding' approach, you could use gl_PrimitiveID to fill the color coded buffer(s) using a proper fragment shader, this would basically give you the index of the drawn triangle.

About the CPU based picking, you could use an existing library that handles the acceleration structures, and ray-mesh intersection, such as Bullet or Opcode.

The 'best' option for you depends of your use case, hard to tell.

like image 95
rotoglup Avatar answered Feb 03 '23 12:02

rotoglup