|
最近写OSG代码发现一个很奇怪的问题:其中主要代码如下
- #include <osgDB/ReadFile>
- #include <osgViewer/Viewer>
- #include <osg/Node>
- #include <osg/MatrixTransform>
- #include <osgGA/MatrixManipulator>
- #include <osg/Geode>
- #include <osgText/Text>
- #include <osgViewer/Viewer>
- #include <osgDB/ReadFile>
- #include <osgText/Font>
- osgText::Text* t = new osgText::Text;
- /*******************************************************************************/
- //创建平面文字,即文字不随着场景的变化,一直显示在屏幕上
- void createHUDText(osg::Camera* ca,osgText::Text* text)
- {
- osg::Group* rootNode = new osg::Group;//新建Group节点
- osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");//读取字体文件
- osg::Geode* geode = new osg::Geode;//新建Geode节点
- rootNode->addChild(geode);
- float layoutCharacterSize = 50.0; //文字的大小
- osg::Vec4 layoutColor(0.0f,1.0f,0.0f,1.0f);
- text->setFont(font);
- text->setColor(layoutColor);
- text->setCharacterSize(150);
- text->setPosition(osg::Vec3(800,0,0.0f));
- text->setLayout(osgText::Text::LEFT_TO_RIGHT);
- text->setDataVariance(osg::Object::DYNAMIC);
- geode->addDrawable(text);
- ca->addChild(rootNode);
- }
- class UseEventHandler : public osgGA::GUIEventHandler
- {
- public:
- UseEventHandler()
- {
- btnLEFT=false;
- btnRIGHT=false;
- }
- virtual bool handle(const osgGA::GUIEventAdapter & ea,osgGA::GUIActionAdapter & aa)
- {
- switch (ea.getEventType())
- {
- case osgGA::GUIEventAdapter::KEYDOWN:
- {
- if (ea.getKey() == 0xFF51 ||ea.getKey () == 0x41||ea.getKey () == 0x61) //A0x61
- {
- btnLEFT=true;
- return true;
- }
- if (ea.getKey() == 0xFF53 ||ea.getKey () == 0x44||ea.getKey () == 0x64)//D
- {
- btnRIGHT=true;
- return true;
- }
- return false;
- case osgGA::GUIEventAdapter::KEYUP:
- if (ea.getKey() == 0xFF51 ||ea.getKey () == 0x41||ea.getKey () == 0x61) //A
- {
- btnLEFT=false;
- return true;
- }
- if (ea.getKey() == 0xFF53 ||ea.getKey () == 0x44||ea.getKey () == 0x64)//D
- {
- btnRIGHT=false;
- return true;
- }
- }
- break;
- default:
- break;
- }
- if (btnLEFT) //A
- t->setText("1234");
- if (btnRIGHT)//D
- t->setText("5678");
- }
- };
- void main()
- {
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Group> root = new osg::Group;
- osg::ref_ptr<osg::Node> tank = new osg::Node;
- osg::ref_ptr<osg::Node> terrain = new osg::Node;
- osg::Camera* camera = new osg::Camera;//新建Camera相机节点,用于显示静态的文字,不跟随场景变动
- camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- camera->setProjectionMatrixAsOrtho2D(0,1440,0,900);
- camera->setViewMatrix(osg::Matrix::identity());
- camera->setClearMask(GL_DEPTH_BUFFER_BIT);
- camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
- t->setText("radar");
- createHUDText(camera,t);
- root->addChild(camera);//将相机节点关联至场景的组节点
- terrain = osgDB::readNodeFile("simple.osg");
- tank = osgDB::readNodeFile("t72-tank_des.flt");
- root->addChild(tank);
- root->addChild(terrain);
- viewer.setSceneData(root.get());
- viewer.addEventHandler(new UseEventHandler());
- viewer.realize();
- viewer.run();
- }
复制代码
编译运行后没有错误,但是在操作时,如果长时间按 A D 按键(程序中设置的按键),会出现错误,自动退出程序,一直没有明白这是个什么问题
我用的是VS2008 OSG包是2.8.3
希望有人能解答下,小生谢谢了 |
|