osg中openvrml程序示例(可调试):
把IndexedFaceSet.cpp,ReaderWriterVRML2.h,ReaderWriterVRML2.cpp,Primitives.cpp这四个文件复制到新建工程内,(可能要在IndexedFaceSet.cpp,ReaderWriterVRML2.cpp,Primitives.cpp这三个.cpp文件的开头加上#include "stdafx.h"),然后在工程属性的<附加包含目录>中加上"vrml97node.h"的文件目录(如D:\openVrml\home-build),然后在主程序中加入库:
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\zlib1.lib");
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\libjpeg.lib");
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\libpng13.lib");
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\openvrml.lib");//openvrml开源库 D:\openVrml\home-build\lib\openvrml.lib
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\antlr.lib");
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\regex.lib");
#pragma comment( lib, "Ws2_32.lib");//要加入网络的库
也可以把这些库文件加入到该工程的[链接器]的<附加依赖项>中.
主函数main.cpp如下(release版本下):
#include "stdafx.h"
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include "ReaderWriterVRML2.h"
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\zlib1.lib");
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\libjpeg.lib");
#pragma comment( lib, "D:\\osg\\3rdParty\\windows-VS9.0.30729-x86-release\\lib\\libpng13.lib");
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\openvrml.lib");//openvrml开源库 D:\openVrml\home-build\lib\openvrml.lib
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\antlr.lib");
#pragma comment( lib, "D:\\openVrml\\home-build\\lib\\regex.lib");
#pragma comment( lib, "Ws2_32.lib");//要加入网络的库
#pragma comment( lib, "osg.lib") //.在Debug版本下的库名都加d,如"osgd.lib"
#pragma comment( lib, "osgDB.lib")
#pragma comment( lib, "osgViewer.lib")
int main( int argc, char **argv )
{
ReaderWriterVRML2 vrmlObj;//vrml对象
osgDB::ReaderWriter::ReadResult vrmlResult= vrmlObj.readNode( "C:\\Users\\tmljs1988\\Desktop\\snoman2.wrl");//读取vrml文件
//osgDB::ReaderWriter::ReadResult vrmlResult= vrmlObj.readNode( "C:\\Users\\tmljs1988\\Desktop\\sample_vrml\\can.wrl");
osg::ref_ptr< osgViewer::Viewer> pViewer= new osgViewer::Viewer();//osgviewer
osg::Node* pNode= dynamic_cast< osg::Node*> ( vrmlResult.takeObject());//获取vrml中的节点,并动态转化为Node*
if ( pNode != NULL)
{
pViewer->setSceneData( pNode);//设置vrml节点为OSG场景
}
return pViewer->run();
}