OSG渲染引擎对于在内部画的几何图形能够直接使用rgba中的第四个参数对其实现透明化,而从外部导入的模型则需要以下方法:
void setTransparent(osg::Node *node, float trans)//trans用于调节透明度,范围为0.0-1.0,
//其中1.0代表不透明。
{
osg::StateSet * stateset = node->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH, osg::StateAttribute::OFF);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::BlendColor *bc = new osg::BlendColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
osg::ref_ptr<osg::BlendFunc>blendFunc = new osg::BlendFunc();
blendFunc->setSource(osg::BlendFunc::CONSTANT_ALPHA);
blendFunc->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
stateset->setAttributeAndModes(bc, osg::StateAttribute::ON);
stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON);
bc->setConstantColor(osg::Vec4(1, 1, 1, trans));//第四个参数用于调节透明度
}
直接调用这个函数就可以了。