|
看Array大哥的例子,自己也做了做,后来发现问题,Array大哥的例子中,如果使用旋转和缩放拖拽器时,物体就会发生偏移,这是个问题啊。如何解决的呢?dragger中有一个方法是removeTransformUpdating(),不知道是否与之相关,相关的话该如何设置呢?以下是Array大哥osg edit例子中的部分代码。
bool PickModelHandler::SelectNode( const osgGA::GUIEventAdapter &ea,
osgGA::GUIActionAdapter &aa ){
osgViewer::View* view = dynamic_cast<osgViewer::View*>( &aa );
_pointer.reset();
TestOperationType();
osgUtil:ineSegmentIntersector::Intersections hits;
if( view->computeIntersections( ea.getX(), ea.getY(), hits ) ){
_pointer.setCamera( view->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 ){
if( !_hasSelected ){
switch( _selectType ){
case GEOD:
{
osg::ref_ptr<osg::Geode> gode = dynamic_cast<osg::Geode*>( *itr );
if( gode ){
float scale = gode->computeBound().radius() * 1.6;
_selection->setMatrix( osg::Matrix::identity() );
_dragger->setMatrix( osg::Matrix::scale( scale, scale, scale ) *
osg::Matrix::translate( gode->computeBound().center() ) );
gode->getParent( 0 )->addChild( _selection.get() );
gode->getParent( 0 )->addChild( _dragger.get() );
_selection->addChild( gode.get() );
gode->getParent( 0 )->removeChild( gode.get() );
_dragger->setNodeMask( 1 );
_hasSelected = true;
return true;
}
}
break;
case MODEL:
{
osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>( *itr );
if( group ){
if( group->getName() == "MODEL" ){
float scale = group->computeBound().radius() * 1.6;
_selection->setMatrix( osg::Matrix::identity() );
_dragger->setMatrix( osg::Matrix::scale( scale, scale, scale ) *
osg::Matrix::translate( group->computeBound().center() ) );
group->getParent( 0 )->addChild( _selection.get() );
group->getParent( 0 )->addChild( _dragger.get() );
_selection->addChild( group.get() );
group->getParent( 0 )->removeChild( group.get() );
_dragger->setNodeMask( 1 );
_hasSelected = true;
return true;
}
}
}
break;
}
}else{
osgManipulator:ragger* dragger = dynamic_cast<osgManipulator::Dragger*>( *itr );
if( dragger ){
dragger->handle( _pointer, ea, aa );
_activeDragger = dragger;
return true;
}
}
}
}else{
_draggerTranslate->setNodeMask( 0 );
_draggerRotate->setNodeMask( 0 );
_draggerScale->setNodeMask( 0 );
_hasSelected = false;
if( _selection->getNumChildren() != 0 ){
osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>( _selection->getParent( 0 ) );
if( mt ){
mt->setMatrix( mt->getMatrix() * _selection->getMatrix() );
mt->removeChild( _selection.get() );
mt->removeChild( _dragger.get() );
mt->addChild( _selection->getChild( 0 ) );
_selection->removeChild( _selection->getChild( 0 ) );
}else{
osg::MatrixTransform* mt = new osg::MatrixTransform;
mt->setMatrix( _selection->getMatrix() );
_selection->getParent( 0 )->addChild( mt );
_selection->getParent( 0 )->removeChild( _dragger.get() );
_selection->getParent( 0 )->removeChild( _selection.get() );
mt->addChild( _selection->getChild( 0 ) );
_selection->removeChild( _selection->getChild( 0 ) );
}
}
return view->getCameraManipulator()->handle( ea, aa );
}
return false;
}
bool PickModelHandler::DragNode( const osgGA::GUIEventAdapter &ea,
osgGA::GUIActionAdapter &aa ){
osgViewer::View* view = dynamic_cast<osgViewer::View*>( &aa );
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();
}
return true;
}else
return view->getCameraManipulator()->handle( ea, aa );
}
该如何调整呢?搞了好久了。不知道怎么整了,大牛出来帮帮小弟吧。
|
|