|
我参考例子代码和Array大哥的《OpenSceneGraph三维渲染引擎设计与实践》里面拖拽器的例子,想实现对场景中模型的平移,旋转等操作。拖拽器的添加都没有问题,但是在鼠标选中模型,移动的时候模型位置并不会改变。经调试发现,在鼠标PickHandler函数里:
- 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;
- }
- }
复制代码
dragger的指针一直为空,但是itr是有值的啊。不知道问题出在哪里?以下是一些相关的代码,贴出来供大家查看,是否是这些代码的问题。谢谢!
-
- //自定义消息响应函数,向场景中添加模型
- LRESULT CMy3DGISView::OnImportModelToScene(WPARAM wParam,LPARAM lParam)
- {
- //从参数面板获得模型导入需要的参数,包括模型路径,模型将导入场景的位置。
- CChildFrame* pFrame = (CChildFrame*)GetParentFrame();
- osg::Vec3d importModelCoords = pFrame->GetFunctionParaSheet()->SceneCreatPage.ImportModelDlg->GetImportModelCoords();
- CString modelPathName = pFrame->GetFunctionParaSheet()->SceneCreatPage.ImportModelDlg->GetImportModelPathName();
- USES_CONVERSION;
- std::string strModelPathName =W2A(modelPathName.LockBuffer());
- modelPathName.UnlockBuffer();
-
- //传递模型路径名和空间位置到图形绘制模块
- MFCOSG->SetImportedModelPathName(strModelPathName);
- MFCOSG->SetImportedModelPosition(importModelCoords);
-
- //为每个待加入的模型创建一个命令管理器,以便后面对模型的拖拽所用
- MFCOSG->SetCommandManager();
-
- //创建一个新的线程,用于加载模型
- ImportThreadHandle = (HANDLE)_beginthread(&CMFCOSG::AddModelToScene, 0, MFCOSG);
- return 1;
- }
复制代码
以下是线程函数的代码:
-
- void CMFCOSG::AddModelToScene(void* ptr)
- {
- CMFCOSG* mfcOsg = (CMFCOSG*)ptr;
-
- //获得模型待导入的空间位置,和模型文件路径名称。
- osg::Vec3d position = mfcOsg->GetImportedModelPosition();
- std::string modelname = mfcOsg->GetImportedModelPathName();
- //读取模型
- osg::ref_ptr<osg::Node> importedModel = osgDB::readNodeFile(modelname);
- //创建位置变换节点
- osg::ref_ptr<osg::PositionAttitudeTransform> modelPat = new osg::PositionAttitudeTransform();
- modelPat->setPosition(position);
- modelPat->addChild(importedModel.get());
- //创建矩阵变换节点
- osg::ref_ptr<osg::MatrixTransform> modelMatrix = new osg::MatrixTransform;
- //为模型添加拖拽器
- modelMatrix.get()->addChild(mfcOsg->AddDraggerToModel(modelPat.get(),mfcOsg->GetCommandManager()));
- mfcOsg->GetSceneRoot()->addChild(modelMatrix.get());
-
- //结束线程
- _endthread();
- }
复制代码
以下是为模型添加拖拽器的代码:
osg::Node* CMFCOSG::AddDraggerToModel(osg::ref_ptr<osg::Node> model, osg::ref_ptr<osgManipulator::CommandManager> cmdMgr)
{
//将模型加入selcetion
osgManipulator::Selection* selection = new osgManipulator::Selection;
selection->addChild(model.get());
osgManipulator::TranslateAxisDragger* dragger = new osgManipulator::TranslateAxisDragger();
dragger->setupDefaultGeometry();
osg::Group* root = new osg::Group;
root->addChild(selection);
root->addChild(dragger);
float scale = model->getBound().radius() * 1.6;
dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
osg::Matrix::translate(model->getBound().center()));
cmdMgr->connect(*dragger, *selection);
return root;
} |
|