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非三角面转换为三角面




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值