|
楼主 |
发表于 2012-8-23 14:19:21
|
显示全部楼层
liuzhiyu123 发表于 2012-8-23 13:51
1.Dynamic
2.Dirty
貌似不行,我的代码是这样的。在osgviewerQT头文件中:
QString mStringModel;
QStringList mStringTextures;
QTimer mTimeChangeTexture;
osg::Group* mRoot;
//osg::Image* mImage;////反复读nodefile,开销大
QVector<osg::Image*> mImage;
osg::Node *mNode;
osg::Texture2D *mTexture2D;
osg::StateSet *mStateset;
int mCountTextures;//用于计数,指示设置的是第几个纹理
构造函数中代码:
mRoot=new osg::Group();
mTexture2D=new osg::Texture2D();
mTexture2D->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST);
mTexture2D->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
mTexture2D->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
mTexture2D->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
mTexture2D->setDataVariance(osg::Object:YNAMIC);
//mImage=new osg::Image;
mStateset=new osg::StateSet();
mNode=new osg::Node();
mCountTextures=0;
connect(&mTimeChangeTexture,SIGNAL(timeout()),this,SLOT(ResetTexture()));
首先装载一个模型:
mStringModel = QFileDialog::getOpenFileName(this, tr("Load Scene Model File"), "", tr("ive(*.ive);;Allfile(*.*);"));
if(mStringModel!=NULL)
{
mNode = osgDB::readNodeFile(mStringModel.toStdString ());
mRoot->addChild(mNode);
setSceneData(mNode);
}
然后读入要加的贴图:
mStringTextures = QFileDialog::getOpenFileNames(this, tr("Load Scene Texture File"), "", tr("Images (*.png *.xpm *.jpg *.bmp)"));
if(mStringTextures.size()>0&&mNode!=NULL)
{
/*////反复读nodefile,开销大
mImage=osgDB::readImageFile(mStringTextures.at(0).toStdString());
mTexture2D->setImage(mImage);
mStateset->setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::ON);
mNode->setStateSet(mStateset);
*/
for(int i=0;i<mStringTextures.size();i++)
{
osg::Image *img=new osg::Image;
img=osgDB::readImageFile(mStringTextures.at(i).toStdString());
mImage.push_back(img);
}
mTexture2D->setImage(mImage.at(0));
mStateset->setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::ON);
mNode->setStateSet(mStateset);
}
if(mStringTextures.size()>1)
{
mCountTextures++;
mTimeChangeTexture.start(1000);
}
定时器操作,每一秒调用函数重新贴图:
/*//反复读nodefile,开销大
if(mStringTextures.size()<=0)
QMessageBox::information(NULL,"ERROR",QString::fromLocal8Bit("No texture is available, please choose at least on testure"));
else
{
mCountTextures=mCountTextures%mStringTextures.size();
//mTexture2D->
mImage=osgDB::readImageFile(mStringTextures.at(mCountTextures).toStdString());
mTexture2D->setImage(mImage);
mCountTextures++;
}
*/
if(mStringTextures.size()<=0)
QMessageBox::information(NULL,"ERROR",QString::fromLocal8Bit("No texture is available, please choose at least on testure"));
else
{
mCountTextures=mCountTextures%mStringTextures.size();
mImage.at(mCountTextures)->dirty();
mTexture2D->setImage(mImage.at(mCountTextures));
mCountTextures++;
} |
|