|
//问题,运行结果似乎只调用seed[0]的值,不能实现纹理图片信息的动态显示
void delay(int second)
{
time_t start_time, cur_time; // 变量声明
time(&start_time);
do { time(&cur_time);
} while((cur_time - start_time) < second );
}
short seed[]={10,50,100,150,200,255};
//节点回调函数,更新节点的纹理
class CCubeTex : public osg::NodeCallback
{
public:
CCubeTex():index(0) {}
virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
{
osg::ref_ptr<osg::Image> image=new osg::Image();
unsigned char* imageBf=NULL;
image=node->getStateSet()->getTextureAttribute(0,osg::StateAttribute::TEXTURE)
->asTexture()->getImage(0);
imageBf=image->data();
delay(1); //做一秒的延时
int i=0;
for(i=0; i<image->getImageSizeInBytes();i++)
{
//imageBf[i]=(unsigned char)rand()%256;
imageBf[i]=seed[index];
}
traverse(node,nv);
std::cout<<i<<std::endl
<<index<<std::endl
<<(int)imageBf[index]<<std::endl
<<seed[index]<<std::endl;
index++;
if(index>5) index=0;
}
protected:
short index;
};
void main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Node> cube=new osg::Node();
cube=osgDB::readNodeFile("simple.osg");
//设置纹理坐标
osg::ref_ptr<osg::Vec2Array> tc = new osg::Vec2Array;
tc->push_back( osg::Vec2( 0.f, 0.f ) );
tc->push_back( osg::Vec2( 1.f, 0.f ) );
tc->push_back( osg::Vec2( 1.f, 1.f ) );
tc->push_back( osg::Vec2( 0.f, 1.f ) );
cube->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0,tc.get());
osg::ref_ptr<osg::Image> imCube=new osg::Image();
imCube=osgDB::readImageFile("e://fly//xyy.png");
osg::ref_ptr<osg::Texture2D> txCube=new osg::Texture2D();
txCube->setDataVariance(osg::Object:YNAMIC);
txCube->setImage(imCube);
//节点上关联纹理
osg::ref_ptr<osg::StateSet> ssCube= new osg::StateSet();
ssCube->setTextureAttributeAndModes(0,txCube,osg::StateAttribute::ON);
cube->setStateSet(ssCube);
//节点关联更新回调
cube->setUpdateCallback(new CCubeTex);
viewer.setSceneData(cube);
viewer.realize();
viewer.run();/*
while(!viewer.done())
{
viewer.frame();
}*/
} |
|