osg 打开透明度
[cpp] view plain copy
#include
#include
#include
#include
osg::ref_ptr
{
osg::ref_ptr
osg::ref_ptr
gnode->addDrawable(sd.get());
sd->setColor(osg::Vec4(0,0.,0.5,0.3f));
gnode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
gnode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
return gnode;
}
osg::ref_ptr
{
osg::ref_ptr
osg::ref_ptr
geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
geode->addDrawable(sd);
sd->setColor(osg::Vec4(1,0,1.0,0.3));
return geode;
}
int main(int argc, char *argv[])
{
osg::ref_ptr
osg::ref_ptr
root->addChild(osgDB::readNodeFile("cow.osg"));
root->addChild(createBoxB());
root->addChild(createBoxA());
viewer->setSceneData(root.get());
return viewer->run();
}
转载地址:http://blog.csdn.net/zhuyingqingfen/article/details/8221637
从外部导入的模型,有两种方法来设置透明,一种是材质,一种是混合
#include <Windows.h>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osg/StateSet>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/BlendColor>
#include <osg/BlendFunc>
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<osg::Material> 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();
}
//方法2,使用混合函数来设置透明
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中自定义的基本几何体,并设置其透明的效果和网格模型,以圆锥为例。
首先创建圆锥:
<span style="background-color: rgb(255, 255, 255);"> osg::ref_ptr<osg::Geode> geode=new osg::Geode;
//生成圆锥
m_pCone=new osg::Cone;
m_pCone->setHeight(30);
m_pCone->setRadius(30);
osg::ref_ptr<osg::ShapeDrawable> 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);</span>
接下来设置透明效果和网格模型:
<span style="background-color: rgb(255, 255, 255);"> //设置几何体透明效果
osg::ref_ptr<osg::StateSet> stateset=geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//设置网格模型
osg::ref_ptr<osg::PolygonMode> polyMode=new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
stateset->setAttribute(polyMode);</span>
然后就可以使用geode这个节点了。
需要注意的是 从这个例子中可以看出OSG中各个节点的属性设置是在与这个节点相关联的osg::StateSet对象中定义的,之前想设置线框模型时一直在osg::Cone和osg::ShapeDrawable中寻找相关的函数,但是一直没找到。这也加深了对OSG中场景树和渲染树的理解。
还有一点需要注意的就是透明效果不能只在osg::Shape的setColor中设置不透明度,这样好像也不能看到透明效果,还需要在osg::StateSet中设置相关的模式,这是由于OpenGL状态机模型决定的,不要忘了这个地方的设置。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章