Open Inventor练习-三维物体选择操作
选择(SoSelection)是三维操作的基本功能,三维物体变换也是三维场景中的变换形式,这里将二者相结合,用户选择模式下,选中物体,可以实现添加和删除变换外围立方体的切换,操作变换立方体SoTransformBoxManip,可以实现物体的变换。如下式详细代码。
#define COIN_DLL
#define SOWIN_DLL
// 加载COIN库文件
#ifdef _DEBUG
#pragma comment(lib, "SoWin1d.lib")
#pragma comment(lib, "Coin3d.lib")
#else
#pragma comment(lib, "SoWin1.lib")
#pragma comment(lib, "Coin3.lib")
#endif
// 添加COIN头文件-Window操作显示库和节点库
#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
#include <Inventor/Win/SoWin.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSphere.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoTranslation.h>
#include <Inventor/nodes/SoSelection.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/manips/SoTransformBoxManip.h>
static SoSearchAction * searcher;
static SoSelection * selection;
SoPath * findRelatedTransform(SoPath * pathtocube)
{
searcher->reset();
searcher->setInterest(SoSearchAction::LAST);
searcher->setType(SoTransform::getClassTypeId());
searcher->apply(pathtocube);
return searcher->getPath();
}
void selectionCB(void * userdata, SoPath * selection)
{
SoPath * transform = findRelatedTransform(selection);
assert(transform != NULL);
SoTransformBoxManip * manip = new SoTransformBoxManip;
manip->replaceNode(transform);
}
void deselectionCB(void * userdata, SoPath * deselection)
{
SoPath * transform = findRelatedTransform(deselection);
assert(transform != NULL);
SoTransformBoxManip * manip = (SoTransformBoxManip *) transform->getTail();
manip->replaceManip(transform, new SoTransform);
}
SoNode * makeTransformableCube(float x, float y, float z)
{
SoTransform * transform = new SoTransform;
transform->translation.setValue(SbVec3f(x, y, z));
SoSeparator * group = new SoSeparator;
group->addChild(transform);
group->addChild(new SoCube);
return group;
}
SoNode * makeSceneGraph(void)
{
selection = new SoSelection;
selection->addChild(makeTransformableCube(0,0,0));
selection->addChild(makeTransformableCube(3,0,0));
selection->addChild(makeTransformableCube(3,3,0));
selection->addChild(makeTransformableCube(0,3,0));
selection->addSelectionCallback(selectionCB, NULL);
selection->addDeselectionCallback(deselectionCB, NULL);
return selection;
}
int main(int argc, char ** argv)
{
HWND w = SoWin::init(argv[0]);
SoWinExaminerViewer * viewer = new SoWinExaminerViewer(w);
viewer->show();
SoWin::show(w);
viewer->setSceneGraph(makeSceneGraph());
viewer->redrawOnSelectionChange(selection);
searcher = new SoSearchAction;
SoWin::mainLoop();
return 0;
}
演示效果如下