代码如下:  //需要添加两个库:osgUtild.lib、osgTextd.lib

#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>
int main(int argc, char **argv)
{
osg::ArgumentParser arguments(&argc, argv);
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() +
" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName() +
" [options] filename...");
arguments.getApplicationUsage()->addCommandLineOption("--image <filename>", "Load an image and render it on quad");
arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>", "Load an image/DEM and render it on a HeightField");
arguments.getApplicationUsage()->addCommandLineOption("-h or -help", "Display command line parameters");
arguments.getApplicationUsage()->addCommandLineOption("--help-env", "Display environmental variables available");
arguments.getApplicationUsage()->addCommandLineOption("--help-keys", "Display keyboard & mouse bindings available");
arguments.getApplicationUsage()->addCommandLineOption("--help-all", "Display command line, env vars and keyboard & mouse bindings.");
arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded", "Select SingleThreaded threading model for viewer.");
arguments.getApplicationUsage()->addCommandLineOption("-CullDrawThreadPerContext",
"Select CullDrawThreadPerContext threading model for viewer.");
arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext",
"Select DrawThreadPerContext threading model for viewer.");
arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext",
"Select CullThreadPerCameraDrawThreadPerContext threading model for viewer.");
bool helpAll = arguments.read("--help-all");
unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help")) ?
osg::ApplicationUsage::COMMAND_LINE_OPTION : ) |
((helpAll || arguments.read("--help-env")) ?
osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : ) |
((helpAll || arguments.read("--help-keys")) ?
osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : );
if (helpType)
{
arguments.getApplicationUsage()->write(std::cout, helpType);
return ;
} osgViewer::Viewer viewer(arguments);
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return ;
}
if (arguments.argc() <= )
{
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
return ;
} //添加一些操作器
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
keyswitchManipulator->addMatrixManipulator('', "Trackball", new osgGA::TrackballManipulator());
keyswitchManipulator->addMatrixManipulator('', "Flight", new osgGA::FlightManipulator());
keyswitchManipulator->addMatrixManipulator('', "Drive", new osgGA::DriveManipulator());
keyswitchManipulator->addMatrixManipulator('', "Terrain", new osgGA::TerrainManipulator()); std::string pathfile;
char keyForAnimationPath = '';
while (arguments.read("-p", pathfile))
{
osgGA::AnimationPathManipulator *apm = new osgGA::AnimationPathManipulator(pathfile);
if (apm || !apm->valid())
{
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
keyswitchManipulator->addMatrixManipulator(keyForAnimationPath, "Path", apm);
keyswitchManipulator->selectMatrixManipulator(num);
++keyForAnimationPath;
}
}
viewer.setCameraManipulator(keyswitchManipulator.get()); //添加状态事件
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
viewer.addEventHandler(new osgViewer::ThreadingHandler);
//窗口大小变化事件
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
//添加一些常用状态设置
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (!loadedModel)
{
std::cout<<arguments.getApplicationName()<<": No data loaded"<<std::endl;
return ;
}
arguments.reportRemainingOptionsAsUnrecognized();
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return ;
}
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
viewer.setSceneData(loadedModel.get()); viewer.realize();
viewer.run();
}

  吐槽一下作者,作为新手教程,搞这些乱七八糟的东西有什么用呢?

  这个例子相比3.0版本,唯一的新东西就是有个模型优化。

最新文章

  1. Java Web 学习链接
  2. R语言中的logical(0)和numeric(0)以及赋值问题
  3. 【探讨】javascript事件机制底层实现原理
  4. start bbs轻论坛安装时数据库提示错误
  5. Zookeeper全解析——Client端(转)
  6. solr python客户端 - solrpy
  7. 44个 Javascript 变态题解析 (上\下)
  8. win8 hyper-v 禁用不必卸载虚拟机
  9. string之substring的用法
  10. web本地存储-IndexedDB
  11. solr query的post方式
  12. L2-014 列车调度 (25 分)
  13. 转:centos7/rhel7安装较高版本ruby2.2/2.3/2.4+
  14. linux的典型分支:
  15. Linux 修改yum源
  16. 把dataset对象转换成list集合方法
  17. 自动释放池autoreleasepool
  18. msgpack和protobuf的对比
  19. 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)
  20. HDU 1284 钱币兑换问题(普通型 数量无限的母函数)

热门文章

  1. 实时OLAP分析利器Druid介绍
  2. ASP.NET Core中配置监听URLs的五种方式
  3. Java包机制和Javadoc的使用
  4. 架构师修炼之设计模式 - 策略模式(Strategy) 【Python与C#实现】
  5. G - Messy codeforces1262C
  6. vue2.x学习笔记(十四)
  7. bypass安全狗测试学习
  8. eclipse添加方法注释
  9. python爬虫面试题集锦及答案
  10. TensorFlow的模型保存与加载