Open Inventor练习-SoSelection节点下三维物体选择功能

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
  }
}

运行结果如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值