4

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 ?

Azraith Sherkhan
  • 131
  • 2
  • 11
  • It's better if you include too your attempts with transparency. Please, update the question with the code. – Adri C.S. Nov 11 '14 at 09:56
  • I tried your code with the cessna model in the OSG data package. It makes the plane transparent. So it's a start. I'll see if I can make a cube face transparent. – Adri C.S. Nov 11 '14 at 11:48
  • Thank you. It's make plane transparent but when you try input another object into this model it's not render at all. I mean you could not see any object placed at center of model, or even behind it. – Azraith Sherkhan Nov 11 '14 at 12:00
  • Mmm. That's strange. I have the plane and the sphere and I can see both of them. Maybe the transparent object is rendered first, so when you add another object, it doesn't pass the Z-test. Try adding: `model->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON ); model->getStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);` – Adri C.S. Nov 11 '14 at 12:10
  • Glad to be of help. I'll post the comment as an answer, then. – Adri C.S. Nov 11 '14 at 12:23

1 Answers1

3

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

Adri C.S.
  • 2,909
  • 5
  • 36
  • 63