• 使用osg::TriangleFunction仿函数求交


    class PickEvent : public osgGA::GUIEventHandler
    {
    public:
     PickEvent(osg::Geometry* drawable) : _drawable(drawable){}
     ~PickEvent(){}
    public:
     bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
     {
      if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH)
      {
         osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
          double x = ea.getX();
          double y = ea.getY();

       osg::Matrix mat = viewer->getCamera()->getViewMatrix() * viewer->getCamera()->getProjectionMatrix();
       mat = mat  * viewer->getCamera()->getViewport()->computeWindowMatrix();
       osg::Matrix inversemat = osg::Matrix::inverse(mat);
       osg::Vec3d v3start = osg::Vec3d(ea.getX(), ea.getY(), 0.0) * inversemat;
       osg::Vec3d v3end = osg::Vec3d(ea.getX(), ea.getY(), 1.0) * inversemat;

       
       osg::TriangleFunctor<MyTestPicker::TriangleIntersector> intersector;
       intersector.set(v3start, v3end);
                _drawable->accept(intersector);

       MyTestPicker::TriangleIntersections::iterator it = intersector._intersections.begin();
       std::pair<const float,MyTestPicker::TriangleIntersection> ps = *it;
       osg::Vec3d v1 =  *(ps.second._v1);
       osg::Vec3d v2 = *(ps.second._v2);
       osg::Vec3d v3 = *(ps.second._v3);

      
       osg::Sphere* sphere = new osg::Sphere;
       sphere->setCenter(v1);
       sphere->setRadius(1);

      
       osg::ShapeDrawable* shapeDrawable = new osg::ShapeDrawable(sphere);
       osg::Geode* geode = new osg::Geode;
       geode->addDrawable(shapeDrawable);

       osg::Camera* camera = viewer->getCamera();
       camera->addChild(geode);

      }
      return false;
      
     }
    public:
     osg::Transform* _transform;
     osg::Geometry* _drawable;

    };

    void main()

    {

         osgViewer::Viewer viewer;

        osg::Node* node = osgDB::readNodeFile("D://cow.osg");

        osg::Group* group = dynamic_cast<osg::Group*>(node);
        osg::Node* child = group->getChild(0);
        osg::Geode* geode = dynamic_cast<osg::Geode*>(child);
        osg::Geometry* drawable = (osg::Geometry*)geode->getDrawable(0);

        viewer.addEventHandler(new PickEvent(drawable));
        viewer.setSceneData(group);
       
        return viewer.run();
    }

    }

  • 相关阅读:
    Irrlicht_0.1源码学习(3)—Irrlicht.cpp & include/Irrlicht.h
    Irrlicht_0.1源码学习(2)—引擎目录结构
    Irrlicht_0.1源码学习(1)—Welcome to the Irrlicht Engine
    Visual Studio 2013 编译时 "error LNK2026:模块对于 SAFESEH 映像是不安全的" 解决方案
    Windows平台下Lua环境的搭建
    系统调用与API
    前端学习技巧分享
    简单的bootstarp项目实例
    js显示表单的提交验证
    拷贝一张图片,从一个目录到另外一个目录下(PS:是拷贝是不是移动)
  • 原文地址:https://www.cnblogs.com/lizhengjin/p/1785643.html
Copyright © 2020-2023  润新知