当前位置:   article > 正文

osg节点拾取_osgfx::scribe怎么使用

osgfx::scribe怎么使用

                      欢迎关注公众号可以查看更多完整文章

使用Scribe特效实现白色轮廓,此类在osgFx模块里面。

所有场景中的节点全部添加Scribe特效。

通过继承GUIEventHandler来自定义鼠标对应动作时,需要进行何种操作。

在事件处理器类中,实现左键时判断鼠标点击位置是否和节点相交,然后隐藏特效;

右键时恢复特效。

判断是否相交,使用computeIntersections来计算,这是osgViewer的函数。

完整代码:

  1. #include <osg/Group>
  2. #include <osgViewer/Viewer>
  3. #include <osgGA/GUIEventHandler>
  4. #include <osg/ShapeDrawable>
  5. #include <osgFX/Scribe>
  6. #include <osg/Node>
  7. #include <osgDB/ReadFile>
  8. #pragma comment(lib,"osgd.lib")
  9. #pragma comment(lib,"osgviewerd.lib")
  10. #pragma comment(lib,"osgFXd.lib")
  11. #pragma comment(lib,"osggad.lib")
  12. #pragma comment(lib,"osgUtild.lib")
  13. #pragma comment(lib,"osgdbd.lib")
  14. class CEventHandler :public osgGA::GUIEventHandler
  15. {
  16. public:
  17. CEventHandler(osgViewer::Viewer *viewer) :m_pThis(viewer){};
  18. virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& ga)
  19. {
  20. switch (ea.getEventType())
  21. {
  22. case osgGA::GUIEventAdapter::PUSH:
  23. Pick(ea.getX(), ea.getY(), ea.getButton());
  24. break;
  25. default:
  26. break;
  27. }
  28. return true;
  29. }
  30. protected:
  31. void Pick(float x, float y,int button)
  32. {
  33. osgUtil::LineSegmentIntersector::Intersections datas;
  34. if (button == 1 && m_pThis->computeIntersections(x, y, datas))//左键隐藏
  35. {
  36. for (auto it = datas.begin(); it != datas.end();it++)
  37. {
  38. osg::NodePath nodePath = it->nodePath;
  39. for (int i = 0; i < nodePath.size(); i++)
  40. {
  41. osgFX::Scribe *sc = dynamic_cast<osgFX::Scribe*>(nodePath[i]);
  42. if (sc)
  43. {
  44. auto it = std::find(m_scribeList.begin(), m_scribeList.end(), sc);
  45. if (it != m_scribeList.end())
  46. continue;
  47. sc->setNodeMask(0);
  48. m_scribeList.push_back(sc);
  49. }
  50. }
  51. }
  52. }
  53. else if (button == 4)//右键显示
  54. {
  55. for (auto it = m_scribeList.begin(); it != m_scribeList.end();it++)
  56. it->get()->setNodeMask(1);
  57. m_scribeList.clear();
  58. }
  59. }
  60. osgViewer::Viewer* m_pThis;
  61. osg::NodeList m_scribeList;
  62. };
  63. int main(int argc, char **argv)
  64. {
  65. osgViewer::Viewer viewer;
  66. osg::ref_ptr<osg::Group> group = new osg::Group;
  67. osg::ref_ptr<osg::ShapeDrawable> coneDraw = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(10.f, 0.f, 0.f), 2.f, 40.f));
  68. osg::ref_ptr<osg::ShapeDrawable> sphereDraw = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-10.f, 0.f, 0.f), 2.f));
  69. osg::ref_ptr<osgFX::Scribe> scribe1 = new osgFX::Scribe;
  70. osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("cessna.osgt");
  71. //scribe1->setNodeMask(0);
  72. scribe1->addChild(node1);
  73. osg::ref_ptr<osgFX::Scribe> scribe2 = new osgFX::Scribe;
  74. osg::ref_ptr<osg::Node> node2 = osgDB::readNodeFile("cow.osgt");
  75. //scribe2->setNodeMask(0);
  76. scribe2->addChild(node2);
  77. group->addChild(node1);
  78. group->addChild(node2);
  79. group->addChild(scribe1);
  80. group->addChild(scribe2);
  81. viewer.setSceneData(group);
  82. viewer.addEventHandler(new CEventHandler(&viewer));
  83. viewer.realize();
  84. return viewer.run();
  85. }


本文地址:osg节点拾取_GreenArrowMan-CSDN博客-CSDN博客

源码下载:osg节点选择-C++代码类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/314981
推荐阅读
相关标签
  

闽ICP备14008679号