[osg-users] Easy way to pick nearest edge?

Simon Notheis notheis at ira.uka.de
Tue Sep 2 00:54:13 PDT 2008


Hi everyone,

I want to use mouse events to pick an object of the 3D scene. Instead of
working with the intersection point, I want to find the nearest vertex
or edge of the polygon that was hit.
Until now I used code similar to the following one:

  // Get the hitlist by using osg::Viewer::computeIntersection(...)
  const osgUtil::LineSegmentIntersector::Intersection&
    intersection = *(hitlist.begin())
  osg::Geometry* geometry = intersection.drawable->asGeometry()

Then i used intersection.indexList and the geometry's VertexArray to
iterate through all vertices to find the closest vertex (vx) wrt the
intersection point.
Then I calculated the angles between vx and the both neighbouring
vertices in the indexList to determine which one belongs to the closest
edge. Finally, the nodePath gave me the local=>world transformation.

All of that only worked because we had some bad code to load/convert our
own scene format into OSG. This code generated seperate triangles or
quads, so actually I always hit a polygon with 3 or 4 vertices, what
made it easy to determinie the neighbours of vx.

Now that we're using some optimization steps (e.g. triangle strips), I
can't use the above code (e.g. the indexList is empty). I tried to use
the geometry's IndexArray instead, but the problem is that without
knowing the exact primitive type, it is hard to determine which vertices
are the neighbouring ones to find the edge. E.g. in case of an triangle
strip I would have to check the last 2 and next 2 vertices in the
index/vertex list. Also the code won't work if the drawable can't be
cast to a Geometry.

So my question is, if there's a more easy or flexible way to determine
the closest vertex and edge of the intersected polygon/primitve?

Looking forward to your ideas or solutions, if something similar was
already discussed here.....

  Simon




More information about the osg-users mailing list