diff --git a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp index 41e111421..047e4680d 100644 --- a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp +++ b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp @@ -120,45 +120,43 @@ public: { std::cout<<"Picking "<getViewport(origX,origY,width,height); - // convert Produce's non dimensional x,y coords back into pixel coords. - int winX = (int)((x+1.0f)*0.5f*(float)width); - int winY = (int)((y+1.0f)*0.5f*(float)height); - - osg::Vec3 nearPoint, farPoint; - _sceneView->projectWindowXYIntoObject(winX,winY,nearPoint,farPoint); - - std::cout<<"nearPoint "< lineSegment = new osg::LineSegment(nearPoint,farPoint); - - // create the IntersectVisitor to do the line intersection traversals. - osgUtil::IntersectVisitor intersector; - intersector.addLineSegment(lineSegment.get()); + // convert Producer's non dimensional x,y coords back into pixel coords. + int pixel_x = (int)((x+1.0f)*0.5f*(float)width); + int pixel_y = (int)((y+1.0f)*0.5f*(float)height); - scene->accept(intersector); - osgUtil::IntersectVisitor::HitList& hits=intersector.getHitList(lineSegment.get()); - if (!hits.empty()) + osgUtil::PickVisitor pick(_sceneView->getViewport(), + _sceneView->getProjectionMatrix(), + _sceneView->getViewMatrix(), + pixel_x, pixel_y); + + scene->accept(pick); + + osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList(); + if (!segHitList.empty() && !segHitList.begin()->second.empty()) { std::cout<<"Got hits"<second; + // just take the first hit - nearest the eye point. osgUtil::Hit& hit = hits.front(); osg::NodePath& nodePath = hit._nodePath; osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; osg::Group* parent = (nodePath.size()>=2)?dynamic_cast(nodePath[nodePath.size()-2]):0; - + + if (node) std::cout<<" Hits "<className()<<" nodePath size"<className()<className()<className()<(parent); if (!parentAsScribe)