OSG是以插件的方式管理,因此要使用OSG的某个格式的解析不仅要有其相关lib还必须将相关插件引入,下面通过一个简单的例子加以说明。
OSG相关插件引入使用USE_OSGPLUGIN,例如:
USE_OSGPLUGIN(ive)
USE_OSGPLUGIN(osg)
USE_OSGPLUGIN(osg2)
USE_OSGPLUGIN(rgb)
OSG核心库的引入使用USE_DOTOSGWRAPPER_LIBRARY,例如
USE_DOTOSGWRAPPER_LIBRARY(osg)
USE_DOTOSGWRAPPER_LIBRARY(osgFX)
USE_SERIALIZER_WRAPPER_LIBRARY(osg)
USE_SERIALIZER_WRAPPER_LIBRARY(osgFX)
下面是是用OSG静态库,显示cow.osg的一个简单的例子:
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <iostream>
using namespace std;
using namespace osgViewer;
using namespace osgDB;
// include the plugins we need
USE_OSGPLUGIN(ive)
USE_OSGPLUGIN(osg)
USE_OSGPLUGIN(osg2)
USE_OSGPLUGIN(rgb)
USE_OSGPLUGIN(OpenFlight)
USE_OSGPLUGIN(fbx)
USE_OSGPLUGIN(png)
USE_OSGPLUGIN(jpeg)
#ifdef USE_FREETYPE
USE_OSGPLUGIN(freetype)
#endif
USE_DOTOSGWRAPPER_LIBRARY(osg)
USE_DOTOSGWRAPPER_LIBRARY(osgFX)
USE_DOTOSGWRAPPER_LIBRARY(osgParticle)
USE_DOTOSGWRAPPER_LIBRARY(osgShadow)
USE_DOTOSGWRAPPER_LIBRARY(osgSim)
USE_DOTOSGWRAPPER_LIBRARY(osgTerrain)
USE_DOTOSGWRAPPER_LIBRARY(osgText)
USE_DOTOSGWRAPPER_LIBRARY(osgViewer)
USE_DOTOSGWRAPPER_LIBRARY(osgVolume)
USE_DOTOSGWRAPPER_LIBRARY(osgWidget)
USE_SERIALIZER_WRAPPER_LIBRARY(osg)
USE_SERIALIZER_WRAPPER_LIBRARY(osgAnimation)
USE_SERIALIZER_WRAPPER_LIBRARY(osgFX)
USE_SERIALIZER_WRAPPER_LIBRARY(osgManipulator)
USE_SERIALIZER_WRAPPER_LIBRARY(osgParticle)
USE_SERIALIZER_WRAPPER_LIBRARY(osgShadow)
USE_SERIALIZER_WRAPPER_LIBRARY(osgSim)
USE_SERIALIZER_WRAPPER_LIBRARY(osgTerrain)
USE_SERIALIZER_WRAPPER_LIBRARY(osgText)
USE_SERIALIZER_WRAPPER_LIBRARY(osgVolume)
// include the platform specific GraphicsWindow implementation.
USE_GRAPHICSWINDOW()
bool LoadMode(const string &strFilePath)
{
Viewer viewer;
viewer.setSceneData(readNodeFile(strFilePath));
//添加状态事件
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
//窗口大小事件
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
//添加一些常用状态设置
viewer.addEventHandler(new osgViewer::StatsHandler);
//添加操作器
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
viewer.setCameraManipulator( keyswitchManipulator.get() );
//添加路径记录
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
//set up windows and associated threads
viewer.realize();
//Execute a main frame loop
viewer.run();
return true;
}
int main()
{
string strFilePath = "../Data/cow.osg";
LoadMode(strFilePath);
return 0;
}
注:有时候会出现编译不过的情况,需要在预处理器中添加OSG_LIBRARY_STATIC的宏定义。
下篇OSG非三角面转换为三角面