查看: 1134|回复: 2

关于OSG显示变化的数字的问题

[复制链接]

该用户从未签到

发表于 2011-6-9 22:08:53 | 显示全部楼层 |阅读模式
最近写OSG代码发现一个很奇怪的问题:其中主要代码如下
  1. #include <osgDB/ReadFile>
  2. #include <osgViewer/Viewer>
  3. #include <osg/Node>
  4. #include <osg/MatrixTransform>
  5. #include <osgGA/MatrixManipulator>

  6. #include <osg/Geode>
  7. #include <osgText/Text>
  8. #include <osgViewer/Viewer>
  9. #include <osgDB/ReadFile>
  10. #include <osgText/Font>

  11. osgText::Text* t = new osgText::Text;

  12. /*******************************************************************************/
  13. //创建平面文字,即文字不随着场景的变化,一直显示在屏幕上
  14. void createHUDText(osg::Camera* ca,osgText::Text* text)
  15. {
  16. osg::Group* rootNode = new osg::Group;//新建Group节点

  17. osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");//读取字体文件

  18. osg::Geode* geode = new osg::Geode;//新建Geode节点

  19. rootNode->addChild(geode);

  20. float layoutCharacterSize = 50.0; //文字的大小

  21. osg::Vec4 layoutColor(0.0f,1.0f,0.0f,1.0f);
  22. text->setFont(font);
  23. text->setColor(layoutColor);
  24. text->setCharacterSize(150);
  25. text->setPosition(osg::Vec3(800,0,0.0f));
  26. text->setLayout(osgText::Text::LEFT_TO_RIGHT);
  27. text->setDataVariance(osg::Object::DYNAMIC);

  28. geode->addDrawable(text);

  29. ca->addChild(rootNode);
  30. }


  31. class UseEventHandler : public osgGA::GUIEventHandler
  32. {
  33. public:
  34. UseEventHandler()
  35. {
  36. btnLEFT=false;
  37. btnRIGHT=false;
  38. }
  39. virtual bool handle(const osgGA::GUIEventAdapter & ea,osgGA::GUIActionAdapter & aa)
  40. {
  41. switch (ea.getEventType())
  42. {
  43. case osgGA::GUIEventAdapter::KEYDOWN:
  44. {
  45. if (ea.getKey() == 0xFF51 ||ea.getKey () == 0x41||ea.getKey () == 0x61) //A0x61
  46. {
  47. btnLEFT=true;
  48. return true;
  49. }

  50. if (ea.getKey() == 0xFF53 ||ea.getKey () == 0x44||ea.getKey () == 0x64)//D
  51. {
  52. btnRIGHT=true;
  53. return true;
  54. }
  55. return false;
  56. case osgGA::GUIEventAdapter::KEYUP:
  57. if (ea.getKey() == 0xFF51 ||ea.getKey () == 0x41||ea.getKey () == 0x61) //A
  58. {
  59. btnLEFT=false;
  60. return true;
  61. }

  62. if (ea.getKey() == 0xFF53 ||ea.getKey () == 0x44||ea.getKey () == 0x64)//D
  63. {
  64. btnRIGHT=false;
  65. return true;
  66. }
  67. }
  68. break;
  69. default:
  70. break;
  71. }

  72. if (btnLEFT) //A
  73. t->setText("1234");

  74. if (btnRIGHT)//D
  75. t->setText("5678");
  76. }

  77. };

  78. void main()
  79. {
  80. osgViewer::Viewer viewer;

  81. osg::ref_ptr<osg::Group> root = new osg::Group;

  82. osg::ref_ptr<osg::Node> tank = new osg::Node;

  83. osg::ref_ptr<osg::Node> terrain = new osg::Node;

  84. osg::Camera* camera = new osg::Camera;//新建Camera相机节点,用于显示静态的文字,不跟随场景变动

  85. camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  86. camera->setProjectionMatrixAsOrtho2D(0,1440,0,900);
  87. camera->setViewMatrix(osg::Matrix::identity());
  88. camera->setClearMask(GL_DEPTH_BUFFER_BIT);
  89. camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

  90. t->setText("radar");
  91. createHUDText(camera,t);

  92. root->addChild(camera);//将相机节点关联至场景的组节点

  93. terrain = osgDB::readNodeFile("simple.osg");

  94. tank = osgDB::readNodeFile("t72-tank_des.flt");

  95. root->addChild(tank);
  96. root->addChild(terrain);

  97. viewer.setSceneData(root.get());
  98. viewer.addEventHandler(new UseEventHandler());
  99. viewer.realize();
  100. viewer.run();
  101. }
复制代码




编译运行后没有错误,但是在操作时,如果长时间按  A  D 按键(程序中设置的按键),会出现错误,自动退出程序,一直没有明白这是个什么问题
1NA]X0VT7UBBSW]DAVDGEZ1.jpg
我用的是VS2008  OSG包是2.8.3
希望有人能解答下,小生谢谢了

该用户从未签到

发表于 2011-6-10 08:46:06 | 显示全部楼层
我按照您说的方式运行了一段时间,没有出现问题

该用户从未签到

发表于 2011-6-10 15:39:27 | 显示全部楼层
运行操作后没出现问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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