Transparency on one face of an OpenScreenGraph cube
I imported object (Cube) from 3dsMax in my OSG project in VisualStudio. But I can't figure out how to make only one face of this imported cube transparent. this is my code:
#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();
}
This is my source with only imported file. I tried to implement multi-pass transparency but with no success. Is there a way how I can do this?
source to share
The code in the question makes the model transparent. For example, with the cessna model from the OSG dataset:
Adding two more models, a box and a sphere, which also has mixing:
We can see that mixing works. If you add another model, but it does not appear, then the plane is probably displayed in front of the other models. If the plane is in front of other models, even if the plane is transparent, you will not see it; since they do not pass the depth test.
Adding
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON ); cessna->getStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
makes cessna appear after opaque models.
Also note that if you provide a mixing function, you do not need to call
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
Let's look at another scene, where the field is behind the plane, and we have not set the render hint:
And now with the render hint active:
source to share