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
  }
}
演示效果如下

Open Inventor练习-SoSelection三维场景中查找功能_第1张图片


你可能感兴趣的:(Open Inventor练习-SoSelection三维场景中查找功能)