Open Inventor用SoSelection节点实现对其节点下的三维物体节点选择,选中的物体用红色的外框高亮显示,选择的模式有SINGLE单选,SHIFT多选,TOGGLE切换选择三种。代码演示了SHIFT多选功能,对场景中的连个红色球体选择,并在控制台中输出选择信息。代码如下。
#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/SoCone.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSelection.h>
#include <Inventor/actions/SoBoxHighlightRenderAction.h>
SoSelection *selection = NULL;
void made_selection(void *userdata, SoPath *path)
{
SoNode *node = path->getTail();
printf("Node selected: %s (%s)\n",
node->getName().getString(),
node->getTypeId().getName().getString());
selection->touch(); // We must touch to force a redraw
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <file.iv>\n", argv[0]);
exit(1);
}
// Initialize Coin & SoWin
HWND win = SoWin::init(argv[0]);
if (win == NULL) exit(1);
// Read scene graph from file
SoInput in;
if (!in.openFile(argv[1]))
{
fprintf(stderr, "Unable to open file \"%s\"\n",argv[1]);
exit(1);
}
SoSeparator *root = SoDB::readAll(&in);
in.closeFile();
selection = new SoSelection;
selection->policy.setValue(SoSelection::SHIFT);
selection->ref();
selection->addChild(root);
selection->addSelectionCallback(made_selection, NULL);
SoWinExaminerViewer * examinerviewer = new SoWinExaminerViewer( win );
examinerviewer->setSceneGraph(selection);
examinerviewer->setGLRenderAction( new SoBoxHighlightRenderAction );
examinerviewer->show();
SoWin::show(win);
// Enter the application event loop
SoWin::mainLoop();
// Free scene graph memory
selection->unref();
return 0;
}
IV文件如下
#Inventor V2.1 ascii
Separator {
DEF s Sphere {
radius 0.5
}
Separator {
Transform {
translation 0.9 0.5 0
scaleFactor 0.4 0.4 0.4
}
Material {
diffuseColor 1 0 0
}
USE s
}
}
运行结果如下