| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <osg/Timer> |
|---|
| 7 | #include <osg/io_utils> |
|---|
| 8 | #include <osg/observer_ptr> |
|---|
| 9 | |
|---|
| 10 | #include <osgUtil/IntersectionVisitor> |
|---|
| 11 | #include <osgUtil/PolytopeIntersector> |
|---|
| 12 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/ReadFile> |
|---|
| 15 | #include <osgDB/WriteFile> |
|---|
| 16 | |
|---|
| 17 | #include <osgGA/TrackballManipulator> |
|---|
| 18 | #include <osgGA/StateSetManipulator> |
|---|
| 19 | |
|---|
| 20 | #include <osgViewer/SimpleViewer> |
|---|
| 21 | #include <osgViewer/GraphicsWindow> |
|---|
| 22 | |
|---|
| 23 | #include <osgFX/Scribe> |
|---|
| 24 | |
|---|
| 25 | #include <iostream> |
|---|
| 26 | |
|---|
| 27 | class CreateModelToSaveVisitor : public osg::NodeVisitor |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | CreateModelToSaveVisitor(): |
|---|
| 32 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 33 | { |
|---|
| 34 | _group = new osg::Group; |
|---|
| 35 | _addToModel = false; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | virtual void apply(osg::Node& node) |
|---|
| 39 | { |
|---|
| 40 | osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node); |
|---|
| 41 | if (scribe) |
|---|
| 42 | { |
|---|
| 43 | for(unsigned int i=0; i<scribe->getNumChildren(); ++i) |
|---|
| 44 | { |
|---|
| 45 | _group->addChild(scribe->getChild(i)); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | else |
|---|
| 49 | { |
|---|
| 50 | traverse(node); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | osg::ref_ptr<osg::Group> _group; |
|---|
| 55 | bool _addToModel; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | class ExitHandler : public osgGA::GUIEventHandler |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | |
|---|
| 62 | ExitHandler(): |
|---|
| 63 | _done(false) {} |
|---|
| 64 | |
|---|
| 65 | bool done() const { return _done; } |
|---|
| 66 | |
|---|
| 67 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
|---|
| 68 | { |
|---|
| 69 | switch(ea.getEventType()) |
|---|
| 70 | { |
|---|
| 71 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 72 | { |
|---|
| 73 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape) |
|---|
| 74 | { |
|---|
| 75 | _done = true; |
|---|
| 76 | } |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | case(osgGA::GUIEventAdapter::CLOSE_WINDOW): |
|---|
| 80 | case(osgGA::GUIEventAdapter::QUIT_APPLICATION): |
|---|
| 81 | { |
|---|
| 82 | _done = true; |
|---|
| 83 | } |
|---|
| 84 | default: break; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return false; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | bool _done; |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | class PickHandler : public osgGA::GUIEventHandler |
|---|
| 97 | { |
|---|
| 98 | public: |
|---|
| 99 | |
|---|
| 100 | PickHandler(): |
|---|
| 101 | _mx(0.0),_my(0.0) {} |
|---|
| 102 | |
|---|
| 103 | ~PickHandler() {} |
|---|
| 104 | |
|---|
| 105 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 106 | { |
|---|
| 107 | osgViewer::SimpleViewer* viewer = dynamic_cast<osgViewer::SimpleViewer*>(&aa); |
|---|
| 108 | |
|---|
| 109 | switch(ea.getEventType()) |
|---|
| 110 | { |
|---|
| 111 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 112 | { |
|---|
| 113 | if (ea.getKey()=='s' && viewer) |
|---|
| 114 | { |
|---|
| 115 | saveSelectedModel(viewer->getSceneData()); |
|---|
| 116 | } |
|---|
| 117 | return false; |
|---|
| 118 | } |
|---|
| 119 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 120 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 121 | { |
|---|
| 122 | _mx = ea.getX(); |
|---|
| 123 | _my = ea.getY(); |
|---|
| 124 | |
|---|
| 125 | osg::notify(osg::NOTICE)<<"_mx="<<_mx<<" _my="<<_my<<std::endl; |
|---|
| 126 | osg::notify(osg::NOTICE)<<" range ="<<ea.getXmin()<<", "<<ea.getXmax()<<std::endl; |
|---|
| 127 | |
|---|
| 128 | return false; |
|---|
| 129 | } |
|---|
| 130 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 131 | { |
|---|
| 132 | if (_mx == ea.getX() && _my == ea.getY()) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | pick(ea,viewer); |
|---|
| 136 | } |
|---|
| 137 | return true; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | default: |
|---|
| 141 | return false; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void pick(const osgGA::GUIEventAdapter& ea, osgViewer::SimpleViewer* viewer) |
|---|
| 146 | { |
|---|
| 147 | osg::Node* scene = viewer->getSceneData(); |
|---|
| 148 | if (!scene) return; |
|---|
| 149 | |
|---|
| 150 | osg::notify(osg::NOTICE)<<std::endl; |
|---|
| 151 | |
|---|
| 152 | osg::Node* node = 0; |
|---|
| 153 | osg::Group* parent = 0; |
|---|
| 154 | |
|---|
| 155 | bool usePolytopePicking = false; |
|---|
| 156 | if (usePolytopePicking) |
|---|
| 157 | { |
|---|
| 158 | |
|---|
| 159 | #if 0 |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
|---|
| 163 | double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5)); |
|---|
| 164 | double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5)); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | double w = 5.0f; |
|---|
| 168 | double h = 5.0f; |
|---|
| 169 | osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h ); |
|---|
| 170 | #else |
|---|
| 171 | double mx = ea.getXnormalized(); |
|---|
| 172 | double my = ea.getYnormalized(); |
|---|
| 173 | double w = 0.05; |
|---|
| 174 | double h = 0.05; |
|---|
| 175 | osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h ); |
|---|
| 176 | #endif |
|---|
| 177 | osgUtil::IntersectionVisitor iv(picker); |
|---|
| 178 | |
|---|
| 179 | viewer->getCamera()->accept(iv); |
|---|
| 180 | |
|---|
| 181 | if (picker->containsIntersections()) |
|---|
| 182 | { |
|---|
| 183 | osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection(); |
|---|
| 184 | |
|---|
| 185 | osg::NodePath& nodePath = intersection.nodePath; |
|---|
| 186 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 187 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 188 | |
|---|
| 189 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
|---|
| 190 | |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | { |
|---|
| 196 | |
|---|
| 197 | #if 0 |
|---|
| 198 | |
|---|
| 199 | osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() ); |
|---|
| 200 | #else |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
|---|
| 204 | float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); |
|---|
| 205 | float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); |
|---|
| 206 | osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my ); |
|---|
| 207 | #endif |
|---|
| 208 | |
|---|
| 209 | osgUtil::IntersectionVisitor iv(picker); |
|---|
| 210 | |
|---|
| 211 | viewer->getCamera()->accept(iv); |
|---|
| 212 | |
|---|
| 213 | if (picker->containsIntersections()) |
|---|
| 214 | { |
|---|
| 215 | osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); |
|---|
| 216 | osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; |
|---|
| 217 | |
|---|
| 218 | osg::NodePath& nodePath = intersection.nodePath; |
|---|
| 219 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 220 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 221 | |
|---|
| 222 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if (parent && node) |
|---|
| 229 | { |
|---|
| 230 | |
|---|
| 231 | std::cout<<" parent "<<parent->className()<<std::endl; |
|---|
| 232 | |
|---|
| 233 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
|---|
| 234 | if (!parentAsScribe) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
|---|
| 238 | scribe->addChild(node); |
|---|
| 239 | parent->replaceChild(node,scribe); |
|---|
| 240 | } |
|---|
| 241 | else |
|---|
| 242 | { |
|---|
| 243 | |
|---|
| 244 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
|---|
| 245 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
|---|
| 246 | itr!=parentList.end(); |
|---|
| 247 | ++itr) |
|---|
| 248 | { |
|---|
| 249 | (*itr)->replaceChild(parentAsScribe,node); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | void saveSelectedModel(osg::Node* scene) |
|---|
| 256 | { |
|---|
| 257 | if (!scene) return; |
|---|
| 258 | |
|---|
| 259 | CreateModelToSaveVisitor cmtsv; |
|---|
| 260 | scene->accept(cmtsv); |
|---|
| 261 | |
|---|
| 262 | if (cmtsv._group->getNumChildren()>0) |
|---|
| 263 | { |
|---|
| 264 | std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl; |
|---|
| 265 | osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg"); |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | protected: |
|---|
| 270 | |
|---|
| 271 | float _mx,_my; |
|---|
| 272 | }; |
|---|
| 273 | |
|---|
| 274 | int main( int argc, char **argv ) |
|---|
| 275 | { |
|---|
| 276 | if (argc<2) |
|---|
| 277 | { |
|---|
| 278 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 279 | return 1; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 284 | if (!loadedModel) |
|---|
| 285 | { |
|---|
| 286 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 287 | return 1; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 292 | traits->x = 200; |
|---|
| 293 | traits->y = 200; |
|---|
| 294 | traits->width = 800; |
|---|
| 295 | traits->height = 600; |
|---|
| 296 | traits->windowDecoration = true; |
|---|
| 297 | traits->doubleBuffer = true; |
|---|
| 298 | traits->sharedContext = 0; |
|---|
| 299 | |
|---|
| 300 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 301 | osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get()); |
|---|
| 302 | if (!gw) |
|---|
| 303 | { |
|---|
| 304 | osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl; |
|---|
| 305 | return 1; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | gw->realize(); |
|---|
| 309 | gw->makeCurrent(); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | osgViewer::SimpleViewer viewer; |
|---|
| 313 | viewer.setSceneData(loadedModel.get()); |
|---|
| 314 | |
|---|
| 315 | viewer.setEventQueue(gw->getEventQueue()); |
|---|
| 316 | viewer.getEventQueue()->windowResize(traits->x,traits->y,traits->width,traits->height); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | viewer.setCameraManipulator( new osgGA::TrackballManipulator ); |
|---|
| 320 | |
|---|
| 321 | osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; |
|---|
| 322 | statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet()); |
|---|
| 323 | viewer.addEventHandler(statesetManipulator.get()); |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | viewer.addEventHandler(new PickHandler()); |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | ExitHandler* exitHandler = new ExitHandler; |
|---|
| 331 | viewer.addEventHandler(exitHandler); |
|---|
| 332 | |
|---|
| 333 | viewer.init(); |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | while( gw->isRealized() && !exitHandler->done()) |
|---|
| 337 | { |
|---|
| 338 | gw->checkEvents(); |
|---|
| 339 | |
|---|
| 340 | viewer.frame(); |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | gw->swapBuffers(); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | return 0; |
|---|
| 347 | } |
|---|