OSG的配置以及例子
2015/1/3
OSG配置
1, 文件(这个配置中是把拷贝的OSG文件夹放在D盘)
2, 环境变量
在用户变量中添加,不是系统变量中添加
变量:PATH
值:
D:\OSG\OSG-3.2.0-bin\bin;D:\OSG\3rdParty\bin;D:\OSG\osgEarth-2.5-bin\bin;D:\OSG\gdal-1.11.0-bin\bin;D:\OSG\geos-3.4.2-bin\bin;D:\OSG\proj-4.8.0-bin\bin;
示例1-1
OSGHelloWorld
(1)建立一个WIN32控制台输出程序。下一步,附加项:空项目。
添加main.cpp
#ifdef _DEBUG
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#else
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#endif
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
int main( )
{
osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
viewer->setSceneData(osgDB::readNodeFile("glider.osg"));
returnviewer->run();
}
(2)配置
Project->helloworld property pages->configurationproperties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,库目录
include D:\OSG\OSG-3.2.0-bin\include
bin D:\OSG\OSG-3.2.0-bin\bin
lib D:\OSG\OSG-3.2.0-bin\lib
(3)执行,成功时,展示出glider.osg模型
1
问题:OSG3.2下,vs2008配置不成功,文件读取时内存溢出。
解决方案:不使用vs2008,使用vs2010进行配置成功。Project->helloworld propertypages->configuration properties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,库目录
include D:\OSG\OSG-3.2.0-bin\include
bin D:\OSG\OSG-3.2.0-bin\bin
lib D:\OSG\OSG-3.2.0-bin\lib
示例2-1
OSGRateDisplay 显示中文帧速
Main.cpp
#ifdef _DEBUG
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgGAd.lib")//Debug
#else
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgGA.lib")//release
#endif
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
int main( )
{
osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
viewer->addEventHandler(new osgViewer::StatsHandler);
//viewer->setSceneData(osgDB::readNodeFile("glider.osg"));
viewer->setSceneData(osgDB::readNodeFile("cow.osg"));
returnviewer->run();
}
(2)配置
Project->helloworld property pages->configurationproperties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,库目录
include D:\OSG\OSG-3.2.0-bin\include
bin D:\OSG\OSG-3.2.0-bin\bin
lib D:\OSG\OSG-3.2.0-bin\lib
(3)执行,成功时,展示出cow.osg模型
1
问题:无法解析的外部符号
错误 5 errorLNK2019: 无法解析的外部符号"__declspec(dllimport) public: virtual class osg::Object * __thiscallosgGA::GUIEventHandler::cloneType(void)const "(__imp_?cloneType@GUIEventHandler@osgGA@@UBEPAVObject@osg@@XZ),该符号在函数 "[thunk]:public:virtual class osg::Object * __thiscallosgGA::GUIEventHandler::cloneType`vtordisp{4294967292,152}' (void)const "(?cloneType@GUIEventHandler@osgGA@@$4PPPPPPPM@JI@BEPAVObject@osg@@XZ) 中被引用 D:\OSGProject\RateDisplay1\RateDisplay1\main.obj RateDisplay1
解决方案:教材中的源代码有问题,应该添加句
#pragma comment(lib,"osgGAd.lib")//Debug
.........
#pragma comment(lib,"osgGA.lib")//release
笔记:一般出现无法解析的外部符号为库没有添加进去,根据错误提示的osgGA锁定问题是osgGA库未添加。
示例2-2
OSG DefaultEvent添加默认事件
1
问题:按键盘中的“S/s”无法显示状态(state),这并非键盘不响应所致,而是未添加显示状态(state)的事件响应,可能是我的版本3.2和教材版本3.0不同所致。那3.2版本要显示state该添加哪个事件呢?
示例2-3
OSGBasicGraphic 绘制正方形
1
pragma comment:
该指令将一个注释记录放入一个对象文件或可执行文件中。常用的lib关键字,可以帮我们连入一个库文件。