|
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
osg::ref_ptr<osg::Node>node=osgDB::readNodeFile("Kor.flt");
osg::ref_ptr<osg::StateSet>stateset=new osg::StateSet();
stateset=node->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
osg::ref_ptr<osg::Material>material=new osg::Material();
material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.0));
material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.0));
material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.0));
material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.0));
material->setShininess(osg::Material::FRONT_AND_BACK,0);
material->setTransparency(osg::Material::FRONT_AND_BACK,1.0);
material->setAlpha(osg::Material::FRONT_AND_BACK,0.01);//如果AlphaFunc得方式一定,setAlpha值可以调整模型的透明度
stateset->setAttribute(material.get(),osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
//osg::ref_ptr<osg::AlphaFunc>alphaFunc=new osg::AlphaFunc;
//alphaFunc->setFunction(osg::AlphaFunc::EQUAL,0.5f);//根据其方式的选择和值的变化,可以控制模型的透明度,值越大,透明度越大
//stateset->setAttributeAndModes(alphaFunc,osg::StateAttribute::ON);
stateset->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA));//必须要此句
用此程序可以改变osg自带的模型的透明度,比如cession那个模型,但是我导入了一个带纹理的地形模型怎么改变不了啊,如果去掉地形的纹理,地形模型可以实现透明度,但是模型本身的颜色不会变,加上纹理就感觉没有明显的效果,请问这样的,应该怎样实现像地形这样的模型的透明度啊!!谢谢 |
|