【问题标题】:Transparency on one face of cube OpenScreenGraph立方体 OpenScreenGraph 一个面上的透明度
【发布时间】:2015-01-07 19:47:57
【问题描述】:

我已在 VisualStudio 的 OSG 项目中从 3dsMax 导入对象(立方体)。但是我不知道如何只使这个导入的立方体的一个面透明。这是我的代码:

#include <osgViewer/Viewer>
#include <iostream>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/StateAttribute>
#include <osg/CullFace>
#include <osg/Point>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/BlendFunc>
#include <osg/Material>
#include <osg/PolygonMode>


int main(int argc, char** argv)
{
    osg::ref_ptr<osg::Group> root = new osg::Group;
    osg::ref_ptr<osg::Node> model =  osgDB::readNodeFile("cube.3ds"); //Importing model

    osg::StateSet* state2 = model->getOrCreateStateSet(); //Creating material
    osg::ref_ptr<osg::Material> mat2 = new osg::Material;

    mat2->setAlpha(osg::Material::FRONT_AND_BACK, 0.1); //Making alpha channel
    state2->setAttributeAndModes( mat2.get() ,
        osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

    osg::BlendFunc* bf = new                        //Blending
        osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
        osg::BlendFunc::ONE_MINUS_DST_COLOR );
    state2->setAttributeAndModes(bf);      

    root->addChild(model.get());        

    osgViewer::Viewer viewer;
    viewer.setSceneData(root.get());
    viewer.setUpViewOnSingleScreen(0);
    return viewer.run();
}   

这是我刚刚导入文件的来源。我尝试通过多次传递来实现透明度,但没有成功。 有什么方法可以实现吗?

【问题讨论】:

  • 最好也包含透明的尝试。请用代码更新问题。
  • 我用 OSG 数据包中的 cessna 模型尝试了你的代码。它使飞机透明。所以这是一个开始。我看看能不能让立方体的面透明。
  • 谢谢。它使平面透明,但是当您尝试将另一个对象输入此模型时,它根本不会渲染。我的意思是你看不到任何放置在模型中心甚至后面的物体。
  • 嗯。那很奇怪。我有平面和球体,我可以看到它们。也许透明对象是先渲染的,所以当你添加另一个对象时,它没有通过 Z 测试。尝试添加:model-&gt;getStateSet()-&gt;setMode( GL_BLEND, osg::StateAttribute::ON ); model-&gt;getStateSet()-&gt;setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  • 很高兴能提供帮助。那么,我将发表评论作为答案。

标签: visual-c++ transparency openscenegraph


【解决方案1】:

问题中的代码确实使模型透明。例如,使用 OSG 数据包中的 cessna 模型:

添加两个模型,一个盒子和一个球体,盒子也有混合:

我们看到混合正在发挥作用。如果您添加另一个模型但未显示,则可能是在其他模型之前渲染了平面。如果平面恰好在其他模型的前面,即使平面是透明的,你也看不到它们;因为他们没有通过深度测试。

添加

cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
cessna->getStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );

强制在不透明模型之后渲染 cessna。

另外,请注意,如果您提供混合功能,则无需调用

cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );

让我们看另一个场景,盒子在飞机后面,我们还没有设置渲染提示:

现在渲染提示处于活动状态:

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多