Cannot compile examples from Cookbook using PickHandler class

12 views
Skip to first unread message

Rodrigo Dias

unread,
May 18, 2020, 5:18:52 PM5/18/20
to OpenSceneGraph Users
Hi,

Some examples from the Cookbook (book by Rui Wang & Xuelei Qian) use this code snippet, given at page 39.

    class PickHandler : public osgGA::GUIEventHandler
    {
    public:
        // This virtual method must be overrode by subclasses.
        virtual void doUserOperations( osgUtil::LineSegmentIntersector::Intersection& ) = 0;
        virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
        {
            if ( ea.getEventType()!=osgGA::GUIEventAdapter::RELEASE
            ||ea.getButton()!=osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON
            ||!(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) )
                return false;
            osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
            if ( viewer )
            {
                osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
                osgUtil::IntersectionVisitor iv( intersector.get() );
                viewer->getCamera()->accept( iv );
                if ( intersector->containsIntersections() )
                {
                    osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
                    doUserOperations( result );
                }
            }
            return false;
        }
    };

I couldn't compile examples 1 and 2 from chapter 2, and example 1 from chapter 5.

I'm using Debian 10 and the latest version of OSG. My command to compile the last one is:

$ g++ cook5.1.cpp -losg -losgAnimation -losgGA -losgUtil -losgViewer -o cook5.1

I get the following error message:

cook5.1.cpp: In member function ‘virtual bool osgCookBook::PickHandler::handle(const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&)’:
cook5.1.cpp:72:62: error: binding reference of type ‘osgUtil::LineSegmentIntersector::Intersection&’ to ‘const osgUtil::LineSegmentIntersector::Intersection’ discards qualifiers
      osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The message is the same in the previous examples. Anyone knows what's going on?

Thanks,
Rodrigo.

OpenSceneGraph Users

unread,
May 19, 2020, 2:59:49 AM5/19/20
to osg-...@lists.openscenegraph.org
Hi, Rodrigo. The compiler says that the Intersection object is const,
so the reference you are taking should be also const.

--
Alberto

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Reply all
Reply to author
Forward
0 new messages