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

  1. #include <osgDB/ReadFile>
  2. #include <osgUtil/Optimizer>
  3. #include <osg/CoordinateSystemNode>
  4. #include <osg/Switch>
  5. #include <osgText/Text>
  6. #include <osgViewer/Viewer>
  7. #include <osgViewer/ViewerEventHandlers>
  8.  
  9. #include <osgGA/TrackballManipulator>
  10. #include <osgGA/FlightManipulator>
  11. #include <osgGA/DriveManipulator>
  12. #include <osgGA/KeySwitchMatrixManipulator>
  13. #include <osgGA/StateSetManipulator>
  14. #include <osgGA/AnimationPathManipulator>
  15. #include <osgGA/TerrainManipulator>
  16. #include <iostream>
  17. int main(int argc, char **argv)
  18. {
  19. osg::ArgumentParser arguments(&argc, argv);
  20. arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
  21. arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() +
  22. " is the standard OpenSceneGraph example which loads and visualises 3d models.");
  23. arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName() +
  24. " [options] filename...");
  25. arguments.getApplicationUsage()->addCommandLineOption("--image <filename>", "Load an image and render it on quad");
  26. arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>", "Load an image/DEM and render it on a HeightField");
  27. arguments.getApplicationUsage()->addCommandLineOption("-h or -help", "Display command line parameters");
  28. arguments.getApplicationUsage()->addCommandLineOption("--help-env", "Display environmental variables available");
  29. arguments.getApplicationUsage()->addCommandLineOption("--help-keys", "Display keyboard & mouse bindings available");
  30. arguments.getApplicationUsage()->addCommandLineOption("--help-all", "Display command line, env vars and keyboard & mouse bindings.");
  31. arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded", "Select SingleThreaded threading model for viewer.");
  32. arguments.getApplicationUsage()->addCommandLineOption("-CullDrawThreadPerContext",
  33. "Select CullDrawThreadPerContext threading model for viewer.");
  34. arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext",
  35. "Select DrawThreadPerContext threading model for viewer.");
  36. arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext",
  37. "Select CullThreadPerCameraDrawThreadPerContext threading model for viewer.");
  38. bool helpAll = arguments.read("--help-all");
  39. unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help")) ?
  40. osg::ApplicationUsage::COMMAND_LINE_OPTION : ) |
  41. ((helpAll || arguments.read("--help-env")) ?
  42. osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : ) |
  43. ((helpAll || arguments.read("--help-keys")) ?
  44. osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : );
  45. if (helpType)
  46. {
  47. arguments.getApplicationUsage()->write(std::cout, helpType);
  48. return ;
  49. }
  50.  
  51. osgViewer::Viewer viewer(arguments);
  52. if (arguments.errors())
  53. {
  54. arguments.writeErrorMessages(std::cout);
  55. return ;
  56. }
  57. if (arguments.argc() <= )
  58. {
  59. arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
  60. return ;
  61. }
  62.  
  63. //添加一些操作器
  64. osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
  65. keyswitchManipulator->addMatrixManipulator('', "Trackball", new osgGA::TrackballManipulator());
  66. keyswitchManipulator->addMatrixManipulator('', "Flight", new osgGA::FlightManipulator());
  67. keyswitchManipulator->addMatrixManipulator('', "Drive", new osgGA::DriveManipulator());
  68. keyswitchManipulator->addMatrixManipulator('', "Terrain", new osgGA::TerrainManipulator());
  69.  
  70. std::string pathfile;
  71. char keyForAnimationPath = '';
  72. while (arguments.read("-p", pathfile))
  73. {
  74. osgGA::AnimationPathManipulator *apm = new osgGA::AnimationPathManipulator(pathfile);
  75. if (apm || !apm->valid())
  76. {
  77. unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
  78. keyswitchManipulator->addMatrixManipulator(keyForAnimationPath, "Path", apm);
  79. keyswitchManipulator->selectMatrixManipulator(num);
  80. ++keyForAnimationPath;
  81. }
  82. }
  83. viewer.setCameraManipulator(keyswitchManipulator.get());
  84.  
  85. //添加状态事件
  86. viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
  87. viewer.addEventHandler(new osgViewer::ThreadingHandler);
  88. //窗口大小变化事件
  89. viewer.addEventHandler(new osgViewer::WindowSizeHandler);
  90. //添加一些常用状态设置
  91. viewer.addEventHandler(new osgViewer::StatsHandler);
  92. viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
  93. viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
  94.  
  95. osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
  96. if (!loadedModel)
  97. {
  98. std::cout<<arguments.getApplicationName()<<": No data loaded"<<std::endl;
  99. return ;
  100. }
  101. arguments.reportRemainingOptionsAsUnrecognized();
  102. if (arguments.errors())
  103. {
  104. arguments.writeErrorMessages(std::cout);
  105. return ;
  106. }
  107. osgUtil::Optimizer optimizer;
  108. optimizer.optimize(loadedModel.get());
  109. viewer.setSceneData(loadedModel.get());
  110.  
  111. viewer.realize();
  112. viewer.run();
  113. }

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

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

OSG程序设计之Hello World 4.0的更多相关文章

  1. OSG程序设计之Hello World 3.0

    直接上代码: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgViewer/View ...

  2. OSG程序设计之Hello World 2.0

    现在为Hello World添加一些键盘响应事件. //需要多添加两个库:osgGAd.lib.osgd.lib 代码如下: #include <osgDB/ReadFile> #incl ...

  3. OSG程序设计之Hello World1.0

    对于从未接触过OSG的我来说,首先需要一个入门教程.在OSG论坛逛了半天,再加上google,最终决定使用<OSG程序设计>这本书. 下面就贴出书中的第一个例子:Hello World. ...

  4. OSG程序设计之更新回调

    更新回调(Update Callback)涉及到一个类:osg::NodeCallback.这个类重载了函数调用操作符.当回调动作发生时,将会执行这一操作符的内容. 如果节点绑定了更新回调函数,那么在 ...

  5. OSG程序设计之osg::NodeVisitor

    本文所有内容来自<OpenSceneGraph三维渲染引擎设计与实践>一书. 本文主要讨论的是OSG中节点的访问. 对于节点的访问是从节点接收一个访问器开始的,用户执行某个节点的accep ...

  6. OSG程序设计之osg::Group

    以下是一个简单的模型读取程序: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osg/N ...

  7. C语言程序设计(基础)- 第0次作业

    亲爱的同学们,恭喜你成为一名大学生,我也很荣幸能够带大家一起学习大学的第一门专业基础课.还在军训的你,肯定对大学生活和计算机专业有着美好的憧憬,那么大学生活是什么样子的那?计算机专业应该怎么学习那?请 ...

  8. 2018下C程序设计(上)第0次作业

    1.翻阅邹欣老师博客关于师生关系博客,并回答下列问题: (1)大学和高中最大的不同是什么?请看大学理想的师生关系是?有何感想? 我认为大学和高中最大的不同在于我们(包括老师)对学习的态度.在高中,学生 ...

  9. 【OSG】osgText::Text 研究

    由于需要在3D坐标轴上显示刻度值,所以要用到osgText::Text,这里简单记录一下其常见用法. 一.基本知识 常见设置 设置字体:setFont 设置内容:setText,这里输入参数需要是os ...

随机推荐

  1. kubernates常用命令

    Kubernetes常用操作命令 kubectl log  //查看日志 $ kubectl log myapp-pod –c test kubectl get pods查看pod列表 [root@n ...

  2. Sentry实时应用错误跟踪系统在Kubernetes中私有化部署

    应用错误跟踪系统:对软件系统运行过程中产生的错误日志进行收集从而实现监控告警. 虽然软件错误❌是不可避免的,但是可以降低错误数. 提高对错误的治理能力能让错误带来的损失降到最低 ​

  3. python爬取《龙岭迷窟》的数据,看看质量剧情还原度到底怎么样

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:简单 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行 ...

  4. 用Python介绍了企业资产情况的数据爬取、分析与展示。

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:张耀杰 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...

  5. sorted排序的两个方法 - Python

    在给列表排序时,sorted非常好用,语法如下: sorted(iterable[, cmp[,key[,reverse]]]) 简单列表排序,很容易完成,sorted(list)返回的对象就是列表结 ...

  6. linux CVE-2019-14287 Sudo提权漏洞

    CVE-2019-14287 sudo介绍 sudo,也就是以超级管理员身份运行(superuser do)的意思.sudo 是 Linux 中最常使用的重要实用程序之一,它功能十分强大,几乎安装在每 ...

  7. SpringCloud(一)学习笔记之项目搭建

    [springcloud项目名称不支持下划线] 一.创建父项目 File---new---project: 填写项目信息: 默认即可,点击finish创建完成: 由于父项目只用到pom文件 所以把sr ...

  8. SpringBoot+Netty+WebSocket实现实时通信

    这篇随笔暂时不讲原理,首先搭建起一个简单的可以实现通信的Demo.之后的一系列随笔会进行一些原理上的分享. 不过在这之前大家最好了解一下Netty的线程模型和NIO编程模型,会对它的整体逻辑有所了解. ...

  9. ASP.NET母版页

    ASP.NET母版页:主要是设置一致界面的页面,在固定的页中进行更新. 如图1-1所示 页头 页中(页内容) 页尾 图1-1  母版页 一般网页是固定页头和页尾,只更新页内容,来实现网页的跳转或内容的 ...

  10. Python软件定时器APScheduler使用【软件定时器,非操作系统定时器,软件可控的定时器】【用途:定时同步数据库和缓存等】【刘新宇】

    APScheduler使用 APScheduler (advanceded python scheduler)是一款Python开发的定时任务工具. 文档地址 https://apscheduler. ...