osg 打开透明度
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char** argv)
{
osgViewer::Viewer view;
osg::Group* root = new osg::Group();
root->addChild(osgDB::readNodeFile("cow.osg"));
//方法1
osg::StateSet* state = root->getOrCreateStateSet();
state->setMode(GL_BLEND,osg::StateAttribute::ON);
osg::ref_ptr mat = new osg::Material;
//漫发射光
mat->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.5));
//环境光
mat->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,0.5));
//设置材质的混合光颜色
mat->setTransparency(osg::Material::FRONT_AND_BACK,0.5);
state->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
view.setSceneData(root);
view.run();
}
int main(int argc,char** argv)
{
//方法2
osg::StateSet* state = root->getOrCreateStateSet();
//关闭灯光
state->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);
//打开混合融合模式
state->setMode(GL_BLEND,osg::StateAttribute::ON);
state->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//使用BlendFunc实现透明效果
osg::BlendColor* bc =new osg::BlendColor(osg::Vec4(1.0,1.0,1.0,0.0));
osg::BlendFunc*bf = new osg::BlendFunc();
state->setAttributeAndModes(bf,osg::StateAttribute::ON);
state->setAttributeAndModes(bc,osg::StateAttribute::ON);
bf->setSource(osg::BlendFunc::CONSTANT_ALPHA);
bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
bc->setConstantColor(osg::Vec4(1,1,1,0.5));
}
网格对象的透明设置:
//方法1
osg::ref_ptr
mat->setDiffuse(osg::Material::Front_AND_BACK,osg::Vec4f(1,1,1,0.5));
mat->setAmbient(osg::Material::Front_AND_BACK,osg::Vec4f(1,1,1,0.5));
state->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); //一定要加上这句话,否则网格内部对象看不见
//方法2,这种方法对于网格效果不好,不能关闭光照
osg::StateSet* state = root->getOrCreateStateSet();
state->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);
//打开混合融合模式
state->setMode(GL_BLEND,osg::StateAttribute::ON);
state->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//使用BlendFunc实现透明效果
osg::BlendColor* bc =new osg::BlendColor(osg::Vec4(1.0,1.0,1.0,0.0));
osg::BlendFunc*bf = new osg::BlendFunc();
state->setAttributeAndModes(bf,osg::StateAttribute::ON);
state->setAttributeAndModes(bc,osg::StateAttribute::ON);
bf->setSource(osg::BlendFunc::CONSTANT_ALPHA);
bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
bc->setConstantColor(osg::Vec4(1,1,1,0.5));
基本几何体的透明度设置:
使用OSG中自定义的基本几何体,并设置其透明的效果和网格模型,以圆锥为例。
首先创建圆锥:
osg::ref_ptr geode=new osg::Geode;
//生成圆锥
m_pCone=new osg::Cone;
m_pCone->setHeight(30);
m_pCone->setRadius(30);
osg::ref_ptr shap=new osg::ShapeDrawable(m_pCone);
//第四个参数0.25表示不透明度,0表示完全透明,1表示完全不透明
shap->setColor(osg::Vec4(0.4,0.8,0.4,0.25));
geode->addDrawable(shap);
接下来设置透明效果和网格模型:
//设置几何体透明效果
osg::ref_ptr stateset=geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//设置网格模型
osg::ref_ptr polyMode=new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
stateset->setAttribute(polyMode);
然后就可以使用geode这个节点了。
需要注意的是 从这个例子中可以看出OSG中各个节点的属性设置是在与这个节点相关联的osg::StateSet对象中定义的,之前想设置线框模型时一直在osg::Cone和osg::ShapeDrawable中寻找相关的函数,但是一直没找到。这也加深了对OSG中场景树和渲染树的理解。
还有一点需要注意的就是透明效果不能只在osg::Shape的setColor中设置不透明度,这样好像也不能看到透明效果,还需要在osg::StateSet中设置相关的模式,这是由于OpenGL状态机模型决定的,不要忘了这个地方的设置。