Open Inventor练习-SoSelection三维场景中查找功能

在三维场景中查找是一个常用的操作,这里演示了Open Inventor实现查找的操作方法和过程,SoSearchAction是Open Inventor用来实现查找的节点,这里利用它在场景中的两个球体中查找红色的那个,并在控制台中输出查找结果。代码如下。

#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/SoMaterial.h>
#include <Inventor/actions/SoSearchAction.h>

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();
	root->ref();

	SoSearchAction sa;
	SoPath *path;
	sa.setType(SoSphere::getClassTypeId());
	sa.setInterest(SoSearchAction::FIRST);
	sa.apply(root);
	path = sa.getPath();
	if (path != NULL) 
	{
		SoSphere *s = (SoSphere *)path->getTail();
		printf("Sphere found: %s (radius=%.2f)\n", 
			s->getName().getString(), 
			s->radius.getValue());
	}

	// Create the viewer
	SoWinExaminerViewer * v = new SoWinExaminerViewer(win);
	v->setSceneGraph(root);
	v->setTitle("search action example");

	// Show the window
	v->show();
	SoWin::show(win);

	// Enter the application event loop
	SoWin::mainLoop();

	// Free scene graph memory
	root->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、付费专栏及课程。

余额充值