Open Inventor练习-SoRayPickAction和SoMouseButtonEvent点选操作
来源:互联网 发布:js获取偶数行 编辑:程序博客网 时间:2024/05/27 00:38
SoRayPickAction是三维物体射线选择操作,SoPickedPoin用来存放选择的坐标,SoMouseButtonEvent用来鼠标操作信息处理。例子中获得鼠标点击物体的类型和坐标位置信息,在Console控制台窗口中输出。代码如下。
#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/nodes/SoEventCallback.h>#include <Inventor/actions/SoRayPickAction.h>#include <Inventor/events/SoMouseButtonEvent.h>#include <Inventor/SoPickedPoint.h>SoWinExaminerViewer *viewer = NULL;SoSeparator *root = NULL;void button_event_cb(void *userdata, SoEventCallback *node){const SoMouseButtonEvent *event = (SoMouseButtonEvent *)node->getEvent();SbVec2s pos = event->getPosition(viewer->getViewportRegion());if (event->getButton()==SoMouseButtonEvent::BUTTON1 &&event->getState()==SoButtonEvent::DOWN) {SoRayPickAction pick(viewer->getViewportRegion());pick.setPoint(pos);pick.apply(viewer->getSceneManager()->getSceneGraph());SoPickedPoint *pp = pick.getPickedPoint();if (pp) {const SoPath *path = pp->getPath();SbVec3f p = pp->getPoint();SoNode *node = path->getTail();printf("Picked shape: %s (%s), point: (%f %f %f)\n", node->getName().getString(), node->getTypeId().getName().getString(),p[0], p[1], p[2]);}}}int main (int argc, char ** argv){if (argc != 2) {fprintf(stderr, "Usage: %s <file.iv>\n", argv[0]);exit(1);}// Initialize Coin & SoWinHWND win = SoWin::init(argv[0]);if (win == NULL) exit(1);// Read scene graph from fileSoInput in;if (!in.openFile(argv[1])) {fprintf(stderr, "Unable to open file \"%s\"\n",argv[1]);exit(1);}SoSeparator *fileroot = SoDB::readAll(&in);in.closeFile();root = new SoSeparator;root->ref();SoEventCallback *cb = new SoEventCallback;cb->addEventCallback(SoMouseButtonEvent::getClassTypeId(), button_event_cb, NULL);root->addChild(cb);root->addChild(fileroot);// Create the viewerviewer = new SoWinExaminerViewer(win);viewer->setSceneGraph(root);viewer->setTitle("picking example");viewer->setViewing(FALSE);viewer->setDecoration(FALSE);// Show the windowviewer->show();SoWin::show(win);// Enter the application event loopSoWin::mainLoop();// Free scene graph memoryroot->unref();return 0;}
iv文件如下:
#Inventor V2.1 ascii# open this file using the "raypick" demo programSeparator { DEF A Separator { DEF D Translation { translation -5 -5 0 } DEF E Material { diffuseColor 0 1 1 } DEF E2 MaterialBinding { value PER_PART } DEF F Cube { } } DEF B SoSphere { } DEF C Separator { DEF G Translation { translation 5 0 0 } DEF H Cylinder { } }}
运行效果如下
- Open Inventor练习-SoRayPickAction和SoMouseButtonEvent点选操作
- Open Inventor练习-三维物体选择操作
- Open Inventor练习-一维拖拽器SoTranslate1Dragger
- Open Inventor练习-文件读取
- Open Inventor练习-文件存储
- Open Inventor读写操作SoAction
- Open Inventor动作操作SoCallbackAction
- Open Inventor写操作SoWriteAction
- Open Inventor练习-SoElapsedTime引擎作用
- Open Inventor练习-传感器(SoFieldSensor)功能
- Open Inventor练习-纹理(SoTexture2)
- Open Inventor练习-SoWinExaminerViewer的继承演示
- Open Inventor练习-iv文件读写显示1
- Open Inventor练习-iv文件读写显示2
- Open Inventor练习-iv文件读写显示3
- Open Inventor练习-iv文件读写显示4
- Open Inventor练习-SoEventCallback,SoKeyboardEvent键盘事件(红绿灯)
- Open Inventor练习-SoSelection三维场景中查找功能
- delphi md5加密函数
- eclipse编译java文件
- 新浪开放平台开发1--认证
- Hadoop运行原理详解
- int 和 String 互相转换的多种方法
- Open Inventor练习-SoRayPickAction和SoMouseButtonEvent点选操作
- Qt 简易播放器
- C#中对Word的操作
- 软件创富密码:iPhone应用程序开发攻略之iPhone特色传感器应用(双色)
- 十月百度,阿里巴巴,迅雷搜狗最新面试十一题
- Python中的property()
- Lazarus下WinCE调试
- listview 点击事件无响应
- 怎样调用mysqlcppconn中的escapeString