查看: 2387|回复: 2

osgART中,KeyboardEventHandler无响应

[复制链接]

该用户从未签到

发表于 2012-4-24 22:40:18 | 显示全部楼层 |阅读模式
我是一个初学者,最近刚把osgART2.0 RC3编译成功,折腾了几天也把自己从solidworks提取的模型用osgART给跑起来了。
但是想用键盘来控制模型的显示大小,使用了GUIEventHandler,但是一直没成功,把代码贴上来大家指导指导。

  1. #include <osg/Geometry>
  2. #include <osg/Geode>
  3. #include <osg/MatrixTransform>
  4. #include <osg/ref_ptr>
  5. #include <osg/PrimitiveSet>
  6. #include <osgViewer/Viewer>
  7. #include <osgViewer/ViewerEventHandlers>
  8. #include <osgDB/FileUtils>

  9. #include <osgART/Foundation>
  10. #include <osgART/VideoLayer>
  11. #include <osgART/PluginManager>
  12. #include <osgART/VideoGeode>
  13. #include <osgART/Utils>
  14. #include <osgART/GeometryUtils>
  15. #include <osgART/MarkerCallback>
  16. #include <osgART/TransformFilterCallback>

  17. #include "XMLReader.h"
  18. #include "FileReader.h"
  19. #include "../VAARDataModel/DataModel.h"
  20. #include "../VAARDataModel/Component.h"


  21. osg::Geometry* CreatGeometry(
  22.         vaar_data::Component* component,
  23.         const osg::Vec4f color
  24. ) {
  25.         osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
  26.        
  27.         geometry->setVertexArray(component->GetRefPtrTriangles().get());
  28.        
  29.         geometry->setNormalArray(component->GetRefPtrNormals().get());
  30.         geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);

  31.         osg::ref_ptr<osg::Vec4Array> color_array = new osg::Vec4Array;
  32.         color_array->push_back(color);
  33.         geometry->setColorArray(color_array.get());
  34.         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);

  35.         geometry->addPrimitiveSet(
  36.                 new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, component->GetRefPtrTriangles()->size())
  37.         );

  38.         return geometry.release();
  39. } // CreatGeometry

  40. osg::Group* CreateImageBackground(osg::Image* video) {
  41.         osgART::VideoLayer* _layer = new osgART::VideoLayer();
  42.         _layer->setSize(*video);
  43.         osgART::VideoGeode* _geode = new osgART::VideoGeode(osgART::VideoGeode::USE_TEXTURE_RECTANGLE, video);
  44.         addTexturedQuad(*_geode,video->s(),video->t());
  45.         _layer->addChild(_geode);
  46.         return _layer;
  47. } // CreateImageBackground

  48. class KeyboardEventHandler : public osgGA::GUIEventHandler {
  49. public:
  50.         // Constructor
  51.         KeyboardEventHandler(osg::MatrixTransform *matrix) : osgGA::GUIEventHandler(){
  52.                 _scale_matrix = matrix;
  53.         } // constructor

  54.         virtual bool handle(
  55.                 const osgGA::GUIEventAdapter &event_adapter,
  56.                 const osgGA::GUIActionAdapter &acation_adapter
  57.         ) {
  58.                 osg::notify(osg::NOTICE) << "Handling" << std::endl;
  59.                 switch(event_adapter.getEventType()) {
  60.                         case osgGA::GUIEventAdapter::KEYDOWN: {
  61.                                 switch(event_adapter.getKey()) {
  62.                                         case osgGA::GUIEventAdapter::KEY_J:
  63.                                                 osg::notify(osg::NOTICE) << "J pressed" << std::endl;
  64.                                                 _scale_matrix->setMatrix(osg::Matrix::scale(0.5, 0.5, 0.5));
  65.                                                 return true;
  66.                                         case osgGA::GUIEventAdapter::KEY_K:
  67.                                                 osg::notify(osg::NOTICE) << "K pressed" << std::endl;
  68.                                                 _scale_matrix->setMatrix(osg::Matrix::scale(2.0, 2.0, 2.0));
  69.                                                 return true;
  70.                                 }
  71.                         }
  72.                         default:
  73.                                 return false;
  74.                 }
  75.         } // handle

  76. private:
  77.         osg::ref_ptr<osg::MatrixTransform> _scale_matrix;
  78. }; // KeyboardEventHandler

  79. void Run(vaar_data::DataModel& data_model) {
  80.         // create a root node
  81.         osg::ref_ptr<osg::Group> root = new osg::Group;
  82.         osg::ref_ptr<osg::MatrixTransform> scale_matrix = new osg::MatrixTransform();
  83.         osgViewer::Viewer viewer;

  84.         // attach root node to the viewer
  85.         viewer.setSceneData(root.get());

  86.         // add relevant handlers to the viewer
  87.         viewer.addEventHandler(new KeyboardEventHandler(scale_matrix.get()));
  88.         viewer.addEventHandler(new osgViewer::StatsHandler);
  89.         viewer.addEventHandler(new osgViewer::WindowSizeHandler);
  90.         viewer.addEventHandler(new osgViewer::ThreadingHandler);
  91.         viewer.addEventHandler(new osgViewer::HelpHandler);

  92.         // preload the video and tracker
  93.         int _video_id = osgART::PluginManager::instance()->load("osgart_video_artoolkit2");
  94.         //int _tracker_id = osgART::PluginManager::instance()->load("osgart_tracker_sstt");
  95.         int _tracker_id = osgART::PluginManager::instance()->load("osgart_tracker_artoolkit2");

  96.         // Load a video plugin.
  97.         osg::ref_ptr<osgART::Video> video =
  98.                 dynamic_cast<osgART::Video*>(osgART::PluginManager::instance()->get(_video_id));

  99.         // check if an instance of the video stream could be started
  100.         if (!video.valid()) {   
  101.                 // Without video an AR application can not work. Quit if none found.
  102.                 osg::notify(osg::FATAL) << "Could not initialize video plugin!" << std::endl;
  103.                 exit(-1);
  104.         }

  105.         // Open the video. This will not yet start the video stream but will
  106.         // get information about the format of the video which is essential
  107.         // for the connected tracker
  108.         video->open();

  109.         osg::ref_ptr<osgART::Tracker> tracker =
  110.                 dynamic_cast<osgART::Tracker*>(osgART::PluginManager::instance()->get(_tracker_id));

  111.         if (!tracker.valid()) {
  112.                 // Without tracker an AR application can not work. Quit if none found.
  113.                 osg::notify(osg::FATAL) << "Could not initialize tracker plugin!" << std::endl;
  114.                 exit(-1);
  115.         }

  116.         // get the tracker calibration object
  117.         osg::ref_ptr<osgART::Calibration> calibration = tracker->getOrCreateCalibration();

  118.         // load a calibration file
  119.         if (!calibration->load(std::string("data/camera_para.dat"))) {
  120.                 // the calibration file was non-existing or couldnt be loaded
  121.                 osg::notify(osg::FATAL) << "Non existing or incompatible calibration file" << std::endl;
  122.                 exit(-1);
  123.         }

  124.         // set the image source for the tracker
  125.         tracker->setImage(video.get());

  126.         osgART::TrackerCallback::addOrSet(root.get(), tracker.get());

  127.         osg::ref_ptr<osgART::Marker> marker_1 = tracker->addMarker("single;data/patt.hiro;80;0;0");
  128.         osg::ref_ptr<osgART::Marker> marker_2 = tracker->addMarker("single;data/patt.kanji;80;0;0");
  129.         //if (!marker_1.valid() || !marker_2->valid())
  130.         if (!marker_1.valid()) {
  131.                 // Without marker an AR application can not work. Quit if none found.
  132.                 osg::notify(osg::FATAL) << "Could not add marker_1!" << std::endl;
  133.                 exit(-1);
  134.         }
  135.         if (!marker_2.valid()) {
  136.                 // Without marker an AR application can not work. Quit if none found.
  137.                 osg::notify(osg::FATAL) << "Could not add marker_2!" << std::endl;
  138.                 exit(-1);
  139.         }

  140.         marker_1->setActive(true);
  141.         marker_2->setActive(true);

  142.         osg::ref_ptr<osg::MatrixTransform> marker_trans_1 = new osg::MatrixTransform();
  143.         osgART::attachDefaultEventCallbacks(marker_trans_1.get(), marker_1.get());
  144.         osg::ref_ptr<osg::MatrixTransform> marker_trans_2 = new osg::MatrixTransform();
  145.         osgART::attachDefaultEventCallbacks(marker_trans_2.get(), marker_2.get());

  146.         osg::ref_ptr<osg::Geode> marker_geode_1 = new osg::Geode;
  147.         osg::ref_ptr<osg::Geode> marker_geode_2 = new osg::Geode;
  148.         vaar_data::Component* component = data_model.GetRoot();
  149.         marker_geode_1->addDrawable(CreatGeometry(component->GetSubComponents()->at(0), osg::Vec4f(1.0f, 0.0f, 0.0f, 1.0f)));
  150.         marker_geode_2->addDrawable(CreatGeometry(component->GetSubComponents()->at(1), osg::Vec4f(0.0f, 0.0f, 0.8f, 1.0f)));
  151.         marker_trans_1->addChild(marker_geode_1.get());
  152.         marker_trans_1->getOrCreateStateSet()->setRenderBinDetails(100, "RenderBin");
  153.         marker_trans_2->addChild(marker_geode_2.get());
  154.         marker_trans_2->getOrCreateStateSet()->setRenderBinDetails(100, "RenderBin");

  155.         osg::ref_ptr<osg::Group> videoBackground = CreateImageBackground(video.get());
  156.         videoBackground->getOrCreateStateSet()->setRenderBinDetails(0, "RenderBin");

  157.         osg::ref_ptr<osg::Camera> cam = calibration->createCamera();
  158.         scale_matrix->addChild(marker_trans_1.get());
  159.         scale_matrix->addChild(marker_trans_2.get());
  160.         cam->addChild(scale_matrix.get());
  161.         cam->addChild(videoBackground.get());

  162.         root->addChild(cam.get());

  163.         video->start();
  164.         viewer.run();
  165.         video->close();
  166. }

  167. int main() {
  168.         vaar_data::DataModel* data_model = new vaar_data::DataModel();
  169.         vaar_file::FileReader* file_reader = new vaar_file::XMLReader;
  170.        
  171.         file_reader->Read("tutor.xml", *data_model);
  172.         if (NULL != file_reader)
  173.                 delete file_reader;

  174.         Run(*data_model);

  175.         if (NULL != data_model)
  176.                 delete data_model;
  177.        
  178.         return 0;
  179. }
复制代码

该用户从未签到

 楼主| 发表于 2012-4-25 10:53:36 | 显示全部楼层
自己解决了,把
virtual bool handle(
                const osgGA::GUIEventAdapter &event_adapter,
                const osgGA::GUIActionAdapter &acation_adapter
)
改为
virtual bool handle(
                const osgGA::GUIEventAdapter &event_adapter,
                osgGA::GUIActionAdapter &acation_adapter
)

该用户从未签到

发表于 2013-7-25 09:36:59 | 显示全部楼层
请问楼主,为什么我使用您的108行和110行的代码返回的值是-1呢?就是说找不到osgart_tracker_artoolkit2.dll和osgart_video_artoolkit2.dll这两个dlll。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表