|
class TextCallBack: public osg::NodeCallback {
public:
TextCallBack(osgText::Text *text)
{
..................................
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
..............................................
traverse(node,nv);
}
protected:
.......................
};
class ImageCallBack : public osg::NodeCallback
{
public:
ImageCallBack(osg::Image* image):{
.............. }
virtual void operator()(osg::Node* node,osg::NodeVisitor* nv){
.....................................
traverse(node,nv);
}
protected:
......................
};
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr <osg::Group> root = new osg::Group;
//#################
osg::ref_ptr <osg::Image> image = new osg::Image;
osg::ref_ptr <osg::Geode> geode_image = new osg::Geode;
osg::ref_ptr <osg::StateSet> stateset = new osg::StateSet();
osg::ref_ptr <osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(image.get());
texture->setDataVariance(osg::Object:YNAMIC);
stateset->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON);
geode_image->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0), osg::Vec3(1.0f,0.0f,0.0), osg::Vec3(0.0f,0.0f,1.0f)));
geode_image->setStateSet(stateset.get());
geode_image->setUpdateCallback(new ImageCallBack(image));
//################
osg::ref_ptr<osg::Geode> geode_text = new osg::Geode;
osg::ref_ptr<osgText::Text> text = new osgText::Text;
osg::ref_ptr<osgText::Font> font = osgText::readFontFile("fonts/times.ttf");
text->setFont(font.get());
text->setText("http://www.OsgChina.org");
text->setPosition(osg::Vec3(1.0f,6.0f,0.0f));
text->setCharacterSize(1.0f);
text->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
text->setLayout(osgText::Text:EFT_TO_RIGHT);
text->setDataVariance(osg::Object::DYNAMIC);
geode_text->addDrawable(text);
osg::ref_ptr<osg::MatrixTransform> matrixtran = new osg::MatrixTransform;
osg::Matrix matrix;
matrix.makeRotate(1.0,osg::Vec3(1.0f,0.0f,0.0f));
matrixtran->setMatrix(matrix);
matrixtran->addChild(geode_text);
geode_text->setUpdateCallback(new TextCallBack(text));
//###############
root->addChild(matrixtran);
root->addChild(geode_image.get());
viewer->setSceneData(root.get());
viewer->setUpViewInWindow(0, 0, 800, 800);
viewer->realize();
viewer->run();
return 0;
}
我是想实现文字和图片同步更新 文字和图片的数量是相同 但是文字总是先完成更新 怎么让它们同步更新 |
|