OSG对象设置透明

osg 打开透明度

[cpp]  view plain  copy
  print ?
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. osg::ref_ptrcreateBoxA()  
  6. {  
  7.   osg::ref_ptrgnode=new osg::Geode;  
  8.   osg::ref_ptrsd=new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,-10,0),15,2,14));  
  9.   gnode->addDrawable(sd.get());  
  10.   
  11.   sd->setColor(osg::Vec4(0,0.,0.5,0.3f));  
  12.   gnode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);  
  13.   gnode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);  
  14.   return gnode;  
  15. }  
  16. osg::ref_ptrcreateBoxB()  
  17. {  
  18.   osg::ref_ptrgeode=new osg::Geode;  
  19.   
  20.   osg::ref_ptrsd=new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,10,0),10,2,15));  
  21.   geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);  
  22.   geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);  
  23.   geode->addDrawable(sd);  
  24.   sd->setColor(osg::Vec4(1,0,1.0,0.3));  
  25.   return geode;  
  26. }  
  27. int main(int argc, char *argv[])  
  28. {  
  29.   osg::ref_ptrviewer =new osgViewer::Viewer;  
  30.   osg::ref_ptrroot=new osg::Group;  
  31.   root->addChild(osgDB::readNodeFile("cow.osg"));  
  32.   root->addChild(createBoxB());  
  33.   root->addChild(createBoxA());  
  34.   viewer->setSceneData(root.get());  
  35.   return viewer->run();  

  1. }  

转载地址:http://blog.csdn.net/zhuyingqingfen/article/details/8221637

从外部导入的模型,有两种方法来设置透明,一种是材质,一种是混合

#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();
}

//方法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 = new osg::Material;

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状态机模型决定的,不要忘了这个地方的设置。


你可能感兴趣的:(OSG)