Transparency on one face of cube OpenScreenGraph

2.2k views Asked by At

I have imported object (Cube) from 3dsMax in my OSG project in VisualStudio. But I can't find out how to make transparent only one face of this imported cube. 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 just imported file. I've tried implement transparency with multiple passes but have no success. Is there any method how could i make it ?

1

There are 1 answers

0
Adri C.S. On BEST ANSWER

The code in the question does make the model transparent. For example, with the cessna model from the OSG data package:

A transparent cessna

Adding two more models, a box and a sphere, where the box also has blending:

With a box and a sphere

From another angle

We see that blending is working. If you add another model but it isn't displayed, then probably the plane is being rendered before the other models. If the plane happens to be in front of the other models, even if the plane is transparent, you won't see them; as they don't pass the depth test.

Adding

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

forces the cessna to be rendered after the opaque models.

Also, note that if you provide a blending function, then you don't need to call

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

Let's see another scene, where the box is behind the plane and we haven't set the rendering hint:

The box is behind the plane, but the rendering hint is deactivated

And now with the rendering hint active:

The box is behind the plane and we have set the rendering hint