diff --git a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp index 17bf2acea..25c3b0c60 100644 --- a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp +++ b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp @@ -183,70 +183,113 @@ public: osg::Node* scene = viewer->getSceneData(); if (!scene) return; - + osg::notify(osg::NOTICE)<getCamera()->getViewport(); - float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); - float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); - osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); - -#endif - - osgUtil::IntersectionVisitor iv(picker); - - viewer->getCamera()->accept(iv); - - - if (picker->containsIntersections()) + bool usePolytopePicking = true; + if (usePolytopePicking) { - osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); - osg::notify(osg::NOTICE)<<"Picked "<getCamera()->getViewport(); + float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); + float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); -#if 1 - osg::NodePath& nodePath = intersection.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; + float width = 10.0f; + float height = 10.0f; - if (node) std::cout<<" Hits "<className()<<" nodePath size"<getCamera()->accept(iv); + + if (picker->containsIntersections()) { + osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection(); - std::cout<<" parent "<className()<=1)?nodePath[nodePath.size()-1]:0; + parent = (nodePath.size()>=2)?dynamic_cast(nodePath[nodePath.size()-2]):0; + + if (node) std::cout<<" Hits "<className()<<" nodePath size"<(parent); - if (!parentAsScribe) - { - // node not already picked, so highlight it with an osgFX::Scribe - osgFX::Scribe* scribe = new osgFX::Scribe(); - scribe->addChild(node); - parent->replaceChild(node,scribe); - } - else - { - // node already picked so we want to remove scribe to unpick it. - osg::Node::ParentList parentList = parentAsScribe->getParents(); - for(osg::Node::ParentList::iterator itr=parentList.begin(); - itr!=parentList.end(); - ++itr) - { - (*itr)->replaceChild(parentAsScribe,node); - } - } } -#endif } - + else + { + + #if 0 + // use non dimension coordinates - in projection/clip space + osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),-1.0), osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),1.0) ); + #else + // use window coordinates + // remap the mouse x,y into viewport coordinates. + osg::Viewport* viewport = viewer->getCamera()->getViewport(); + float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); + float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); + osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); + #endif + + osgUtil::IntersectionVisitor iv(picker); + + viewer->getCamera()->accept(iv); + + if (picker->containsIntersections()) + { + osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); + osg::notify(osg::NOTICE)<<"Picked "<=1)?nodePath[nodePath.size()-1]:0; + parent = (nodePath.size()>=2)?dynamic_cast(nodePath[nodePath.size()-2]):0; + + if (node) std::cout<<" Hits "<className()<<" nodePath size"<className()<(parent); + if (!parentAsScribe) + { + // node not already picked, so highlight it with an osgFX::Scribe + osgFX::Scribe* scribe = new osgFX::Scribe(); + scribe->addChild(node); + parent->replaceChild(node,scribe); + } + else + { + // node already picked so we want to remove scribe to unpick it. + osg::Node::ParentList parentList = parentAsScribe->getParents(); + for(osg::Node::ParentList::iterator itr=parentList.begin(); + itr!=parentList.end(); + ++itr) + { + (*itr)->replaceChild(parentAsScribe,node); + } + } + } } void saveSelectedModel(osg::Node* scene) diff --git a/src/osgUtil/IntersectionVisitor.cpp b/src/osgUtil/IntersectionVisitor.cpp index 5dad22f81..fce41f085 100644 --- a/src/osgUtil/IntersectionVisitor.cpp +++ b/src/osgUtil/IntersectionVisitor.cpp @@ -564,9 +564,18 @@ void PolytopeIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawa { if ( !_polytope.contains( drawable->getBound() ) ) return; + osg::Geometry* geometry = drawable->asGeometry(); + osg::Vec3Array* vertices = geometry ? dynamic_cast(geometry->getVertexArray()) : 0; + if (vertices) + { + if (!_polytope.contains(*vertices)) return; + } + Intersection hit; hit.nodePath = iv.getNodePath(); hit.drawable = drawable; + + insertIntersection(hit); } @@ -736,11 +745,11 @@ void IntersectionVisitor::reset() void IntersectionVisitor::apply(osg::Node& node) { - osg::notify(osg::NOTICE)<<"apply(Node&)"<