transforms.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <osgViewer/Viewer>
  2. #include <osg/ShapeDrawable>
  3. #include <osg/Geode>
  4. #include <osgDB/ReadFile>
  5. #include <osg/Group>
  6. #include <osg/MatrixTransform>
  7. #include <osg/Matrix>
  8. #include <osgGA/TrackballManipulator>
  9. #include <osgUtil/LineSegmentIntersector>
  10. #include <osgUtil/IntersectionVisitor>
  11. #include <osg/PolygonMode>
  12. class PickHandler: public osgGA::GUIEventHandler {
  13. public:
  14. osg::Node* getOrCreateSelectionCylinder();
  15. virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
  16. protected:
  17. osg::ref_ptr<osg::MatrixTransform> _selectionCylinder;
  18. };
  19. osg::Node* PickHandler::getOrCreateSelectionCylinder() {
  20. if (!_selectionCylinder) {
  21. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  22. geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f)));
  23. _selectionCylinder = new osg::MatrixTransform;
  24. _selectionCylinder->setNodeMask(0.1);
  25. _selectionCylinder->addChild(geode.get());
  26. osg::StateSet* ss = _selectionCylinder->getOrCreateStateSet();
  27. ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
  28. ss->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE));
  29. }
  30. return _selectionCylinder.get();
  31. }
  32. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  33. if (ea.getEventType() != osgGA::GUIEventAdapter::RELEASE || ea.getButton() != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON || !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL)) {
  34. return false;
  35. }
  36. osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
  37. if (viewer) {
  38. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  39. osgUtil::IntersectionVisitor iv(intersector.get());
  40. iv.setTraversalMask(~0x1);
  41. viewer->getCamera()->accept(iv);
  42. if (intersector->containsIntersections()) {
  43. osgUtil::LineSegmentIntersector::Intersection result = *(intersector->getIntersections().begin());
  44. printf("Found intersection at: %lf, %lf, %lf\n", result.localIntersectionPoint.x(), result.localIntersectionPoint.y(), result.localIntersectionPoint.z());
  45. printf("Intersection normals: %lf, %lf, %lf\n", result.localIntersectionNormal.x(), result.localIntersectionNormal.y(), result.localIntersectionNormal.z());
  46. }
  47. }
  48. return false;
  49. }
  50. int main(int argc, char** argv) {
  51. // Root node of the scene
  52. osg::ref_ptr<osg::Group> root = new osg::Group;
  53. // Create the viewer
  54. osgViewer::Viewer viewer;
  55. viewer.setSceneData(root);
  56. viewer.realize();
  57. // Add a small sphere
  58. osg::ref_ptr<osg::ShapeDrawable> mouseSphere = new osg::ShapeDrawable;
  59. mouseSphere->setShape(new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f), 1.0f));
  60. mouseSphere->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
  61. osg::ref_ptr<osg::Geode> mouseParent = new osg::Geode;
  62. mouseParent->addDrawable(mouseSphere.get());
  63. root->addChild(mouseParent);
  64. // Add axesNode under root
  65. osg::ref_ptr<osg::Node> axesNode = osgDB::readNodeFile("../../testdata/zPlate_0.stl");
  66. if (!axesNode) {
  67. printf("Origin node not loaded, model not found\n");
  68. return 1;
  69. }
  70. root->addChild(axesNode);
  71. // Attach a manipulator (it's usually done for us when we use viewer.run())
  72. osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
  73. viewer.setCameraManipulator(tm);
  74. osg::ref_ptr<PickHandler> picker = new PickHandler();
  75. root->addChild(picker->getOrCreateSelectionCylinder());
  76. viewer.addEventHandler(picker.get());
  77. return viewer.run();
  78. }