Open Inventor动作操作SoCallbackAction

本文介绍了如何使用Inventor库创建3D模型,并通过实现三角形回调函数来实现实时渲染。通过创建坐标节点、索引面集节点和场景根节点,展示了如何在Windows平台上构建基本的3D图形应用程序。

#include <Windows.h> #include <Inventor/Win/SoWin.h> #include <Inventor/Win/viewers/SoWinExaminerViewer.h> #include <Inventor/SoDB.h> #include <Inventor/SoPrimitiveVertex.h> #include <Inventor/actions/SoCallbackAction.h> #include <Inventor/nodes/SoCoordinate3.h> #include <Inventor/nodes/SoIndexedFaceSet.h> #include <Inventor/nodes/SoSeparator.h> #include <Inventor/nodes/SoShape.h> #include <Inventor/nodes/SoSphere.h> #include <Inventor/nodes/SoTranslation.h> static SoCoordinate3 * coord3 = NULL; static SoIndexedFaceSet * ifs = NULL; static int coord3idx = 0; static void triangle_cb(void * userdata, SoCallbackAction * action, const SoPrimitiveVertex * v1, const SoPrimitiveVertex * v2, const SoPrimitiveVertex * v3) { const SbVec3f vtx[] = { v1->getPoint(), v2->getPoint(), v3->getPoint() }; const SbMatrix mm = action->getModelMatrix(); SbVec3f vx[3]; for (int j=0; j < 3; j++) { mm.multVecMatrix(vtx[j], vx[j]); } // (This is sub-optimal -- should scan for the same vertex // coordinates already being present in the SoCoordinate3 // node. We'll get lots of duplicate coordinates from this.) coord3->point.setNum(coord3->point.getNum() + 3); coord3->point.setValues(coord3idx, 3, vx); int32_t indices[] = { coord3idx, coord3idx + 1, coord3idx + 2, -1 }; coord3idx += 3; int oldsize = ifs->coordIndex.getNum(); ifs->coordIndex.setNum(oldsize + 4); ifs->coordIndex.setValues(oldsize, 4, indices); // (Note that it would likely be desirable to grab normal vectors, // materials and / or texture coordinates in a real-world // application. How to do this is not shown by the above code, // but it is not much different from the extraction of vertex // coordinates.) } int main(void) { HWND hWnd = SoWin::init(""); SoDB::init(); SoSeparator * root = new SoSeparator; root->addChild(new SoSphere); SoTranslation * trans = new SoTranslation; trans->translation.setValue(10, 0, 0); root->addChild(trans); SoSphere * ss = new SoSphere; ss->radius = 3; root->addChild(ss); root->ref(); coord3 = new SoCoordinate3; coord3->point.setNum(0); ifs = new SoIndexedFaceSet; ifs->coordIndex.setNum(0); SoCallbackAction ca; ca.addTriangleCallback(SoShape::getClassTypeId(), triangle_cb, NULL); ca.apply(root); SoWinExaminerViewer view(hWnd); view.setSceneGraph(root); view.show(); SoWin::show(hWnd); SoWin::mainLoop(); root->unref(); // [the generated SoCoordinate3 and SoIndexedFaceSet nodes would now // typically be used in a scenegraph in a viewer, or written to disk // or something] return 0; }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值