|
楼主 |
发表于 2009-3-26 20:33:21
|
显示全部楼层
我是遍历每个PagedLOD结点来调用淡入的callback的
class FadeCallback :public osg::NodeCallback
{
public:
FadeCallback():alpha(0)
{
blendFunc = new osg::BlendFunc();
blendColor= new osg::BlendColor(osg::Vec4(1, 1, 1, 0.0));
blendFunc->setSource(osg::BlendFunc::CONSTANT_ALPHA);
blendFunc->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
}
virtual void operator()(osg::Node* node ,osg::NodeVisitor* nv)
{
node->getOrCreateStateSet()->setAttributeAndModes(blendFunc,osg::StateAttribute::ON);
node->getOrCreateStateSet()->setAttributeAndModes(blendColor,osg::StateAttribute::ON);
alpha += 0.01;
blendColor->setConstantColor(osg::Vec4(1, 1, 1,alpha));
if (alpha > 1) alpha = 1.0f;
traverse( node, nv );
}
float alpha;
osg::BlendFunc *blendFunc;
osg::BlendColor *blendColor;
};
class LODVisitor : public osg::NodeVisitor
{
public:
LODVisitor()sg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN){}
virtual void apply(osg::Node& node)
{
osg:agedLOD* mLOD;
if(dynamic_cast<osg::PagedLOD*>(&node)!=NULL)
{
mLOD=dynamic_cast<osg::PagedLOD*>(&node);
mLOD->setUpdateCallback(new FadeCallback());
}
traverse(node);
}
};
int main(int argc, char** argv)
{
setlocale(LC_ALL,"chs");
osg::ref_ptr<osg::Node> root = new osg::Node();
if(argc < 2)
{
string strFileName;
cout<<"lease input File Name:"<<endl;
cin>>strFileName;
root=osgDB::readNodeFile(strFileName);
}
else
{
root=osgDB::readNodeFile(argv[1]);
}
LODVisitor lv;
root->accept(lv);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0;
} |
|