| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <Producer/RenderSurface> |
|---|
| 7 | #include <Producer/KeyboardMouse> |
|---|
| 8 | #include <Producer/Trackball> |
|---|
| 9 | |
|---|
| 10 | #include <osg/Timer> |
|---|
| 11 | #include <osg/io_utils> |
|---|
| 12 | #include <osg/observer_ptr> |
|---|
| 13 | |
|---|
| 14 | #include <osgUtil/SceneView> |
|---|
| 15 | #include <osgUtil/IntersectVisitor> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgDB/WriteFile> |
|---|
| 19 | |
|---|
| 20 | #include <osgGA/SimpleViewer> |
|---|
| 21 | #include <osgGA/TrackballManipulator> |
|---|
| 22 | #include <osgGA/StateSetManipulator> |
|---|
| 23 | |
|---|
| 24 | #include <osgFX/Scribe> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) : |
|---|
| 32 | _done(false), |
|---|
| 33 | _eventQueue(eventQueue) |
|---|
| 34 | { |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | virtual void shutdown() |
|---|
| 38 | { |
|---|
| 39 | _done = true; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | virtual void specialKeyPress( Producer::KeyCharacter key ) |
|---|
| 43 | { |
|---|
| 44 | if (key==Producer::KeyChar_Escape) |
|---|
| 45 | shutdown(); |
|---|
| 46 | |
|---|
| 47 | _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | virtual void specialKeyRelease( Producer::KeyCharacter key ) |
|---|
| 51 | { |
|---|
| 52 | _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | virtual void keyPress( Producer::KeyCharacter key) |
|---|
| 56 | { |
|---|
| 57 | _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | virtual void keyRelease( Producer::KeyCharacter key) |
|---|
| 61 | { |
|---|
| 62 | _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | virtual void mouseMotion( float mx, float my ) |
|---|
| 66 | { |
|---|
| 67 | _eventQueue->mouseMotion( mx, my ); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | virtual void buttonPress( float mx, float my, unsigned int mbutton ) |
|---|
| 71 | { |
|---|
| 72 | _eventQueue->mouseButtonPress(mx, my, mbutton); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | virtual void buttonRelease( float mx, float my, unsigned int mbutton ) |
|---|
| 76 | { |
|---|
| 77 | _eventQueue->mouseButtonRelease(mx, my, mbutton); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | bool done() { return _done; } |
|---|
| 81 | |
|---|
| 82 | private: |
|---|
| 83 | |
|---|
| 84 | bool _done; |
|---|
| 85 | osg::ref_ptr<osgGA::EventQueue> _eventQueue; |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced |
|---|
| 89 | { |
|---|
| 90 | public: |
|---|
| 91 | |
|---|
| 92 | virtual void requestRedraw() {} |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | virtual void requestContinuousUpdate(bool =true) {} |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | virtual void requestWarpPointer(float ,float ) {} |
|---|
| 99 | |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | class CreateModelToSaveVisitor : public osg::NodeVisitor |
|---|
| 106 | { |
|---|
| 107 | public: |
|---|
| 108 | |
|---|
| 109 | CreateModelToSaveVisitor(): |
|---|
| 110 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 111 | { |
|---|
| 112 | _group = new osg::Group; |
|---|
| 113 | _addToModel = false; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | virtual void apply(osg::Node& node) |
|---|
| 117 | { |
|---|
| 118 | osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node); |
|---|
| 119 | if (scribe) |
|---|
| 120 | { |
|---|
| 121 | for(unsigned int i=0; i<scribe->getNumChildren(); ++i) |
|---|
| 122 | { |
|---|
| 123 | _group->addChild(scribe->getChild(i)); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | traverse(node); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | osg::ref_ptr<osg::Group> _group; |
|---|
| 133 | bool _addToModel; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | class PickHandler : public osgGA::GUIEventHandler { |
|---|
| 138 | public: |
|---|
| 139 | |
|---|
| 140 | PickHandler(osgUtil::SceneView* sceneView): |
|---|
| 141 | _sceneView(sceneView), |
|---|
| 142 | _mx(0.0),_my(0.0) {} |
|---|
| 143 | |
|---|
| 144 | ~PickHandler() {} |
|---|
| 145 | |
|---|
| 146 | bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 147 | { |
|---|
| 148 | switch(ea.getEventType()) |
|---|
| 149 | { |
|---|
| 150 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 151 | { |
|---|
| 152 | if (ea.getKey()=='s') |
|---|
| 153 | { |
|---|
| 154 | saveSelectedModel(); |
|---|
| 155 | } |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 159 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 160 | { |
|---|
| 161 | _mx = ea.getX(); |
|---|
| 162 | _my = ea.getY(); |
|---|
| 163 | return false; |
|---|
| 164 | } |
|---|
| 165 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 166 | { |
|---|
| 167 | if (_mx == ea.getX() && _my == ea.getY()) |
|---|
| 168 | { |
|---|
| 169 | |
|---|
| 170 | pick(ea); |
|---|
| 171 | } |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | default: |
|---|
| 176 | return false; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | void pick(const osgGA::GUIEventAdapter& ea) |
|---|
| 181 | { |
|---|
| 182 | |
|---|
| 183 | osg::Node* scene = _sceneView.valid() ? _sceneView->getSceneData() : 0; |
|---|
| 184 | if (!scene) return; |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f)); |
|---|
| 189 | float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f)); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 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 | |
|---|
| 203 | osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second; |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | osgUtil::Hit& hit = hits.front(); |
|---|
| 207 | |
|---|
| 208 | osg::NodePath& nodePath = hit._nodePath; |
|---|
| 209 | osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 210 | osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 211 | |
|---|
| 212 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | if (parent && node) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | std::cout<<" parent "<<parent->className()<<std::endl; |
|---|
| 219 | |
|---|
| 220 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
|---|
| 221 | if (!parentAsScribe) |
|---|
| 222 | { |
|---|
| 223 | |
|---|
| 224 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
|---|
| 225 | scribe->addChild(node); |
|---|
| 226 | parent->replaceChild(node,scribe); |
|---|
| 227 | } |
|---|
| 228 | else |
|---|
| 229 | { |
|---|
| 230 | |
|---|
| 231 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
|---|
| 232 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
|---|
| 233 | itr!=parentList.end(); |
|---|
| 234 | ++itr) |
|---|
| 235 | { |
|---|
| 236 | (*itr)->replaceChild(parentAsScribe,node); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | void saveSelectedModel() |
|---|
| 245 | { |
|---|
| 246 | CreateModelToSaveVisitor cmtsv; |
|---|
| 247 | _sceneView->getSceneData()->accept(cmtsv); |
|---|
| 248 | |
|---|
| 249 | if (cmtsv._group->getNumChildren()>0) |
|---|
| 250 | { |
|---|
| 251 | std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl; |
|---|
| 252 | osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg"); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | protected: |
|---|
| 257 | |
|---|
| 258 | osg::observer_ptr<osgUtil::SceneView> _sceneView; |
|---|
| 259 | float _mx,_my; |
|---|
| 260 | }; |
|---|
| 261 | |
|---|
| 262 | int main( int argc, char **argv ) |
|---|
| 263 | { |
|---|
| 264 | if (argc<2) |
|---|
| 265 | { |
|---|
| 266 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 267 | return 1; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 272 | if (!loadedModel) |
|---|
| 273 | { |
|---|
| 274 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 275 | return 1; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface; |
|---|
| 280 | renderSurface->setWindowName("osgkeyboardmouse"); |
|---|
| 281 | renderSurface->setWindowRectangle(100,100,800,600); |
|---|
| 282 | renderSurface->useBorder(true); |
|---|
| 283 | renderSurface->realize(); |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | osgGA::SimpleViewer viewer; |
|---|
| 288 | viewer.setSceneData(loadedModel.get()); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | osg::ref_ptr<Producer::KeyboardMouse> kbm = new Producer::KeyboardMouse(renderSurface.get()); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue()); |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | viewer.setCameraManipulator( new osgGA::TrackballManipulator ); |
|---|
| 300 | |
|---|
| 301 | osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; |
|---|
| 302 | statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet()); |
|---|
| 303 | viewer.addEventHandler(statesetManipulator.get()); |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | viewer.addEventHandler(new PickHandler(viewer.getSceneView())); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | viewer.getEventQueue()->getCurrentEventState()->setInputRange(-1.0, -1.0, 1.0, 1.0); |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); |
|---|
| 318 | |
|---|
| 319 | viewer.init(); |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | while( renderSurface->isRealized() && !kbmcb->done()) |
|---|
| 323 | { |
|---|
| 324 | |
|---|
| 325 | viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight(), false); |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | kbm->update( *kbmcb ); |
|---|
| 329 | |
|---|
| 330 | viewer.frame(); |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | renderSurface->swapBuffers(); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | return 0; |
|---|
| 337 | } |
|---|