|
1、像如下直接导出的话,是没问题的
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
osg::ref_ptr<osg::Group>root = new osg::Group;
osg::ref_ptr<osg::Node>node = new osg::Node;
node = osgDB::readNodeFile("jiangnanan.ive");
root->addChild(node);
viewer->setSceneData(root);
osgDB::writeNodeFile(*(node.get()), “jiangnananWithTrees.ive”);
viewer->realize();
return viewer->run();
}
2、但是我把导出函数写到事件类中,然后添加事件导出时,纹理就会丢失。
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Group>root = new osg::Group;
osg::ref_ptr<osg::Node>node = new osg::Node;
node = osgDB::readNodeFile("jiangnanan.ive");
root1->addChild(node);
viewer->setSceneData(root1);
viewer->addEventHandler(new SaveCurrentScene("jiangnananWithTrees.ive",node));
//光照效果
viewer->realize();
return viewer->run();
}
类定义如下:按空格键是将节点导出
class SaveCurrentScene :public osgGA::GUIEventHandler
{
public:
SaveCurrentScene(std::string filename,osg::ref_ptr<osg::Node>node) :
_filename(filename),
_node(node)
{
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa)
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (!viewer)
return false;
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Space)
{
bool isWriteSuccess;
isWriteSuccess = osgDB::writeNodeFile(*(_node.get()), _filename);
if (isWriteSuccess)
std::cout << "current scene writes to file successfully!" << std::endl;
else
std::cout << "current scene writes to file unsuccessfully!" << std::endl;
return true;
}
break;
default:
break;
}
return false;
}
private:
std::string _filename;
osg::ref_ptr<osg::Node>_node;
}; |
|