| 186 | | |
| 187 | | |
| 188 | | osg::notify(osg::NOTICE)<<std::endl; |
| 189 | | |
| 190 | | #if 0 |
| 191 | | // use non dimension coordinates - in projection/clip space |
| 192 | | 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) ); |
| 193 | | #else |
| 194 | | // use window coordinates |
| 195 | | // remap the mouse x,y into viewport coordinates. |
| 196 | | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
| 197 | | float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); |
| 198 | | float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); |
| 199 | | osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); |
| 200 | | |
| 201 | | #endif |
| 202 | | |
| 203 | | osgUtil::IntersectionVisitor iv(picker); |
| 204 | | |
| 205 | | viewer->getCamera()->accept(iv); |
| 206 | | |
| 207 | | |
| 208 | | if (picker->containsIntersections()) |
| 209 | | { |
| 210 | | osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); |
| 211 | | osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; |
| 212 | | |
| 213 | | #if 1 |
| 214 | | osg::NodePath& nodePath = intersection.nodePath; |
| 215 | | osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
| 216 | | osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
| 217 | | |
| 218 | | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
| 219 | | |
| 220 | | // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked" |
| 221 | | if (parent && node) |
| 222 | | { |
| 223 | | |
| 224 | | std::cout<<" parent "<<parent->className()<<std::endl; |
| 225 | | |
| 226 | | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
| 227 | | if (!parentAsScribe) |
| | 186 | osg::notify(osg::NOTICE)<<std::endl; |
| | 187 | |
| | 188 | osg::Node* node = 0; |
| | 189 | osg::Group* parent = 0; |
| | 190 | |
| | 191 | bool usePolytopePicking = true; |
| | 192 | if (usePolytopePicking) |
| | 193 | { |
| | 194 | // use window coordinates |
| | 195 | // remap the mouse x,y into viewport coordinates. |
| | 196 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
| | 197 | float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); |
| | 198 | float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); |
| | 199 | |
| | 200 | float width = 10.0f; |
| | 201 | float height = 10.0f; |
| | 202 | |
| | 203 | float min_x = mx - width*0.5; |
| | 204 | float max_x = mx + width*0.5; |
| | 205 | float min_y = my - height*0.5; |
| | 206 | float max_y = my + height*0.5; |
| | 207 | |
| | 208 | osg::Polytope polytope; |
| | 209 | polytope.add(osg::Plane(1,0,0,-min_x)); |
| | 210 | polytope.add(osg::Plane(-1,0,0,max_x)); |
| | 211 | polytope.add(osg::Plane(0,1,0,-min_y)); |
| | 212 | polytope.add(osg::Plane(0,-1,0,max_y)); |
| | 213 | polytope.add(osg::Plane(0,0,1,0)); |
| | 214 | |
| | 215 | osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, polytope ); |
| | 216 | |
| | 217 | osgUtil::IntersectionVisitor iv(picker); |
| | 218 | |
| | 219 | viewer->getCamera()->accept(iv); |
| | 220 | |
| | 221 | if (picker->containsIntersections()) |
| | 222 | { |
| | 223 | osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection(); |
| | 224 | |
| | 225 | osg::NodePath& nodePath = intersection.nodePath; |
| | 226 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
| | 227 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
| | 228 | |
| | 229 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
| | 230 | |
| | 231 | } |
| | 232 | |
| | 233 | } |
| | 234 | else |
| | 235 | { |
| | 236 | |
| | 237 | #if 0 |
| | 238 | // use non dimension coordinates - in projection/clip space |
| | 239 | 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) ); |
| | 240 | #else |
| | 241 | // use window coordinates |
| | 242 | // remap the mouse x,y into viewport coordinates. |
| | 243 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
| | 244 | float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); |
| | 245 | float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); |
| | 246 | osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); |
| | 247 | #endif |
| | 248 | |
| | 249 | osgUtil::IntersectionVisitor iv(picker); |
| | 250 | |
| | 251 | viewer->getCamera()->accept(iv); |
| | 252 | |
| | 253 | if (picker->containsIntersections()) |
| | 254 | { |
| | 255 | osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); |
| | 256 | osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; |
| | 257 | |
| | 258 | osg::NodePath& nodePath = intersection.nodePath; |
| | 259 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
| | 260 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
| | 261 | |
| | 262 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
| | 263 | |
| | 264 | } |
| | 265 | } |
| | 266 | |
| | 267 | // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked" |
| | 268 | if (parent && node) |
| | 269 | { |
| | 270 | |
| | 271 | std::cout<<" parent "<<parent->className()<<std::endl; |
| | 272 | |
| | 273 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
| | 274 | if (!parentAsScribe) |
| | 275 | { |
| | 276 | // node not already picked, so highlight it with an osgFX::Scribe |
| | 277 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
| | 278 | scribe->addChild(node); |
| | 279 | parent->replaceChild(node,scribe); |
| | 280 | } |
| | 281 | else |
| | 282 | { |
| | 283 | // node already picked so we want to remove scribe to unpick it. |
| | 284 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
| | 285 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
| | 286 | itr!=parentList.end(); |
| | 287 | ++itr) |