讀取 Open Inventor (.iv) 場景圖,轉換為 VRML 2.0 (.wrl) 文件。
#include <Inventor/SoDB.h>
#include <Inventor/SoInteraction.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoOutput.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/actions/SoToVRML2Action.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/VRMLnodes/SoVRMLGroup.h>
int main(int argc, char* argv[]) {
SoDB::init();
SoInteraction::init();
SoInput in;
in.openFile(argv[1]);
printf("Reading... ");
SoSeparator* root = SoDB::readAll(&in);
if (root) {
root->ref();
SbString hdr = in.getHeader();
in.closeFile();
printf("Converting... ");
SoToVRML2Action tovrml2;
tovrml2.apply(root);
SoVRMLGroup* newroot = tovrml2.getVRML2SceneGraph();
newroot->ref();
root->unref();
printf("Writing... ");
SoOutput out;
out.openFile("out.wrl");
out.setHeaderString("#VRML V2.0 utf8");
SoWriteAction wra(&out);
wra.apply(newroot);
out.closeFile();
newroot->unref();
}
return 0;
}
SoDB::init() / SoInteraction::init():初始化數據庫與交互系統。SoInput::openFile() + SoDB::readAll():解析 .iv 文件生成場景圖 SoSeparator*。SoToVRML2Action::apply():遍歷場景圖,將 Inventor 節點轉換為對應 VRML97 節點。getVRML2SceneGraph():獲取轉換後的 VRML 根節點 SoVRMLGroup*。SoOutput::setHeaderString("#VRML V2.0 utf8"):寫入標準 VRML 頭。SoWriteAction:將場景圖序列化為文本文件。ref()/unref() 管理節點生命周期,防止洩漏。g++ iv2vrml.cpp -o iv2vrml -lCoin -lSoVRML
# 或 Visual Studio 鏈接 Coin3D 庫