OSG对象设置透明

osg 打开透明度

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osg/StateSet>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/BlendColor>
#include <osg/BlendFunc>
#include <osg/Node>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
osg::ref_ptr<osg::Node>createBoxA()
{
	osg::ref_ptr<osg::Geode>gnode = new osg::Geode;
	osg::ref_ptr<osg::ShapeDrawable>sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, -10, 0), 15, 2, 14));
	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::Node>createBoxB()
{
	osg::ref_ptr<osg::Geode>geode = new osg::Geode;

	osg::ref_ptr<osg::ShapeDrawable>sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 10, 0), 10, 2, 15));
	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<osgViewer::Viewer>viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::Group>root = new osg::Group;
	root->addChild(osgDB::readNodeFile("cow.osg"));
	root->addChild(createBoxB());
	root->addChild(createBoxA());
	viewer->setSceneData(root.get());
	return viewer->run();
}

效果如下:

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

#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<osg::Material> 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<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>

 接下来设置透明效果和网格模型: 

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);

  然后就可以使用geode这个节点了。

       需要注意的是 从这个例子中可以看出OSG中各个节点的属性设置是在与这个节点相关联的osg::StateSet对象中定义的,之前想设置线框模型时一直在osg::Cone和osg::ShapeDrawable中寻找相关的函数,但是一直没找到。这也加深了对OSG中场景树和渲染树的理解。

        还有一点需要注意的就是透明效果不能只在osg::Shape的setColor中设置不透明度,这样好像也不能看到透明效果,还需要在osg::StateSet中设置相关的模式,这是由于OpenGL状态机模型决定的,不要忘了这个地方的设置。

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值