| 1 | |
|---|
| 2 | #include <osgDB/ReadFile> |
|---|
| 3 | #include <osgUtil/Optimizer> |
|---|
| 4 | #include <osgViewer/Viewer> |
|---|
| 5 | #include <osg/CoordinateSystemNode> |
|---|
| 6 | #include <osgText/Text> |
|---|
| 7 | |
|---|
| 8 | #include <osgManipulator/CommandManager> |
|---|
| 9 | #include <osgManipulator/TabBoxDragger> |
|---|
| 10 | #include <osgManipulator/TabPlaneDragger> |
|---|
| 11 | #include <osgManipulator/TabPlaneTrackballDragger> |
|---|
| 12 | #include <osgManipulator/TrackballDragger> |
|---|
| 13 | #include <osgManipulator/Translate1DDragger> |
|---|
| 14 | #include <osgManipulator/Translate2DDragger> |
|---|
| 15 | #include <osgManipulator/TranslateAxisDragger> |
|---|
| 16 | |
|---|
| 17 | #include <iostream> |
|---|
| 18 | |
|---|
| 19 | osgManipulator::Dragger* createDragger(const std::string& name) |
|---|
| 20 | { |
|---|
| 21 | osgManipulator::Dragger* dragger = 0; |
|---|
| 22 | if ("TabPlaneDragger" == name) |
|---|
| 23 | { |
|---|
| 24 | osgManipulator::TabPlaneDragger* d = new osgManipulator::TabPlaneDragger(); |
|---|
| 25 | d->setupDefaultGeometry(); |
|---|
| 26 | dragger = d; |
|---|
| 27 | } |
|---|
| 28 | else if ("TabPlaneTrackballDragger" == name) |
|---|
| 29 | { |
|---|
| 30 | osgManipulator::TabPlaneTrackballDragger* d = new osgManipulator::TabPlaneTrackballDragger(); |
|---|
| 31 | d->setupDefaultGeometry(); |
|---|
| 32 | dragger = d; |
|---|
| 33 | } |
|---|
| 34 | else if ("TrackballDragger" == name) |
|---|
| 35 | { |
|---|
| 36 | osgManipulator::TrackballDragger* d = new osgManipulator::TrackballDragger(); |
|---|
| 37 | d->setupDefaultGeometry(); |
|---|
| 38 | dragger = d; |
|---|
| 39 | } |
|---|
| 40 | else if ("Translate1DDragger" == name) |
|---|
| 41 | { |
|---|
| 42 | osgManipulator::Translate1DDragger* d = new osgManipulator::Translate1DDragger(); |
|---|
| 43 | d->setupDefaultGeometry(); |
|---|
| 44 | dragger = d; |
|---|
| 45 | } |
|---|
| 46 | else if ("Translate2DDragger" == name) |
|---|
| 47 | { |
|---|
| 48 | osgManipulator::Translate2DDragger* d = new osgManipulator::Translate2DDragger(); |
|---|
| 49 | d->setupDefaultGeometry(); |
|---|
| 50 | dragger = d; |
|---|
| 51 | } |
|---|
| 52 | else if ("TranslateAxisDragger" == name) |
|---|
| 53 | { |
|---|
| 54 | osgManipulator::TranslateAxisDragger* d = new osgManipulator::TranslateAxisDragger(); |
|---|
| 55 | d->setupDefaultGeometry(); |
|---|
| 56 | dragger = d; |
|---|
| 57 | } |
|---|
| 58 | else |
|---|
| 59 | { |
|---|
| 60 | osgManipulator::TabBoxDragger* d = new osgManipulator::TabBoxDragger(); |
|---|
| 61 | d->setupDefaultGeometry(); |
|---|
| 62 | dragger = d; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | return dragger; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | osg::Node* createHUD() |
|---|
| 69 | { |
|---|
| 70 | osg::Geode* geode = new osg::Geode(); |
|---|
| 71 | |
|---|
| 72 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 73 | |
|---|
| 74 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 75 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 76 | |
|---|
| 77 | osgText::Text* text = new osgText::Text; |
|---|
| 78 | geode->addDrawable( text ); |
|---|
| 79 | |
|---|
| 80 | osg::Vec3 position(50.0f,50.0f,0.0f); |
|---|
| 81 | text->setPosition(position); |
|---|
| 82 | text->setText("Use the Tab key to switch between the trackball and pick modes."); |
|---|
| 83 | text->setFont(timesFont); |
|---|
| 84 | |
|---|
| 85 | osg::Camera* camera = new osg::Camera; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 92 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 99 | |
|---|
| 100 | camera->addChild(geode); |
|---|
| 101 | |
|---|
| 102 | return camera; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | #if 0 |
|---|
| 106 | bool computePixelCoords(osgProducer::Viewer* viewer,float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y) |
|---|
| 107 | { |
|---|
| 108 | Producer::KeyboardMouse* km = viewer->getKeyboardMouse(); |
|---|
| 109 | if (!km) return false; |
|---|
| 110 | |
|---|
| 111 | if (cameraNum>=viewer->getNumberOfCameras()) return false; |
|---|
| 112 | |
|---|
| 113 | Producer::Camera* camera=viewer->getCamera(cameraNum); |
|---|
| 114 | Producer::RenderSurface* rs = camera->getRenderSurface(); |
|---|
| 115 | |
|---|
| 116 | Producer::InputArea* inputArea = km->getInputArea(); |
|---|
| 117 | if( inputArea != NULL ) |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | unsigned int i; |
|---|
| 121 | for(i=0;i<inputArea->getNumRenderSurfaces();++i) |
|---|
| 122 | { |
|---|
| 123 | if (inputArea->getRenderSurface(i)==rs) break; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if (i==inputArea->getNumRenderSurfaces()) return false; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | Producer::RenderSurface::InputRectangle ir = inputArea->getRenderSurface(i)->getInputRectangle(); |
|---|
| 131 | |
|---|
| 132 | float rx = (x-ir.left())/ir.width(); |
|---|
| 133 | float ry = (y-ir.bottom())/ir.height(); |
|---|
| 134 | |
|---|
| 135 | int wx, wy; |
|---|
| 136 | unsigned int w, h; |
|---|
| 137 | rs->getWindowRectangle( wx, wy, w, h ); |
|---|
| 138 | |
|---|
| 139 | pixel_x = ((float)w)* rx; |
|---|
| 140 | pixel_y = ((float)h)* ry; |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | float rx = (x+1.0f)*0.5f; |
|---|
| 145 | float ry = (y+1.0f)*0.5f; |
|---|
| 146 | |
|---|
| 147 | int wx, wy; |
|---|
| 148 | unsigned int w, h; |
|---|
| 149 | rs->getWindowRectangle( wx, wy, w, h ); |
|---|
| 150 | |
|---|
| 151 | pixel_x = ((float)w)* rx; |
|---|
| 152 | pixel_y = ((float)h)* ry; |
|---|
| 153 | } |
|---|
| 154 | return true; |
|---|
| 155 | } |
|---|
| 156 | #endif |
|---|
| 157 | |
|---|
| 158 | osg::Node* addDraggerToScene(osg::Node* scene, osgManipulator::CommandManager* cmdMgr, const std::string& name) |
|---|
| 159 | { |
|---|
| 160 | scene->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 161 | |
|---|
| 162 | osgManipulator::Selection* selection = new osgManipulator::Selection; |
|---|
| 163 | selection->addChild(scene); |
|---|
| 164 | |
|---|
| 165 | osgManipulator::Dragger* dragger = createDragger(name); |
|---|
| 166 | |
|---|
| 167 | osg::Group* root = new osg::Group; |
|---|
| 168 | root->addChild(dragger); |
|---|
| 169 | root->addChild(selection); |
|---|
| 170 | root->addChild(createHUD()); |
|---|
| 171 | |
|---|
| 172 | float scale = scene->getBound().radius() * 1.6; |
|---|
| 173 | dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) * |
|---|
| 174 | osg::Matrix::translate(scene->getBound().center())); |
|---|
| 175 | cmdMgr->connect(*dragger, *selection); |
|---|
| 176 | |
|---|
| 177 | return root; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | class PickModeHandler : public osgGA::GUIEventHandler |
|---|
| 181 | { |
|---|
| 182 | public: |
|---|
| 183 | enum Modes |
|---|
| 184 | { |
|---|
| 185 | VIEW = 0, |
|---|
| 186 | PICK |
|---|
| 187 | }; |
|---|
| 188 | |
|---|
| 189 | PickModeHandler(): |
|---|
| 190 | _mode(VIEW), |
|---|
| 191 | _activeDragger(0) |
|---|
| 192 | { |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, |
|---|
| 196 | osg::Object*, osg::NodeVisitor*) |
|---|
| 197 | { |
|---|
| 198 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 199 | if (!view) return false; |
|---|
| 200 | |
|---|
| 201 | if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Tab && |
|---|
| 202 | ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && |
|---|
| 203 | _activeDragger == 0) |
|---|
| 204 | { |
|---|
| 205 | _mode = ! _mode; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | if (VIEW == _mode) return false; |
|---|
| 209 | |
|---|
| 210 | switch (ea.getEventType()) |
|---|
| 211 | { |
|---|
| 212 | case osgGA::GUIEventAdapter::PUSH: |
|---|
| 213 | { |
|---|
| 214 | osgUtil::LineSegmentIntersector::Intersections intersections; |
|---|
| 215 | |
|---|
| 216 | _pointer.reset(); |
|---|
| 217 | |
|---|
| 218 | if (view->computeIntersections(ea.getX(),ea.getY(),intersections)) |
|---|
| 219 | { |
|---|
| 220 | _pointer.setCamera(view->getCamera()); |
|---|
| 221 | _pointer.setMousePosition(ea.getX(), ea.getY()); |
|---|
| 222 | |
|---|
| 223 | for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); |
|---|
| 224 | hitr != intersections.end(); |
|---|
| 225 | ++hitr) |
|---|
| 226 | { |
|---|
| 227 | _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint()); |
|---|
| 228 | } |
|---|
| 229 | for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin(); |
|---|
| 230 | itr != _pointer._hitList.front().first.end(); |
|---|
| 231 | ++itr) |
|---|
| 232 | { |
|---|
| 233 | osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr); |
|---|
| 234 | if (dragger) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | dragger->handle(_pointer, ea, aa); |
|---|
| 238 | _activeDragger = dragger; |
|---|
| 239 | break; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | case osgGA::GUIEventAdapter::DRAG: |
|---|
| 245 | case osgGA::GUIEventAdapter::RELEASE: |
|---|
| 246 | { |
|---|
| 247 | if (_activeDragger) |
|---|
| 248 | { |
|---|
| 249 | _pointer._hitIter = _pointer._hitList.begin(); |
|---|
| 250 | _pointer.setCamera(view->getCamera()); |
|---|
| 251 | _pointer.setMousePosition(ea.getX(), ea.getY()); |
|---|
| 252 | |
|---|
| 253 | _activeDragger->handle(_pointer, ea, aa); |
|---|
| 254 | } |
|---|
| 255 | break; |
|---|
| 256 | } |
|---|
| 257 | default: |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE) |
|---|
| 262 | { |
|---|
| 263 | _activeDragger = 0; |
|---|
| 264 | _pointer.reset(); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | return true; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | private: |
|---|
| 271 | unsigned int _mode; |
|---|
| 272 | osgManipulator::Dragger* _activeDragger; |
|---|
| 273 | osgManipulator::PointerInfo _pointer; |
|---|
| 274 | }; |
|---|
| 275 | |
|---|
| 276 | int main( int argc, char **argv ) |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 284 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 285 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 286 | arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad"); |
|---|
| 287 | arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField"); |
|---|
| 288 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); |
|---|
| 289 | arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available"); |
|---|
| 290 | arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available"); |
|---|
| 291 | arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings."); |
|---|
| 292 | |
|---|
| 293 | arguments.getApplicationUsage()->addCommandLineOption("--dragger <draggername>","Use the specified dragger for manipulation"); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | osgViewer::Viewer viewer; |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | bool helpAll = arguments.read("--help-all"); |
|---|
| 304 | unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) | |
|---|
| 305 | ((helpAll || arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) | |
|---|
| 306 | ((helpAll || arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 ); |
|---|
| 307 | if (helpType) |
|---|
| 308 | { |
|---|
| 309 | arguments.getApplicationUsage()->write(std::cout, helpType); |
|---|
| 310 | return 1; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | if (arguments.errors()) |
|---|
| 315 | { |
|---|
| 316 | arguments.writeErrorMessages(std::cout); |
|---|
| 317 | return 1; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | if (arguments.argc()<=1) |
|---|
| 321 | { |
|---|
| 322 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 323 | return 1; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | std::string dragger_name = "TabBoxDragger"; |
|---|
| 327 | arguments.read("--dragger", dragger_name); |
|---|
| 328 | |
|---|
| 329 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | if (!loadedModel) |
|---|
| 336 | { |
|---|
| 337 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 338 | return 1; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | if (arguments.errors()) |
|---|
| 346 | { |
|---|
| 347 | arguments.writeErrorMessages(std::cout); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | osg::Timer_t end_tick = osg::Timer::instance()->tick(); |
|---|
| 351 | |
|---|
| 352 | std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl; |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | osgUtil::Optimizer optimizer; |
|---|
| 357 | optimizer.optimize(loadedModel.get()); |
|---|
| 358 | |
|---|
| 359 | osg::ref_ptr<osgManipulator::CommandManager> cmdMgr = new osgManipulator::CommandManager; |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | viewer.setSceneData(addDraggerToScene(loadedModel.get(), cmdMgr.get(), dragger_name)); |
|---|
| 363 | |
|---|
| 364 | viewer.addEventHandler(new PickModeHandler()); |
|---|
| 365 | |
|---|
| 366 | return viewer.run(); |
|---|
| 367 | } |
|---|