openglselectionvbomouse-picking

OpenGL Mouse Picking Strategy


I am using OpenGL to render a model of an object that is rotationally-symmetric in a given plane, and I want the user to be able to scroll over the model (possibly after rotations, zooms, etc.) and determine what world coordinate on the model the mouse is currently pointing to.

The reason I mentioned the symmetry is that I'm building the model using VBOs of individual components for ease of use. An analogy to what I'm doing would be a bicycle wheel - I'd have one VBO for a spoke, one for the hub, and one for the wheel/tire and I'd resuse the spoke VBO a number of times (after suitable translations and rotations). My first question is, is this arrangement conducive to the kind of picking that I'm trying to do? I want each individual spoke in the resulting model to be "pickable", for example. Do I need a seperate VBO for each quad/triangle in the mesh to do the kind of selection that I'm trying to do? I really hope that's not the case...

Also, what would be the best picking algorithm to use? I've heard nothing but negative things about OpenGL's built-in selection mode. Thanks in advance!


Solution

  • Regarding your question about VBOs, there is nothing wrong in having few VBOs and reuse them. In fact, you can have everything in one VBO and only index into it. For your bicycle wheel analogy, the spoke vertices could be followed by the tire vertices, and you could draw the vertices of the spokes several times (maybe even using instancing, see glMultiDrawElements), followed by drawing from the same VBO but starting at a different index, the tire.

    Regarding your picking question, one easy way of getting the world coordinate of the point on the model under the mouse would be to read back the depth value (glReadPixels on a 1x1 rectangle, preferrably with a pixel buffer object on the last frame's data, that way you hide the transfer latency). Then call gluUnproject to get world coordinates.