|
如下代码所示,基本解决了模型保存时纹理丢失问题,但在将附件中的数据导入场景后,保存前遍历模型发现纹理存在,将场景模型保存为osgb文件,导入系统中发现模型的纹理丢失,请问这是怎么回事?我需要再做哪些处理?谢谢!现附上测试数据,在此表示感谢!
void ViewRender::importSingleData( std::string filename )
{
//取得文件父路径
std::string m_filePath=osgDB::getFilePath(filename);
osgDB::FilePathList m_filePathList=osgDB::Registry::instance()->getDataFilePathList();
m_filePathList.push_back(m_filePath);//设置文件路径
osgDB::Registry::instance()->setDataFilePathList(m_filePathList);
mModel = osgDB::readNodeFile(filename);
mModel->setName(filename);
osgUtil::Optimizer optimizer;
optimizer.optimize(mModel.get());
optimizer.reset();
osgUtil::Optimizer::TextureVisitor tv(true,false, false,false, false,1.0);
mModel->accept(tv);
//添加孩子
mRoot->addChild(mModel.get());
}
//模型遍历函数
void vgTextureVisitor::apply( osg::StateSet& stateset )
{
//得到纹理属性列表
for(unsigned int i=0;i<stateset.getTextureAttributeList().size();++i)
{
osg::Texture* texture = dynamic_cast<osg::Texture*>(stateset.getTextureAttribute(i,osg::StateAttribute::TEXTURE));
if (texture)
{
//texture->setDataVariance(osg::Object::STATIC);
osg::Texture1D* texture1D = dynamic_cast<osg::Texture1D*>(texture);
osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(texture);
osg::Texture3D* texture3D = dynamic_cast<osg::Texture3D*>(texture);
//osg::ref_ptr<osg::Image> image = texture2D ? texture2D->getImage() : (texture3D ? texture3D->getImage() : 0);
osg::ref_ptr<osg::Image> image = texture2D ? texture2D->getImage() : (texture3D ? texture3D->getImage() :
(texture1D ? texture1D->getImage():0));
if (image==0)
{
std::cout<<"纹理丢失"<<std::endl;
texture->setUnRefImageDataAfterApply(false);
}
else if (image!=0 &&
(image->getPixelFormat()==GL_RGB || image->getPixelFormat()==GL_RGBA) &&
(image->s()>=32 && image->t()>=32)
)
{
std::cout<<"纹理存在!!!!!!"<<std::endl;
_imageList.insert(std::make_pair(image->getFileName(),image));
}
}
}
}
//模型保存命令
void ViewRender::OutputModelData( std::string _filePath )
{
std::string m_filePath=osgDB::getFilePath(_filePath);
osg::ref_ptr<osg::Node> mNode=mViewer->getSceneData()->asGroup();
//创建纹理访问器
vgTextureVisitor textureTV;
mNode->accept(textureTV);
osgDB::ReaderWriter::WriteResult writeresult=osgDB::Registry::instance()
->writeNode(*mNode,_filePath,osgDB::Registry::instance()->getOptions());
if (writeresult.success())
{
osg::notify(osg::NOTICE)<<"保存成功!"<<std::endl;
}
}
测试数据:
模型测试数据.7z
(213.36 KB, 下载次数: 2)
|
|