[osg-users] Problems with Alpha blending

Tony Vasile minghia at gmail.com
Mon Aug 1 20:26:24 PDT 2016


I'm playing around with alpha blending of an image and I am changing the alpha value of the colour array and the image isn't becoming invisible. The program I have is a modified example. So what am I doing wrong?


Code:

#include <osg/AlphaFunc>
#include <osg/BlendFunc>
#include <osg/Texture2D>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <iostream>

osg::ref_ptr<osg::Vec4Array> colors;
unsigned int g_width = 800, g_height = 600;

class TechniqueEventHandler : public osgGA::GUIEventHandler
{
public:

    TechniqueEventHandler() { _alpha=(*colors)[0][3];}

    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);

protected:

    ~TechniqueEventHandler() {}

    TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}

    float         _alpha;
};




bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
                ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
            {
                _alpha += 0.1;
                if (_alpha > 1.0) _alpha = 1.0;
                (*colors)[0][3] = _alpha;
                std::cout << "Alpha value is " << _alpha << std::endl;
                return true;
            }
            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
            {
                _alpha -= 0.1;
                if (_alpha < 0.0) _alpha = 0.0;
                (*colors)[0][3] = _alpha;
                std::cout << "Alpha value is " << _alpha << std::endl;
                return true;
            }
            return false;
        }

        default:
            return false;
    }
}

int main( int argc, char** argv )
{
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(-0.5f, 0.0f,-0.5f) );
    vertices->push_back( osg::Vec3( 0.5f, 0.0f,-0.5f) );
    vertices->push_back( osg::Vec3( 0.5f, 0.0f, 0.5f) );
    vertices->push_back( osg::Vec3(-0.5f, 0.0f, 0.5f) );

    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
    normals->push_back( osg::Vec3(0.0f,-1.0f, 0.0f) );

    osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
    texcoords->push_back( osg::Vec2(0.0f, 0.0f) );
    texcoords->push_back( osg::Vec2(0.0f, 1.0f) );
    texcoords->push_back( osg::Vec2(1.0f, 1.0f) );
    texcoords->push_back( osg::Vec2(1.0f, 0.0f) );

    colors = new osg::Vec4Array;
    colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 0.5f) );
    osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
    quad->setVertexArray( vertices.get() );
    quad->setNormalArray( normals.get() );
    quad->setNormalBinding( osg::Geometry::BIND_OVERALL );
    quad->setColorArray( colors.get() );
    quad->setColorBinding( osg::Geometry::BIND_OVERALL );
    quad->setTexCoordArray( 0, texcoords.get() );
    quad->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4) );

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( quad.get() );

    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile( "lz.rgb" );
    texture->setImage( image.get() );

    osg::StateSet* stateset = geode->getOrCreateStateSet();
    stateset->setTextureAttributeAndModes( 0, texture.get() );
    stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
   // stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
    osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
    alphaFunc->setFunction(osg::AlphaFunc::GREATER,0.2f);


    osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc;
    //blendFunc->setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    stateset->setAttributeAndModes( blendFunc );

    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( geode.get() );

    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    viewer.addEventHandler(new TechniqueEventHandler());
     viewer.setUpViewInWindow( 50, 50, g_width, g_height );
    return viewer.run();
}





------------------------
Tony V

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=68245#68245








More information about the osg-users mailing list