# [osg-users] Transparency on a drawable

Paul Speed pspeed at progeeks.com
Tue Jul 15 01:03:13 PDT 2008

```
Vincent Bourdier wrote:
> Hi
>
> Yes, no solutions seems to work on my problem.
> Because all the propositions concern nodes, I permit to remember that I
> am looking at a way to set alpha level on a osg::Geometry ...
>
> On node I already have a method using material : set alpha channel, and
> it works good...  But on my geometry nothing look to work good...
>
> This is all my geometry creator code :
>
>     osg::ref_ptr<osg::Geometry> builtGeometry(){
>
>         double alpha = 0.1;
>         osg::Vec4 grey(0.4,0.4,0.4,1.0);
>         osg::Vec4 yellow(1.0,1.0,0.0,1.0);

What happens if you put alpha for the fourth value in your color?
-Paul

>
>         osg::Geometry* g = new osg::Geometry;
>         osg::Vec3Array* vertices    = new osg::Vec3Array();
>         osg::Vec3Array* normals        = new osg::Vec3Array();
>         osg::Vec4Array* colors        = new osg::Vec4Array();
>
>         normals->push_back(osg::Vec3(0,0,1));
>         colors->push_back(grey);
>
>         //----
>         vertices->push_back(osg::Vec3( _width/2.0, _height + _dist,
>     _offset));
>         vertices->push_back(osg::Vec3(-_width/2.0, _height + _dist,
>     _offset));
>         vertices->push_back(osg::Vec3(-_width/2.0, 0.0       + _dist,
>     _offset));
>         vertices->push_back(osg::Vec3( _width/2.0, 0.0       + _dist,
>     _offset));
>
>         if(!_empty)
>         else
>     osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,4));
>
>
>         //-----
>         //LINE
>
>         vertices->push_back(osg::Vec3(0.0, 1.0, _offset));
>         vertices->push_back(osg::Vec3(0.0, _dist, _offset));
>         colors->push_back(grey);
>     osg::DrawArrays(osg::PrimitiveSet::LINES,4,2));
>
>
>         //----
>         //LINE_LOOP
>
>         vertices->push_back(osg::Vec3( _width/2.0, _height + _dist,
>     _offset/2.0));
>         vertices->push_back(osg::Vec3(-_width/2.0, _height + _dist,
>     _offset/2.0));
>         vertices->push_back(osg::Vec3(-_width/2.0, 0.0       + _dist,
>     _offset/2.0));
>         vertices->push_back(osg::Vec3( _width/2.0, 0.0       + _dist,
>     _offset/2.0));
>
>         colors->push_back(yellow);
>     osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,6,4));
>
>         g->setNormalArray(normals);
>         g->setVertexArray(vertices);
>         g->setColorArray(colors);
>
>         g->setNormalBinding(osg::Geometry::BIND_OVERALL);
>         g->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
>         g->setDataVariance(osg::Object::DYNAMIC);
>
>
>     //TRANSPARENCY
>
>         osg::StateSet* state = g->getOrCreateStateSet();
>
>     state->setMode(GL_BLEND,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
>         osg::Material* mat = new osg::Material;
>         mat->setAlpha(osg::Material::FRONT_AND_BACK, alpha);
>         state->setAttributeAndModes(mat,osg::StateAttribute::ON |
>     osg::StateAttribute::OVERRIDE);
>
>         osg::BlendFunc* bf = new
>     osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
>     osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
>         state->setAttributeAndModes(bf);
>
>         state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
>         state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
>
>         g->setStateSet(state);
>
>         return g;
>     }
>
>
>
> Regards,
>
> Vincent.
>
> 2008/7/15 dimi christop <dimi_christop at yahoo.com
> <mailto:dimi_christop at yahoo.com>>:
>
>     Hi Vincent,
>     I see you still havent found a solution. So here I send you a
>     complete example of transpareny.
>     Its a modification of the Viewer example from the Qucik start guide.
>     It loads up a cow and overrides the alpha to 0.1.
>     Hope you can start from there.
>
>     Dimi
>
>     // Viewer Example, A minimal OSG viewer
>     #include <osgDB/WriteFile>
>     #include <osg/Notify>
>     #include <osgViewer/Viewer>
>     #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>
>     #include <osg/Notify>
>
>     int
>     main( int, char ** )
>     {
>         // Create a Viewer.
>         osgViewer::Viewer viewer;
>
>         osg::ref_ptr<osg::Node> nde = osgDB::readNodeFile( "cow.osg" );
>
>         // Create StateSet and Material
>         osg::StateSet* state2 = nde->getOrCreateStateSet();
>         osg::ref_ptr<osg::Material> mat2 = new osg::Material;
>
>         // Set alpha to 0.1
>         mat2->setAlpha(osg::Material::FRONT_AND_BACK, 0.1);
>         state2->setAttributeAndModes( mat2.get() ,
>     osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
>
>          // Turn on blending
>         osg::BlendFunc* bf = new
>     osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
>     osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
>         state2->setAttributeAndModes(bf);
>
>         viewer.setSceneData(nde.get());
>
>         if (!viewer.getSceneData())
>         {
>             osg::notify( osg::FATAL ) << "Unable to load data file.
>     Exiting." << std::endl;
>             return 1;
>         }
>
>         // Display, and main loop.
>         return viewer.run();
>     }
>
>     ----- Original Message ----
>     From: Vincent Bourdier <vincent.bourdier at gmail.com
>     <mailto:vincent.bourdier at gmail.com>>
>     To: OpenSceneGraph Users <osg-users at lists.openscenegraph.org
>     <mailto:osg-users at lists.openscenegraph.org>>
>     Sent: Tuesday, July 15, 2008 9:04:23 AM
>     Subject: Re: [osg-users] Transparency on a drawable
>
>     Hi Ulrich
>
>     thanks for help.
>
>     I use/try this code for the moment :
>
>         osg::StateSet* state = g->getOrCreateStateSet();
>
>         state->setMode(GL_BLEND,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
>             osg::Material* mat =
>         (osg::Material*)state->getAttribute(osg::StateAttribute::MATERIAL);
>             if(!mat) {
>                 mat = new osg::Material;
>             }
>             mat->setAlpha(osg::Material::FRONT_AND_BACK, alpha);
>             state->setAttributeAndModes(mat,osg::StateAttribute::ON);
>             state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
>             state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
>
>     But nothing appear transparent...
>
>     Regards,
>        Vincent
>
>     2008/7/12 Ulrich Hertlein <u.hertlein at sandbox.de
>     <mailto:u.hertlein at sandbox.de>>:
>
>         Vincent Bourdier wrote:
>
>             I do exactly the same things, on Nodes...
>
>                 stateset->setMode(GL_BLEND,
>                 osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON );
>
>                 stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
>
>
>         Are you sure you're doing this last line?  It's not in the code
>         snippet you posted initially...
>
>         Cheers,
>         /ulrich
>
>
>                double opacity = 0.1;
>
>                   osg::StateSet* state = mygometry->getOrCreateStateSet();
>                   state->setMode(GL_BLEND,osg::StateAttribute::ON|
>                   osg::StateAttribute::OVERRIDE);
>                   osg::Material* mat = (osg::Material*)state->getAttribute
>                   (osg::StateAttribute::MATERIAL);
>                   if(!mat) {
>                       mat = new osg::Material;
>                       mat->setAlpha(osg::Material::FRONT_AND_BACK, opacity);
>
>             state->setAttributeAndModes(mat,osg::StateAttribute::ON);
>                   }
>
>
>         _______________________________________________
>         osg-users mailing list
>         osg-users at lists.openscenegraph.org
>         <mailto:osg-users at lists.openscenegraph.org>
>         http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
>
>
>     _______________________________________________
>     osg-users mailing list
>     osg-users at lists.openscenegraph.org
>     <mailto:osg-users at lists.openscenegraph.org>
>     http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

```