Rotate Tracknode

classic Classic list List threaded Threaded
8 messages Options
Jack S. Jack S.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Rotate Tracknode

Hi all,
I have a  osg::ref_ptr<osgEarth::Annotation::TrackNode>  with an image inside. Is there any way to rotate it or its image?
I have read documentation about ImageControl but don't seems adequate because in my understanding I must add the control to the canvas and rotate the control itself (insteadI need to add directly the tracknode in order to use  its facilities ).

Thanks in advance,

Jack S.
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

Jack,

Try setting the heading() property on the IconSymbol.
Glenn Waldron / Pelican Mapping
Jack S. Jack S.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

Thanks for your reply Glenn.  
I have extended the TrackNode class to be able to access _style variable which in turn can return the IconSymbol. Then I have written a method to set the heading but still not work..
here the code:

MyTrackNode::OrientableTrackNode(osgEarth::MapNode* mapNode, const osgEarth::GeoPoint& position, osg::Image* image, const osgEarth::Annotation::TrackNodeFieldSchema& fieldSchema ):
osgEarth::Annotation::TrackNode(mapNode, position, image, fieldSchema){
}


void MyTrackNode::TrackNode::setHeading(double heading){
    _style.getOrCreateSymbol<osgEarth::Symbology::IconSymbol>()->heading() = heading;
}


It seems have no effect at all
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

Changing the style object won't do anything; you then would need to rebuild the node.

Are you trying to change the heading on the fly? Or just need to set it once at create time?
Glenn Waldron / Pelican Mapping
Jack S. Jack S.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

Hi Glenn,
I'm trying to change heading on the fly to avoid rebuild the node. In any case if I set heading (as I showed you:   _style.getOrCreateSymbol<osgEarth::Symbology::IconSymbol>()->heading() = heading; ) at create time it has no effect
Nikkitta Nikkitta
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

Yeah, that's a good feature to implement.
I have the same issue - I'm trying to create a 2D view (from the sky) of moving planes.
I add osg::Image of plane to TrackNode and I need to set heading on the fly (every step).

Currently I'm thinking of creating a quad with Image texture and rotating it. I hope this will work as a work-around.
Best regards,
Nikita Petrov
john stone john stone
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

I did it like this:

// An update callback used to apply heading rotations to the trackNode's image geometry
struct UpdateCallback : osg::Drawable::UpdateCallback
{
    UpdateCallback(osg::Vec3Array* arr) : init(new osg::Vec3Array(*arr)) {}

    virtual void update(osg::NodeVisitor*, osg::Drawable* d)
    {
        // No work to do if there's no heading
        if (std::isnan(heading)) return;

        // Rotate the vertices by the heading
        auto verts = dynamic_cast<osg::Vec3Array*>(d->asGeometry()->getVertexArray());
        auto rotate = osg::Matrixd::rotate(heading, osg::Z_AXIS);
        for (unsigned i = 0; i < 4; i++) (*verts)[i] = rotate * (*init)[i];
        d->asGeometry()->setVertexArray(verts);

        // Mark as used
        heading = NAN;
    }

    osg::Vec3Array* init; double heading = NAN;
};

void addRotationFeature(osgEarth::Annotation::TrackNode* trackNode, osg::ref_ptr<UpdateCallback>& updateCallback)
{
    // Dig out the underlying Geometry drawable and add an update callback to enable rotation
    struct FindGeometry : osg::NodeVisitor {
        FindGeometry() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
        void apply(osg::Geometry& node) { geom = &node; }
        osg::Geometry* geom = nullptr;
    } f;

    trackNode->accept(f); if (f.geom) {
        auto verts = dynamic_cast<osg::Vec3Array*>(f.geom->getVertexArray()); if (verts && 4 == verts->size()) {
            f.geom->setUseDisplayList(false);
            f.geom->setUseVertexBufferObjects(false);
            f.geom->addUpdateCallback(updateCallback = new UpdateCallback(verts));
        }
    }
}

void rotate(UpdateCallback* callback, double angle)
{
    updateCallback->heading = osg::DegreesToRadians(angle);
}

donghaimoon donghaimoon
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rotate Tracknode

thank you very much. I am in accordance with the way you say, the picture rotated the angle, it worked

   donghaimoon.
Loading...