transforms.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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* getOrCreateSelectionPoint();
  15. virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
  16. protected:
  17. osg::ref_ptr<osg::MatrixTransform> _selectionPoint;
  18. };
  19. osg::Node* PickHandler::getOrCreateSelectionPoint() {
  20. if (!_selectionPoint) {
  21. printf("Got here\n");
  22. }
  23. return _selectionPoint.get();
  24. }
  25. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  26. if (ea.getEventType() != osgGA::GUIEventAdapter::RELEASE || ea.getButton() != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON || !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL)) {
  27. return false;
  28. }
  29. osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
  30. if (viewer) {
  31. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  32. osgUtil::IntersectionVisitor iv(intersector.get());
  33. iv.setTraversalMask(~0x1);
  34. viewer->getCamera()->accept(iv);
  35. if (intersector->containsIntersections()) {
  36. osgUtil::LineSegmentIntersector::Intersection result = *(intersector->getIntersections().begin());
  37. printf("Found intersection at: %lf, %lf, %lf\n", result.localIntersectionPoint.x(), result.localIntersectionPoint.y(), result.localIntersectionPoint.z());
  38. printf("Intersection normals: %lf, %lf, %lf\n", result.localIntersectionNormal.x(), result.localIntersectionNormal.y(), result.localIntersectionNormal.z());
  39. }
  40. }
  41. return false;
  42. }
  43. int main(int argc, char** argv)
  44. {
  45. // Root node of the scene
  46. osg::ref_ptr<osg::Group> root = new osg::Group;
  47. // Create the viewer
  48. osgViewer::Viewer viewer;
  49. viewer.setSceneData(root);
  50. viewer.realize();
  51. // Add a small sphere
  52. osg::ref_ptr<osg::ShapeDrawable> mouseSphere = new osg::ShapeDrawable;
  53. mouseSphere->setShape(new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f), 1.0f));
  54. mouseSphere->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
  55. osg::ref_ptr<osg::Geode> mouseParent = new osg::Geode;
  56. mouseParent->addDrawable(mouseSphere.get());
  57. root->addChild(mouseParent);
  58. // Add axesNode under root
  59. osg::ref_ptr<osg::Node> axesNode = osgDB::readNodeFile("../../testdata/zPlate_0.stl");
  60. if (!axesNode)
  61. {
  62. printf("Origin node not loaded, model not found\n");
  63. return 1;
  64. }
  65. root->addChild(axesNode);
  66. // Attach a manipulator (it's usually done for us when we use viewer.run())
  67. osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
  68. viewer.setCameraManipulator(tm);
  69. osg::ref_ptr<PickHandler> picker = new PickHandler();
  70. viewer.addEventHandler(picker.get());
  71. return viewer.run();
  72. }