| 190 | | |
| 191 | | // do the pick traversal. |
| 192 | | osgUtil::PickVisitor pick(_sceneView->getViewport(), |
| 193 | | _sceneView->getProjectionMatrix(), |
| 194 | | _sceneView->getViewMatrix(), mx, my); |
| 195 | | scene->accept(pick); |
| 196 | | |
| 197 | | osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList(); |
| 198 | | if (!segHitList.empty() && !segHitList.begin()->second.empty()) |
| 199 | | { |
| 200 | | std::cout<<"Got hits"<<std::endl; |
| 201 | | |
| 202 | | // get the hits for the first segment |
| 203 | | osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second; |
| 204 | | |
| 205 | | // just take the first hit - nearest the eye point. |
| 206 | | osgUtil::Hit& hit = hits.front(); |
| 207 | | |
| 208 | | osg::NodePath& nodePath = hit._nodePath; |
| | 191 | |
| | 192 | #if 1 |
| | 193 | |
| | 194 | osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); |
| | 195 | picker->setCoordinateFrame(osgUtil::Intersector::WINDOW); |
| | 196 | |
| | 197 | osgUtil::IntersectionVisitor iv(picker); |
| | 198 | |
| | 199 | _sceneView->getCamera()->accept(iv); |
| | 200 | |
| | 201 | osg::notify(osg::NOTICE)<<"Done pick, "<<picker->containsIntersections()<<std::endl; |
| | 202 | |
| | 203 | if (picker->containsIntersections()) |
| | 204 | { |
| | 205 | osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); |
| | 206 | osg::notify(osg::NOTICE)<<"Picking "<<intersection.localIntersectionPoint<<std::endl; |
| | 207 | |
| | 208 | |
| | 209 | osg::NodePath& nodePath = intersection.nodePath; |
| | 243 | |
| | 244 | |
| | 245 | #else |
| | 246 | |
| | 247 | // do the pick traversal. |
| | 248 | osgUtil::PickVisitor pick(_sceneView->getViewport(), |
| | 249 | _sceneView->getProjectionMatrix(), |
| | 250 | _sceneView->getViewMatrix(), mx, my); |
| | 251 | scene->accept(pick); |
| | 252 | |
| | 253 | osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList(); |
| | 254 | if (!segHitList.empty() && !segHitList.begin()->second.empty()) |
| | 255 | { |
| | 256 | std::cout<<"Got hits"<<std::endl; |
| | 257 | |
| | 258 | // get the hits for the first segment |
| | 259 | osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second; |
| | 260 | |
| | 261 | // just take the first hit - nearest the eye point. |
| | 262 | osgUtil::Hit& hit = hits.front(); |
| | 263 | |
| | 264 | osg::NodePath& nodePath = hit._nodePath; |
| | 265 | osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
| | 266 | osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
| | 267 | |
| | 268 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
| | 269 | |
| | 270 | // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked" |
| | 271 | if (parent && node) |
| | 272 | { |
| | 273 | |
| | 274 | std::cout<<" parent "<<parent->className()<<std::endl; |
| | 275 | |
| | 276 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
| | 277 | if (!parentAsScribe) |
| | 278 | { |
| | 279 | // node not already picked, so highlight it with an osgFX::Scribe |
| | 280 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
| | 281 | scribe->addChild(node); |
| | 282 | parent->replaceChild(node,scribe); |
| | 283 | } |
| | 284 | else |
| | 285 | { |
| | 286 | // node already picked so we want to remove scribe to unpick it. |
| | 287 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
| | 288 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
| | 289 | itr!=parentList.end(); |
| | 290 | ++itr) |
| | 291 | { |
| | 292 | (*itr)->replaceChild(parentAsScribe,node); |
| | 293 | } |
| | 294 | } |
| | 295 | } |
| | 296 | |
| | 297 | } |
| | 298 | #endif |
| | 299 | |