首页 > 代码库 > OSG第一个Demo

OSG第一个Demo

环境:Vs2010

  OpenSceneGraph-3.0.1-VS10.0.30319-x86-debug-12741

  OpenSceneGraph-3.0.1-VS10.0.30319-x86-release-12741

设置环境变量
PATH:                   D:\osg\bin
OSG_FILE_PATH:   D:\osg\data
这时候你可以用osgViewer查看一下osg文件,比如经典的奶牛cow.osg
在命令行下输入:osgViewerd.exe cow.osg
 
新建一个vc空项目,设置vc++包含目录和库目录。vc2010是右击|属性|项目和解决方案|VC++目录,低版本是工具|选项|项目和解决方案|VC++目录
包含文件:   D:\osg\include
库文件:      D:\osg\lib
右击|属性|配置属性|链接器|输入|附加依赖项
OpenThreadsd.lib
osgd.lib
osgDBd.lib
osgViewerd.lib
如果编译Release版本,去掉d
新建main.cpp输入下面的代码,这次显示一个滑翔机。
#include <osgDB/ReadFile>#include <osgViewer/Viewer>void main(){ osgViewer::Viewer viewer; viewer.setSceneData(osgDB::readNodeFile("glider.osg")); viewer.realize(); viewer.run();}

 http://greatverve.cnblogs.com/archive/2012/09/19/osg-hello-world.html

编译出现的问题:d和非d的lib

过程中出现的问题:Microsoft C++ 异常 std::bad_alloc

0x7c812afb 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0012f448 处的 std::bad_alloc。

原因:编译Debug版本的时候使用了不带d的Dll。

处理方法:

Demo编译成Debug版本使用带d的dll,编译成Release版本使用不带d的dll。

注意:

1. 编译Debug和Release版本需要分别设置项目属性中的包含目录文件和库目录

2. 分别设置“链接器——输入——附加依赖项”,或者输入如下代码:

 1 #ifdef _DEBUG 2 #pragma comment (lib,"OpenThreadsd.lib") 3 #pragma comment (lib,"osgd.lib") 4 #pragma comment (lib,"osgDBd.lib") 5 #pragma comment (lib,"osgFXd.lib") 6 #pragma comment (lib,"osgGAd.lib") 7 #pragma comment (lib,"osgManipulatord.lib") 8 #pragma comment (lib,"osgParticled.lib") 9 #pragma comment (lib,"osgShadowd.lib")10 #pragma comment (lib,"osgSimd.lib")11 #pragma comment (lib,"osgTerraind.lib")12 #pragma comment (lib,"osgTextd.lib")13 #pragma comment (lib,"osgUtild.lib")14 #pragma comment (lib,"osgViewerd.lib")15 #else16 #pragma comment (lib,"OpenThreads.lib")17 #pragma comment (lib,"osg.lib")18 #pragma comment (lib,"osgDB.lib")19 #pragma comment (lib,"osgFX.lib")20 #pragma comment (lib,"osgGA.lib")21 #pragma comment (lib,"osgManipulator.lib")22 #pragma comment (lib,"osgParticle.lib")23 #pragma comment (lib,"osgShadow.lib")24 #pragma comment (lib,"osgSim.lib")25 #pragma comment (lib,"osgTerrain.lib")26 #pragma comment (lib,"osgText.lib")27 #pragma comment (lib,"osgUtil.lib")28 #pragma comment (lib,"osgViewer.lib")29 # endif

 

OSG第一个Demo