1. 程式人生 > >OSG學習筆記(二)之OSG靜態庫的使用

OSG學習筆記(二)之OSG靜態庫的使用

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非三角面轉換為三角面