| 93 | | osg::Node* createHUD() |
| 94 | | { |
| 95 | | osg::Geode* geode = new osg::Geode(); |
| 96 | | |
| 97 | | std::string timesFont("fonts/arial.ttf"); |
| 98 | | |
| 99 | | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
| 100 | | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
| 101 | | |
| 102 | | osgText::Text* text = new osgText::Text; |
| 103 | | geode->addDrawable( text ); |
| 104 | | |
| 105 | | osg::Vec3 position(50.0f,50.0f,0.0f); |
| 106 | | text->setPosition(position); |
| 107 | | text->setText("Use the Tab key to switch between the trackball and pick modes."); |
| 108 | | text->setFont(timesFont); |
| 109 | | |
| 110 | | osg::Camera* camera = new osg::Camera; |
| 111 | | |
| 112 | | // set the projection matrix |
| 113 | | camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
| 114 | | |
| 115 | | // set the view matrix |
| 116 | | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
| 117 | | camera->setViewMatrix(osg::Matrix::identity()); |
| 118 | | |
| 119 | | // only clear the depth buffer |
| 120 | | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
| 121 | | |
| 122 | | // draw subgraph after main camera view. |
| 123 | | camera->setRenderOrder(osg::Camera::POST_RENDER); |
| 124 | | |
| 125 | | camera->addChild(geode); |
| 126 | | |
| 127 | | return camera; |
| 128 | | } |
| 129 | | |
| 250 | | |
| 251 | | |
| 252 | | class PickModeHandler : public osgGA::GUIEventHandler |
| 253 | | { |
| 254 | | public: |
| 255 | | enum Modes |
| 256 | | { |
| 257 | | VIEW = 0, |
| 258 | | PICK |
| 259 | | }; |
| 260 | | |
| 261 | | PickModeHandler(): |
| 262 | | _mode(VIEW), |
| 263 | | _activeDragger(0) |
| 264 | | { |
| 265 | | } |
| 266 | | |
| 267 | | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, |
| 268 | | osg::Object*, osg::NodeVisitor*) |
| 269 | | { |
| 270 | | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
| 271 | | if (!view) return false; |
| 272 | | |
| 273 | | if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Tab && |
| 274 | | ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN && |
| 275 | | _activeDragger == 0) |
| 276 | | { |
| 277 | | _mode = ! _mode; |
| 278 | | } |
| 279 | | |
| 280 | | if (VIEW == _mode) return false; |
| 281 | | |
| 282 | | switch (ea.getEventType()) |
| 283 | | { |
| 284 | | case osgGA::GUIEventAdapter::PUSH: |
| 285 | | { |
| 286 | | osgUtil::LineSegmentIntersector::Intersections intersections; |
| 287 | | |
| 288 | | _pointer.reset(); |
| 289 | | |
| 290 | | if (view->computeIntersections(ea.getX(),ea.getY(),intersections)) |
| 291 | | { |
| 292 | | _pointer.setCamera(view->getCamera()); |
| 293 | | _pointer.setMousePosition(ea.getX(), ea.getY()); |
| 294 | | |
| 295 | | for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); |
| 296 | | hitr != intersections.end(); |
| 297 | | ++hitr) |
| 298 | | { |
| 299 | | _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint()); |
| 300 | | } |
| 301 | | for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin(); |
| 302 | | itr != _pointer._hitList.front().first.end(); |
| 303 | | ++itr) |
| 304 | | { |
| 305 | | osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr); |
| 306 | | if (dragger) |
| 307 | | { |
| 308 | | |
| 309 | | dragger->handle(_pointer, ea, aa); |
| 310 | | _activeDragger = dragger; |
| 311 | | break; |
| 312 | | } |
| 313 | | } |
| 314 | | } |
| 315 | | } |
| 316 | | case osgGA::GUIEventAdapter::DRAG: |
| 317 | | case osgGA::GUIEventAdapter::RELEASE: |
| 318 | | { |
| 319 | | if (_activeDragger) |
| 320 | | { |
| 321 | | _pointer._hitIter = _pointer._hitList.begin(); |
| 322 | | _pointer.setCamera(view->getCamera()); |
| 323 | | _pointer.setMousePosition(ea.getX(), ea.getY()); |
| 324 | | |
| 325 | | _activeDragger->handle(_pointer, ea, aa); |
| 326 | | } |
| 327 | | break; |
| 328 | | } |
| 329 | | default: |
| 330 | | break; |
| 331 | | } |
| 332 | | |
| 333 | | if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE) |
| 334 | | { |
| 335 | | _activeDragger = 0; |
| 336 | | _pointer.reset(); |
| 337 | | } |
| 338 | | |
| 339 | | return true; |
| 340 | | } |
| 341 | | |
| 342 | | private: |
| 343 | | unsigned int _mode; |
| 344 | | osgManipulator::Dragger* _activeDragger; |
| 345 | | osgManipulator::PointerInfo _pointer; |
| 346 | | }; |
| 347 | | |
| | 218 | // |