OSG程序设计之Hello World 4.0
2024-10-09 02:37:01
代码如下: //需要添加两个库: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版本,唯一的新东西就是有个模型优化。
最新文章
- Java Web 学习链接
- R语言中的logical(0)和numeric(0)以及赋值问题
- 【探讨】javascript事件机制底层实现原理
- start bbs轻论坛安装时数据库提示错误
- Zookeeper全解析——Client端(转)
- solr python客户端 - solrpy
- 44个 Javascript 变态题解析 (上\下)
- win8 hyper-v 禁用不必卸载虚拟机
- string之substring的用法
- web本地存储-IndexedDB
- solr query的post方式
- L2-014 列车调度 (25 分)
- 转:centos7/rhel7安装较高版本ruby2.2/2.3/2.4+
- linux的典型分支:
- Linux 修改yum源
- 把dataset对象转换成list集合方法
- 自动释放池autoreleasepool
- msgpack和protobuf的对比
- 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)
- HDU 1284 钱币兑换问题(普通型 数量无限的母函数)