RTTPicker and text of PlaceNode don't interact well

classic Classic list List threaded Threaded
1 message Options
10110111 10110111
Reply | Threaded
Open this post in threaded view
|

RTTPicker and text of PlaceNode don't interact well

This post was updated on .
Hello all.
Using osgEarth 2.7, I'm trying to apply RTTPicker to a PlaceNode, which has an icon and a text label. For the icon, this works fine, and I get the correct ObjectID. But the text for some reason returns its own color as the ID. See the following code. Here 0x600dc0de is the ID I would expect from RTTPicker for the whole PlaceNode. But instead for text, whose color I set to 0xbadc0de1, the ID appears equal to this 0xbadc0de1.

What am I doing wrong?

#include <QDir>
#include <iostream>
#include <QMainWindow>
#include <QApplication>
#include <QTemporaryFile>
#include <osgEarth/Registry>
#include <osgEarth/ObjectIndex>
#include <osgEarthUtil/RTTPicker>
#include <osgEarthQt/ViewerWidget>
#include <osgEarthAnnotation/PlaceNode>
#include <osgEarthUtil/EarthManipulator>


class DemoMainWindow : public QMainWindow
{
public:
    DemoMainWindow(osgEarth::MapNode* mapNode, osgViewer::Viewer* viewer)
    {
        using namespace osgEarth::Annotation;
        setCentralWidget(new osgEarth::QtGui::ViewerWidget(viewer));

        const auto pNode=new PlaceNode(mapNode, osgEarth::GeoPoint(mapNode->getMapSRS(), 37, 55, 0),
                                       "Icon on map");
        mapNode->addChild(pNode);

        auto style=pNode->getStyle();
        style.getOrCreateSymbol<TextSymbol>()->fill()->color()=osg::Vec4(0xba,0xdc,0x0d,0xe1)/255.;
        pNode->setStyle(style);

        pNode->setIconImage(osgDB::readImageFile("/usr/share/icons/hicolor/32x32/apps/firefox.png"));

        pNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);

        const osgEarth::ObjectID id=0x600dc0de;
        osgEarth::Registry::objectIndex()->tagNode(pNode,id);
    }
};

struct MyPickCallback : public osgEarth::Util::RTTPicker::Callback
{
    void onHit(osgEarth::ObjectID id) override
    {
        std::cerr << "onHit, id: " << std::hex << id << std::dec << "\n";
    }
    bool accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter&) override
    {
        return ea.getEventType() == ea.MOVE;
    }
};

int main(int argc, char** argv)
{
    using namespace osgEarth::Util;

    QApplication app(argc, argv);
    osg::setNotifyLevel(osg::WARN);
    osgEarth::setNotifyLevel(osg::WARN);
    QTemporaryFile earthFile(QDir::tempPath()+QDir::separator()+"XXXXXX.earth");
    earthFile.open();
    earthFile.write("<map><options><terrain><color>#555555ff</color></terrain></options></map>");
    earthFile.close();
    const auto earthNode = osgDB::readNodeFile(earthFile.fileName().toStdString());
    if(!earthNode)
    {
        OE_WARN << "Unable to load earth model \"" << earthFile.fileName().toStdString() << "\"\n";
        return EXIT_FAILURE;
    }

    const auto viewer = new osgViewer::Viewer;
    viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
    viewer->setSceneData(earthNode);

    const auto windowSize=300;
    osgEarth::Util::RTTPicker* picker = new osgEarth::Util::RTTPicker(windowSize);
    viewer->addEventHandler(picker);
    picker->setDefaultCallback(new MyPickCallback());

    const auto mapNode = osgEarth::MapNode::findMapNode(earthNode);
    picker->addChild(mapNode);

    DemoMainWindow appWin(mapNode, viewer);
    appWin.resize(windowSize,windowSize);

    const auto manip = new osgEarth::Util::EarthManipulator;
    viewer->setCameraManipulator(manip, 2.);
    manip->setViewpoint(osgEarth::Viewpoint("CameraCenter", 37, 55, 0, 0.0, -90.0, 300000));

    appWin.show();
    return app.exec();
}