传感器是Open Inventor的重要功能,也是其重要特性,有了它三维功能可以做的活灵活现,此例子是演示的一个圆锥体动作传感器(SoFieldSensor)监视,实际是关联到视镜的摄像头上,只要其位置发生变化,传感器就会感知到,并输出新的位置信息到控制台窗口。代码如下。
#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/SoWin.h> #include <Inventor/Win/viewers/SoWinExaminerViewer.h> #include <Inventor/events/SoKeyboardEvent.h> #include <Inventor/nodes/SoCone.h> #include <Inventor/nodes/SoDirectionalLight.h> #include <Inventor/nodes/SoEventCallback.h> #include <Inventor/nodes/SoMaterial.h> #include <Inventor/nodes/SoPerspectiveCamera.h> #include <Inventor/nodes/SoTransform.h> #include <Inventor/nodes/SoSeparator.h> #include <Inventor/nodes/SoSwitch.h> #include <Inventor/sensors/SoFieldSensor.h> void moveCB(void *data, SoSensor *sensor) { SoSFVec3f *pos = (SoSFVec3f *)((SoFieldSensor *)sensor)->getAttachedField(); printf("detected camera movement: %f %f %f\n", pos->getValue()[0], pos->getValue()[1], pos->getValue()[2]); } int main(int argc, char **argv) { HWND w = SoWin::init(argv[0]); SoWinExaminerViewer *viewer = new SoWinExaminerViewer(w); viewer->setSceneGraph(new SoCone); SoCamera *camera = viewer->getCamera(); SoFieldSensor *sensor = new SoFieldSensor(moveCB, NULL); sensor->attach(&camera->position); SoWin::show(w); viewer->show(); SoWin::mainLoop(); }
演示结果如下.