|
本帖最后由 qingfeng5211 于 2011-2-24 09:34 编辑
用MFC框架做了个程序, 通过点击工具栏添加节点, 通过在view中点击选择然后按delete键来删除节点, 第一次删除没有问题,反复添加删除几次,再次点击添加时就崩溃了。
我在论坛了看了以往的帖子,添加和删除都是放在NodeCallback里进行的。不知道是什么问题,请大牛们指点。
我用2.9.9版本发现有问题,就又编译2.8.3版本试了一下,还是有问题。
更新回调实现
- CAddRemoveCallback::CAddRemoveCallback()
- {
- _paused = TRUE;
- }
- void CAddRemoveCallback::operator()(osg::Node * node, osg::NodeVisitor * nv)
- {
- if(!_paused)
- {
- osg::Group* root = dynamic_cast<osg::Group*>(node);
- if ( root != NULL )
- {
- for ( size_t i = 0; i < _vtRemoving.size(); ++i )
- {
- osg::Node::ParentList parents = _vtRemoving[i]->getParents();
- for(osg::Node::ParentList::iterator it = parents.begin();
- it!=parents.end();
- ++it)
- {
- (*it)->removeChild(_vtRemoving[i]);
- }
- }
- if (_vtRemoving.size()>0)
- {
- _vtRemoving.clear();
- }
- for ( size_t i = 0; i < _vtAdding.size(); ++i )
- {
- root->addChild( _vtAdding[i] );
- }
-
- if (_vtAdding.size())
- {
- _vtAdding.clear();
- }
- }
- _paused = TRUE;
- }
- }
- void CAddRemoveCallback::AddNode(osg::Node * node)
- {
- _paused = FALSE;
- _vtAdding.push_back(node);
- }
- void CAddRemoveCallback::RemoveNode(osg::Node * node)
- {
- _paused = FALSE;
- _vtRemoving.push_back(node);
- }
- void CAddRemoveCallback::SetFlag(BOOL bpaused)
- {
- _paused = bpaused;
- }
复制代码
添加时的调用:
- mRoot = new osg::Group;
- mRoot->setDataVariance(osg::Object::DYNAMIC);
- mAddRemoveCallback = new CAddRemoveCallback;
- mRoot->setUpdateCallback(mAddRemoveCallback);
- mAddRemoveCallback->AddNode(group);
复制代码
删除时的调用
- mAddRemoveCallBack->RemoveNode(group2);
复制代码 |
|