【发布时间】: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->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON ); model->getStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); -
很高兴能提供帮助。那么,我将发表评论作为答案。
标签: visual-c++ transparency openscenegraph