Choosing triangles in an OpenGL core profile when using glDrawElements

I draw a grid of triangles using glDrawElements, and would like to be able to select / select a triangle with the mouse. The grid of triangles can be very large.

In OpenGL with a fixed function, it is possible to use GL_SELECT: http://content.gpwiki.org/index.php/OpenGL:Tutorials:Picking .. However, I am only interested in the OpenGL kernel profile.

Another option is 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, it is impossible to point to information about the triangle when using glDrawElements?

Finally, I could do the processor-based assembly by shooting the ray beam through the location of the mouse, but that would be pretty slow since I guess I would have to convert the triangles to a CPU, so I would prefer a GPU-based solution.

Does anyone have a suggestion on what to do best when using glDrawElements in an OpenGL kernel profile?

+4
source share
3 answers

As for the “color coding” approach, you can use gl_PrimitiveID to fill the color-coded buffer (s) using the correct fragment shader, this will basically give you the index of the drawn triangle.

Regarding processor-based choices, you can use an existing library that handles acceleration structures and ray intersections, such as Bullet or Opcode .

The “best” option for you depends on your use case, it’s hard to say.

+5
source

Try to see this from this point of view: OpenGL is only for drawing things. Collection must be done differently. If you insist on using OpenGL for it (not unreasonably), use FBO with an integer 16-bit single-channel color buffer attachment into which you visualize the object identifier. The selection is shortened to read one pixel for the identifier.

Finally, I could do the processor-based assembly by shooting the ray beam through the location of the mouse, but that would be pretty slow since I guess I would have to convert the triangles to a CPU, so I would prefer a GPU-based solution.

I recommend it. However, instead of transforming all the triangles, just invert your single bow. You still have to control the scene in some spatial unit structure, so testing against this should be trivial.

+3
source

Well, you really can provide information about the triangle in the DrawElements call. Then you will need to provide the appropriate arrays (former COLOR_ARRAY, TEXTURE_COORD_ARRAY, etc., which are now commonly called VertexAttribArrays). This would probably be the easiest solution if you insist on a GPU.

However, as a rule, the way is to shoot the beam from the point of click into your scene. Therefore, you will not need to convert each triangle, but calculate the intersection test between the ray and your triangles. If the beam crosses, you hit it; if not, you did not. It can really be quite expensive if you make it brute force.

However, usually you will have your triangles contained in some spatial data structure (i.e. your scene graph will / may / should have several representations), for example, an octet (http://en.wikipedia.org/wiki/Octree) eg. This will provide a response to the collision in a number of tests. In addition, you can take into account the final size that the triangle will occupy on the screen (so it would be completely pointless to choose subpixel triangles in most cases).

And if you really want to get some fantasy, you can put the whole process on the GPU too (one example that sounds interesting, for example, http://blog.xeolabs.com/ray-picking-in-scenejs , but there will be more when using Google come.)

For me personally, the easiest way to implement the solution is b) Software Ray Picking, then a) and, finally, collecting the GPU. Start with the easiest, improve if you think this is not fast enough. Don’t get carried away with too high premature optimization (for example, collecting rays-rays works fine on my iPhone;). Have fun and good luck in your choice of algo.

+2
source

Source: https://habr.com/ru/post/1393961/


All Articles