|
本帖最后由 starmessage 于 2012-9-6 15:39 编辑
主要功能是点选了物体后(包括多选),不仅仅出现类似3DMax的标识框,还出现拖拽器(用switch结点进行显示和不显示),以TranslateAxisDragger为例,但是当移动拖拽器后,物体重复添加
我的做法是每次点选的物体都放入m_gMultiNode中,计算后的标识框放入m_gMultiBox中,osgManipulator::Selection *selection=new osgManipulator::Selection;
selection->addChild(m_gMultiBox);
selection->addChild(m_gMultiNode);
dragger放入switch结点中,后放入场景结点。
manager->connect(*dragger,*selection);
但是拖动拖拽器后,物体和标识框都没有跟着移动,如何解决?看到array大侠以前发的一个例子osg editer,里面是删除结点再添加的好像,是对单结点的操作,我的包含多选,该如何操作啊,不知道怎么改了。谁帮我改改啊。
部分代码如下:
void CPickHandle::CreateDragger(osgManipulator:ragger* _dragger)//此函数有问题。在于selection不能及时更新,若添加到root内,则又重复的可能。
void CPickHandle::CreateDragger(osgManipulator::Dragger* _dragger)
{
dragger=_dragger;
osgManipulator::Selection *selection=new osgManipulator::Selection;
selection->addChild(m_gMultiNode);//就这里的问题
selection->addChild(m_gMultiBox);
selection->setNodeMask(0x2);
m_fScale=m_gMultiNode->getBound().radius()*0.6;
m_camera->setClearMask(GL_DEPTH_BUFFER_BIT);
m_camera->setRenderOrder(osg::Camera:OST_RENDER);
dragger->setupDefaultGeometry();
dragger->setNodeMask(0x2);
osg::StateSet *stateset=dragger->getOrCreateStateSet();
stateset->setMode(GL_DEPTH_RANGE,osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
dragger->setMatrix(osg::Matrix::scale(m_fScale,m_fScale,m_fScale)*
osg::Matrix::translate(m_gMultiNode->getBound().center()));
m_camera->addChild(dragger);
osgManipulator::CommandManager *manager=new osgManipulator::CommandManager;
m_camera->setNodeMask(0x2);
m_sDragger->addChild(dragger,false);
m_sDragger->setNodeMask(0x2);
manager->connect(*dragger,*selection);
}
osg::Switch* CPickHandle::getSwitchDragger(void)
{
return m_sDragger;
}
bool CPickHandle::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
viewer=dynamic_cast<osgViewer::Viewer*>(&aa);
if ( ea.getEventType()==osgGA::GUIEventAdapter::PUSH && ea.getButton()==osgGA::GUIEventAdapter:EFT_MOUSE_BUTTON)
{
if (viewer && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL){
pickHandle(ea,aa);//加入点选组结点,加入标识框组结点,没问题。}
if(viewer && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT)
{
osgUtil::LineSegmentIntersector::Intersections hits;
_pointer.reset();
osgUtil::LineSegmentIntersector *intersector =new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
osgUtil::IntersectionVisitor iv(intersector);
viewer->getCamera()->accept( iv );
if(viewer->computeIntersections(ea.getX(),ea.getY(),hits))
{
_pointer.setCamera(viewer->getCamera());
_pointer.setMousePosition(ea.getX(),ea.getY());
osgUtil::LineSegmentIntersector::Intersections::iterator hitr;
for(hitr=hits.begin();hitr!=hits.end();++hitr)
{
_pointer.addIntersection(hitr->nodePath,hitr->getLocalIntersectPoint());
}
osg::NodePath::iterator itr;
for(itr=_pointer._hitList.front().first.begin();
itr!=_pointer._hitList.front().first.end();
++itr)
{
osgManipulator::Dragger* _dragger=
dynamic_cast<osgManipulator::Dragger*>(*itr);
if(_dragger)
{
_dragger->handle(_pointer,ea,aa);
activedragger=_dragger;
break;
}
}
}
}
}
if( ea.getEventType()==osgGA::GUIEventAdapter::DRAG || ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
{
if(viewer && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT)
{
if(activedragger)
{
_pointer._hitIter=_pointer._hitList.begin();
_pointer.setCamera(viewer->getCamera());
_pointer.setMousePosition(ea.getX(),ea.getY());
activedragger->handle(_pointer,ea,aa);}
}
if(ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
{
activedragger=NULL;
_pointer.reset();
}
return true;
}
}
if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH &&
ea.getButton()==osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON &&
!(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT) &&
!(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL)){
cancelSelection();m_sDragger->setAllChildrenOff();}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
osg::ref_ptr<osg::Node> model1 = osgDB::readNodeFile( "..\\..\\3ds\\BoA.3ds" );
osg::ref_ptr<osg::Group> root = new osg::Group;
osgViewer::Viewer viewer;
osg::ref_ptr<CPickHandle> picker = new CPickHandle();
root->addChild( model1.get() );
root->addChild(picker->getMultiBox());
root->addChild(picker->getSwitchDragger());
viewer.setSceneData(root.get());
viewer.addEventHandler(picker.get());
return viewer.run();
}
|
|