OSG对象设置透明
阅读原文时间:2021年04月21日阅读:1

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. sd->setColor(osg::Vec4(0,0.,0.5,0.3f));

  11. gnode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);

  12. gnode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

  13. return gnode;

  14. }

  15. osg::ref_ptrcreateBoxB()

  16. {

  17. osg::ref_ptrgeode=new osg::Geode;

  18. osg::ref_ptrsd=new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,10,0),10,2,15));

  19. geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);

  20. geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

  21. geode->addDrawable(sd);

  22. sd->setColor(osg::Vec4(1,0,1.0,0.3));

  23. return geode;

  24. }

  25. int main(int argc, char *argv[])

  26. {

  27. osg::ref_ptrviewer =new osgViewer::Viewer;

  28. osg::ref_ptrroot=new osg::Group;

  29. root->addChild(osgDB::readNodeFile("cow.osg"));

  30. root->addChild(createBoxB());

  31. root->addChild(createBoxA());

  32. viewer->setSceneData(root.get());

  33. return viewer->run();

  34. }

转载地址: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 = 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中自定义的基本几何体,并设置其透明的效果和网格模型,以圆锥为例。

首先创建圆锥:

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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章