Martin, here's one method. It's a little pricey since you need to attach it
as both a cull and update callback. I've left out some of the code but you
get the idea. Basically you use the MVPW matrix to drop into screen space,
apply the offset, then go back to object space.


struct TextOffsetCB : public osg::NodeCallback
{
    void operator()( osg::Node* node, osg::NodeVisitor* nv )
    {
        if ( nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR )
        {
            osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(
nv );
            osg::Matrix mvpw = *cv->getMVPW();
            osg::Vec3d screenPos = osg::Vec3d(0,0,0) * mvpw;
            osg::Vec3d newScreenPos = screenPos + osg::Vec3d(
pixelOffset.x(), pixelOffset.y(), 0 );
            _newTextPos = newScreenPos * osg::Matrixd::inverse( mvpw );
        }
        else if ( nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR )
        {
            _text->setPosition( _newTextPos );
        }
        traverse( node, nv );
    }

    //...
};


Glenn Waldron : Pelican Mapping : +1.703.652.4791


On Wed, Aug 25, 2010 at 2:59 AM, Martin Scheffler <[email protected]> wrote:

> Hi,
>
> I have a number of actors that I want to add text labels to. The text
> labels should be screen aligned with a fixed size. I want the text to stay
> 50 pixels above the center of the actor, so that the actor is not obscured
> by text when I am far away. Is this possible? There is a method
> osgText::TextBase::setPosition that I can use to add a height in scene
> coordinates, but I would like to add an offset to the text bounding box.
>
> Ideally i would do this:
> osgText::Text* text = new osgText::Text();
>  text->setText("My Actor");
>  text->setAlignment(osgText::TextBase::LEFT_TOP);
>  text->setAxisAlignment(osgText::TextBase::SCREEN);
> text->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
>
> // center is one meter above actor coordinate center
> text->setPosition(osg::Vec3(0, 0, 1));
>
> // push text 20 pixels to left and 30 upwards
> text->setOffset(osg::Vec3(-20, 0, 30));
>
> Any idea on how to accomplish this?
>
>
> Thank you!
>
> Cheers,
> Martin
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=31026#31026
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to