0
votes

I am working on a project that I need to select two vertices of a mesh and calculate the shortest path between them. I am using GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER for binding the OpenGL buffers and draw them by glDrawElements.

Finding the shortest path is not an issue here. The issue is in vertex selection. I need to select the vertices by mouse click and store their ID for further process. I have some functions that return the mouse position, but I couldn't find a method that could get the vertex ID or triangle ID.

I also found about rayPicking methods, but that one is used for selecting the mesh itself rather than an element of it.

I appreciate any help or idea

Update: The meshes that I have to deal with, have high number of vertices and triangles, so methods such as applying unique color to each vertex is not a proper solution. Also adding a sphere to each vertex or triangle and calculate the intersection point of the ray with sphere doesn't sound proper way either.

2
You can use ray-picking with a sphere centered on each mesh point.Colonel Thirty Two

2 Answers

0
votes

I never implemented something like this, but i would recomend another rendering pass like the following.

  1. Define for every Vertex a unique color which specifies the ID of the vertex.
  2. Because it is hard to pick exactly one pixel try to draw a small quad around it. glPointSize(...) may be useful.
  3. Read the color of the pixel out of your texture/framebuffer you rendered the small quads into.
  4. Calculate the vertex ID out of the color.

I heard of several similar implementations for triangles. Maybe you can get some more inspiration from them.

0
votes

Conceptually, you can create another separate frame buffer. Then encode the vertex ID into a RGBA color value. When you draw the mesh on the current visible frame buffer, you need to draw the encoded color of the vertex ID into the other frame buffer simultaneously. When picking a point, you can recover the encoded vertex ID from the other frame buffer, and decode it to get the original vertex ID. In short, you need to make another invisible separate frame buffer for picking.