I created a class intended to act as a floating label for components within 
the scene.  I would use the osg text library except that many of my labels 
use sophisticated mathematical expressions which would be difficult to lay 
out.  What I do is generate my text using Mathematica, and then run it 
through some code using Magick++ to clear the background.  The code below is 
what I am using to create the label in the scene.  

When I use it, the labels appear with a white background.  Using 
osg::TexEnv::DECAL seems to let the color bound to the QUAD show through, but 
what I really want is for the background part to be transparent so that the 
rest of the scene shows through.  Can that be accomplished?  How?

class ComponentLabel : public osg::MatrixTransform {
//...
ComponentLabel( const std::string& fileName = "fileName_not_given")
 this->setMatrix( osg::Matrix::translate( vec3f( from ) ) );
    osg::AutoTransform * ax = new osg::AutoTransform();
    
    ax->setAutoRotateMode( osg::AutoTransform::ROTATE_TO_CAMERA );
    this->addChild( ax );
    
    osg::Geode* g = new osg::Geode();
    g->addDrawable( _edge_rptr.get() );
    this->addChild( g );

    ax->setPosition( vec3f( to ) );

    osg::Geode * geode = new osg::Geode();
    ax->addChild( geode );

    osg::Geometry* geometry = new osg::Geometry();
    geode->addDrawable( geometry );

    osg::DrawElementsUInt* face = new osg::DrawElementsUInt( 
osg::PrimitiveSet::QUADS, 0 );
    geometry->addPrimitiveSet( face );
    face->push_back( 0 );
    face->push_back( 1 );
    face->push_back( 2 );
    face->push_back( 3 );

    
    osg::Image * image = osgDB::readImageFile( fileName );
    if ( !image ) {
      std::cerr << "diagram::ComponentLabel(): failed to load file -> " << 
fileName << std::endl;
      return ;
    }

    image->setFileName( fileName );

    osg::Texture2D* texture = new osg::Texture2D();
    texture->setImage( 0, image );

    osg::Vec3Array* vertices = new osg::Vec3Array;
    geometry->setVertexArray( vertices );
    osg::Vec3 dw( image->s()/2, 0, 0 );
    osg::Vec3 dh( 0, image->t()/2, 0 );

    dw*=0.001f;
    dh*=0.001f;
    vertices->push_back( -( dw + dh ) );
    vertices->push_back( dw - dh );
    vertices->push_back( dw + dh );
    vertices->push_back( -dw + dh );


    osg::Vec2Array* texcoords = new osg::Vec2Array( 4 );
    ( *texcoords ) [ 0 ].set( 0.0f, 0.0f );
    ( *texcoords ) [ 1 ].set( 1.0f, 0.0f );
    ( *texcoords ) [ 2 ].set( 1.0f, 1.0f );
    ( *texcoords ) [ 3 ].set( 0.0f, 1.0f );
        
    geometry->setTexCoordArray( 0, texcoords );
    
    osg::StateSet* ss = geode->getOrCreateStateSet();
    ss->setTextureAttributeAndModes( 0, texture, osg::StateAttribute::ON );

    osg::TexEnv* texEnv = new osg::TexEnv();
    texEnv->setMode( osg::TexEnv::REPLACE );
    ss->setTextureAttribute( 0, texEnv );
    geometry->setStateSet( ss );
    
    osg::Vec4Array* color = new osg::Vec4Array;
    color->push_back( osg::Vec4f( 0, 0, 0, 1 ) );
    geometry->setColorArray( color );
    geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
    
  }
};
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to