Open Inventor练习-SoElapsedTime引擎作用
// 预定义COIN宏
#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/nodes/SoSeparator.h>
int main(int argc, char **argv)
{
SoDB::init(); // 初始化SoDB读取iv文件
HWND hWnd = SoWin::init(argv[0]); // 产生窗口句柄
SoSeparator *pRootSeparator = new SoSeparator;
pRootSeparator->ref(); // 创建根节点并ref计数
SoInput input;
if (input.openFile("engine.iv")) // 加载iv文件并读取全部内容
{
pRootSeparator->addChild(SoDB::readAll(&input));
}
SoWinExaminerViewer viewer(hWnd); // 创建view窗体显示COIN三维内容
viewer.setSceneGraph(pRootSeparator); // 设置显示的根节点
viewer.show(); // view窗体显示三维内容
SoWin::show(hWnd); // windows下显示三维内容
viewer.viewAll(); // 显示物体全部查看模式
SoWin::mainLoop(); // 进入显示主循环
pRootSeparator->unref(); // unref删除节点计数让COIN释放资源
return 0;
}
一下是01.iv的内容
#Inventor V2.1 ascii Separator { Transform { rotation 0 0 1 0 = ComposeRotation { axis 0 1 0 angle 0 = ElapsedTime {speed 0.5 } . timeOut } . rotation } DrawStyle { style LINES } Sphere { } }Transform和ConposeRotation一起结合SoElapsedTime实现自动旋转的功能,等价于SoRotationXYZ的沿轴旋转功能能。
此部分的Open Inventor等价代码如下
// 预定义COIN宏 #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/nodes/SoSeparator.h> // IV等效代码 #include <Inventor/nodes/SoMaterial.h> #include <Inventor/nodes/SoTransform.h> #include <Inventor/nodes/SoCube.h> #include <Inventor/nodes/SoCoordinate3.h> #include <Inventor/nodes/SoIndexedLineSet.h> #include <Inventor/nodes/SoRotation.h> #include <Inventor/engines/SoElapsedTime.h> #include <Inventor/nodes/SoDrawStyle.h> #include <Inventor/nodes/SoSphere.h> #include <Inventor/nodes/SoRotationXYZ.h> int main(int argc, char **argv) { SoDB::init(); // 初始化SoDB读取iv文件 HWND hWnd = SoWin::init(argv[0]); // 产生窗口句柄 SoSeparator *pRootSeparator = new SoSeparator; pRootSeparator->ref(); // 创建根节点并ref计数 SoInput input; if (input.openFile("engine.iv")) // 加载iv文件并读取全部内容 { // pRootSeparator->addChild(SoDB::readAll(&input)); // IV等效代码 SoElapsedTime *pElapsedTime = new SoElapsedTime; pElapsedTime->speed = 0.5f; SoRotationXYZ *pRotationXYZ = new SoRotationXYZ; pRotationXYZ->axis = SoRotationXYZ::Y; pRotationXYZ->angle.connectFrom(&pElapsedTime->timeOut); pRootSeparator->addChild(pRotationXYZ); SoDrawStyle *pDrawStyle = new SoDrawStyle; pDrawStyle->style = SoDrawStyle::LINES; pRootSeparator->addChild(pDrawStyle); pRootSeparator->addChild(new SoSphere); } SoWinExaminerViewer viewer(hWnd); // 创建view窗体显示COIN三维内容 viewer.setSceneGraph(pRootSeparator); // 设置显示的根节点 viewer.show(); // view窗体显示三维内容 SoWin::show(hWnd); // windows下显示三维内容 viewer.viewAll(); // 显示物体全部查看模式 SoWin::mainLoop(); // 进入显示主循环 pRootSeparator->unref(); // unref删除节点计数让COIN释放资源 return 0; }显示的效果如下