查看: 1341|回复: 0

创建一个窗体,通过拖拽器实现鼠标左键随意移动物体,但是单击后物体消失了,不解?

[复制链接]

该用户从未签到

发表于 2015-12-26 18:32:35 | 显示全部楼层 |阅读模式
创建一个窗体,通过拖拽器实现鼠标左键随意移动物体,但是在单击鼠标左键后,物体消失,并显示:osgManipulator::computerNodePathToRoot(,) taking first parents path, ignoring others.这是什么意思,如何更改程序,还请大神指点。

原程序如下:

#include <windows.h>
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgManipulator/CommandManager>
#include <osgManipulator/TabBoxDragger>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>

class MouseHandler:public osgGA::GUIEventHandler
{

public :
    MouseHandler() : _activeDragger(0) {}
        bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa,osg::Object* ,osg::NodeVisitor*)
        {
                osgViewer::View* view=dynamic_cast<osgViewer::View*>(&aa);
                if(!view)
                        return false;
                switch(ea.getEventType())
                {
                case osgGA::GUIEventAdapter:USH:
                        {
                                _pointer.reset();
                            osgUtil:ineSegmentIntersector::Intersections intersections;
                                        if ( view->computeIntersections(ea.getX(), ea.getY(), intersections) )
                                        {
                                                _pointer.setCamera( view->getCamera() );
                        _pointer.setMousePosition( ea.getX(), ea.getY() );
                    
                        for ( osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();hitr != intersections.end(); ++hitr)
                                                {
                                                        _pointer.addIntersection( hitr->nodePath, hitr->getLocalIntersectPoint() );
                                                }
                    
                    
                        for ( osg::NodePath::iterator itr=_pointer._hitList.front().first.begin();itr != _pointer._hitList.front().first.end(); ++itr )
                                                {
                                                         osgManipulator:ragger* dragger = dynamic_cast<osgManipulator::Dragger*>( *itr );
                                                         if ( dragger )
                                                         {
                                                                 dragger->handle( _pointer, ea, aa );
                                 _activeDragger = dragger;
                                 break;
                                                         }
                                                }
                    
                                        }
                               
                                break;
                        }
                case osgGA::GUIEventAdapter::DRAG:
                case osgGA::GUIEventAdapter::RELEASE:
                        {
                                if ( _activeDragger )
                {
                    _pointer._hitIter = _pointer._hitList.begin();
                    _pointer.setCamera( view->getCamera() );
                    _pointer.setMousePosition( ea.getX(), ea.getY() );
                    
                    _activeDragger->handle( _pointer, ea, aa );
                }
               
                if ( ea.getEventType()==osgGA::GUIEventAdapter::RELEASE )
                {
                    _activeDragger = NULL;
                    _pointer.reset();
                }
                break;

                        }
                default: break;
                }
                return true;
        }
protected:
         osgManipulator::Dragger* _activeDragger;
     osgManipulator::PointerInfo _pointer;
};


int main(int argc,char** argv)
{
        auto box1=new osg::ShapeDrawable(new osg::Box(osg::Vec3(1.0,0.0,0.0),0.5));
       
        osg::ref_ptr<osg::Geode> Box1=new osg::Geode;
        Box1->addDrawable(box1);
       
        osg::ref_ptr<osgManipulator::Selection> selection1 = new osgManipulator::Selection;
        selection1->addChild(Box1);

    float scale1 =box1->getBound().radius() * 1.2;
    osg::ref_ptr<osgManipulator::TabBoxDragger> dragger1 = new osgManipulator::TabBoxDragger;
    dragger1->setupDefaultGeometry();
    dragger1->setMatrix( osg::Matrix::scale(scale1, scale1, scale1)* osg::Matrix::translate(Box1->getBound().center()) );

    osg::ref_ptr<osg::Group> root = new osg::Group;//创建根节点
    root->addChild( dragger1.get() );
    root->addChild( selection1.get() );

    osg::ref_ptr<osgManipulator::CommandManager> manager1 = new osgManipulator::CommandManager;
    manager1->connect( *dragger1, *selection1 );

        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;//设置图形上下文的特征
             traits->x = 350;
         traits->y = 200;
         traits->width = 1000;
         traits->height = 700;
         traits->windowDecoration = true;
         traits->doubleBuffer = true;
         traits->sharedContext = 0;
            

        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());//创建图形上下文

    osg::ref_ptr<osg::Camera> camera = new osg::Camera;//创建相机
    camera->setGraphicsContext(gc.get());
    camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
    camera->setDrawBuffer(buffer);
    camera->setReadBuffer(buffer);

        osgViewer::Viewer viewer;
        viewer.addSlave(camera.get());
        viewer.addEventHandler(new MouseHandler);//加入鼠标事件
        viewer.setSceneData(root.get());
        return viewer.run();
       
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表