|
本帖最后由 vinsonxp 于 2013-8-19 10:07 编辑
我的场景由好几个模型文件构成,包括有机器人模型(flt文件)和几个加工工件(osg文件),出于场景布局,我先用osg::Matrix::translate对某些模型进行了移动,结果出现了拖动(dragger)的那个坐标标志出现问题了,它没有与模型在一起,而是在没有移动前的哪个地方,而没有移动过的其他模型的Dragger坐标标志正常。
下图红色的哪个模型是是移动过的,Dragger坐标标志不正常
我是先用osg::ref_ptr<osg::Node>obj = osgDB::readNodeFile把模型从文件读入来,然后再把模型加到osg::ref_ptr<osg::MatrixTransform>,变换后:
osg::Matrix newMatrix = osg::Matrix::translate(tonodeHeight);
newMatrix.preMult(oldMatrix);
mtPutnode->setMatrix(newMatrix);
再添加到根场景中的。拖动功能的代码如下:- void PickDragHandler::pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea, int interMode)
- {
- osg::ref_ptr<osg::Node> node = new osg::Node();
- osg::ref_ptr<osg::Group> parent = new osg::Group();
- double x=ea.getX();
- double y=ea.getY();
- /*方法1 */
- //osgUtil::PolytopeIntersector* picker= new osgUtil::PolytopeIntersector(osgUtil::Intersector::WINDOW, x-0.5, y-0.5, x+0.5, y+0.5 );//多边形求交法
- osgUtil::LineSegmentIntersector* picker= new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW,x,y);
- osgUtil::IntersectionVisitor iv(picker);
- viewer->getCamera()->accept(iv);
- if(picker->containsIntersections())
- {
- osg::NodePath& nodePath=picker->getFirstIntersection().nodePath;
- unsigned int idx=nodePath.size();
- while(idx--)
- {
- osg::MatrixTransform* mt=dynamic_cast<osg::MatrixTransform*>(nodePath[idx]);
- if(mt==NULL) continue;
- int cn = mt->getNumChildren();
- std::string str = mt->getName();
- if (str=="") continue;
- if (str=="Robot")
- {
- AfxMessageBox(_T("机器人不允许移动"));
- return;
- }
- switch(ea.getButton())
- {
- case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
- {
- if(selectedObject.valid())
- {
- unselectObj();
- }
- selectObj(mt);
- break;
- }
- case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
- if(selectedObject==NULL) return;
- unselectObj();
- break;
- }
- return;
- }
- }
- else if (selectedObject!=NULL)
- unselectObj();
复制代码- void PickDragHandler::selectObj(osg::ref_ptr<osg::MatrixTransform> obj)
- {
- selection->addChild(obj.get());
- osg::Node::ParentList parentList=obj->getParents();
- for(osg::Node::ParentList::iterator itr=parentList.begin();
- itr!=parentList.end();
- itr++
- )
- {
- if(*itr!=selection.get())
- {
- (*itr)->replaceChild(obj.get(), selection.get());
- }
- }
- sethighlight(obj);//设置高亮显示
- float scale = obj->getBound().radius() * 1.6;
- dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
- osg::Matrix::translate(obj->getBound().center()));
- selectedObject = obj;
- sceneRoot->addChild(dragger.get());
- cmdMgr->connect((*dragger.get()), (*selection.get()));
- }
- void PickDragHandler::unselectObj(bool flag)
- {
- osg::Node::ParentList parentList=selection->getParents();
- cleanhighlight(selectedObject->getChild(0));//取消高亮显示
- /*变换选中模型的矩阵,实现移动*/
- osg::Matrix mSelection=selection->getMatrix();
- osg::Matrix mObject=selectedObject->getMatrix();
- mObject.preMult(mSelection);
- selectedObject->setMatrix(mObject);
- //获取节点坐标
- osg::Matrixd pos = selectedObject->getMatrix();
- double x = pos.getTrans().x();
- double y = pos.getTrans().y();
- double z = pos.getTrans().z();
- for(osg::Node::ParentList::iterator itr=parentList.begin();
- itr!=parentList.end();
- itr++
- )
- {
- (*itr)->replaceChild(selection.get(), selectedObject.get());
- }
- osg::Matrix identity;
- selection->setMatrix(identity);
- selection->removeChildren(0, selection->getNumChildren());
- selectedObject=NULL;
- sceneRoot->removeChild(dragger.get());
- }
复制代码 拜托各位帮忙找找原因,先谢了! |
|