接下来是用createModel函数创建模型:

 osg::ref_ptr<osg::Group> createModel(bool overlay, osgSim::OverlayNode::OverlayTechnique technique)
{
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 100.0f; osg::ref_ptr<osg::Group> root = new osg::Group; float baseHeight = center.z()-radius*0.5;
osg::ref_ptr<osg::Node> baseModel = createBase(osg::Vec3(center.x(), center.y(), baseHeight),radius);
osg::ref_ptr<osg::Node> movingModel = createMovingModel(center,radius*0.8f); if (overlay)
{
osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique);
overlayNode->setContinuousUpdate(true);
overlayNode->setOverlaySubgraph(movingModel);
overlayNode->setOverlayBaseHeight(baseHeight-0.01);
overlayNode->addChild(baseModel);
root->addChild(overlayNode);
}
else
{ root->addChild(baseModel);
} root->addChild(movingModel); return root;
}

  这个函数首先根据前面定义的函数,创建方格地板和运动模型。根据函数的参数overlay决定是否建立重叠效果。

若指定创建重叠效果,则根据函数的参数technique创建重叠节点,并将运动模型movingModel设为其重叠的模型。将方格地板加入到重叠节点,并将重叠节点加入到根节点中。

如未指定创建重叠模式,则直接将方格地板和运动模型添加到根节点返回。

  最后是主函数代码:

 int main( int argc, char **argv )
{ bool overlay = false;
osg::ArgumentParser arguments(&argc,argv);
while (arguments.read("--overlay")) overlay = true; osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
while (arguments.read("--object")) { technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
while (arguments.read("--ortho") || arguments.read("--orthographic")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
while (arguments.read("--persp") || arguments.read("--perspective")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; overlay=true; } // initialize the viewer.
osgViewer::Viewer viewer; // load the nodes from the commandline arguments.
osg::ref_ptr<osg::Group> model = createModel(overlay, technique);
if (!model)
{
return ;
} // tilt the scene so the default eye position is looking down on the model.
osg::ref_ptr<osg::MatrixTransform> rootnode = new osg::MatrixTransform;
rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
rootnode->addChild(model); // run optimization over the scene graph
osgUtil::Optimizer optimzer;
optimzer.optimize(rootnode); std::string filename;
if (arguments.read("-o",filename))
{
osgDB::writeNodeFile(*rootnode, filename);
return ;
} // set the scene to render
viewer.setSceneData(rootnode); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); // viewer.setUpViewOnSingleScreen(1); #if 0 // use of custom simulation time. viewer.realize(); double simulationTime = 0.0; while (!viewer.done())
{
viewer.frame(simulationTime);
simulationTime += 0.001;
} return ;
#else // normal viewer usage.
return viewer.run(); #endif
}

  主函数中,根据运行程序时指定的命令行参数,用createModel函数创建不同的节点对象。最后对生成的节点进行选转,优化等操作。在运行程序时,若在命令行中输入 -o选项,并在其后指定文件名,则程序可将生成的模型保存在指定的文件中。

运行程序,分别指定和不指定overlay选项时,产生不同的效果。

overlay

可以看到地板上会有模型的投影。

nonOverlay

Enjoy!

Learning OSG programing---osgAnimation(3)的更多相关文章

  1. Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)

    Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些论文, ...

  2. Deep Learning论文笔记之(八)Deep Learning最新综述

    Deep Learning论文笔记之(八)Deep Learning最新综述 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些论文,但老感觉看完 ...

  3. Deep Learning论文笔记之(六)Multi-Stage多级架构分析

    Deep Learning论文笔记之(六)Multi-Stage多级架构分析 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些 ...

  4. Learning Cocos2d-x for WP8(2)——深入刨析Hello World

    原文:Learning Cocos2d-x for WP8(2)--深入刨析Hello World cocos2d-x框架 在兄弟篇Learning Cocos2d-x for XNA(1)——小窥c ...

  5. Learning Cocos2d-x for WP8(1)——创建首个项目

    原文:Learning Cocos2d-x for WP8(1)--创建首个项目 Cocos2d-x for WP8开发语言是C++,系列文章将参考兄弟篇Learning Cocos2d-x for ...

  6. Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

    原文:Learning Cocos2d-x for WP8(9)--Sprite到哪,我做主 工程文件TouchesTest.h和TouchesTest.cpp 相关素材文件 事件驱动同样适用于coc ...

  7. Learning Cocos2d-x for WP8(8)——动作Action

    原文:Learning Cocos2d-x for WP8(8)--动作Action 游戏很大程度上是由动作画面支撑起来的. 动作分为两大类:瞬间动作和延时动作. 瞬间动作基本等同于设置节点的属性,延 ...

  8. Learning Cocos2d-x for WP8(7)——让Sprite动起来

    原文:Learning Cocos2d-x for WP8(7)--让Sprite动起来 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(7)——让Sprite动起来 本讲将 ...

  9. Learning Cocos2d-x for WP8(6)——场景切换和场景过渡效果

    原文:Learning Cocos2d-x for WP8(6)--场景切换和场景过渡效果 C#(wp7)兄弟篇 Learning Cocos2d-x for XNA(6)——场景切换和场景过渡效果 ...

  10. Learning Cocos2d-x for WP8(5)——详解Menu菜单

    原文:Learning Cocos2d-x for WP8(5)--详解Menu菜单 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(5)——详解Menu菜单 菜单是游戏必不 ...

随机推荐

  1. Python 3实现网页爬虫

    1 什么是网页爬虫 网络爬虫( 网页蜘蛛,网络机器人,网页追逐者,自动索引,模拟程序)是一种按照一定的规则自动地抓取互联网信息的程序或者脚本,从互联网上抓取对于我们有价值的信息.Tips:自动提取网页 ...

  2. wxpython菜单栏、子菜单栏、弹出菜单栏、状态栏小程序学习源代码分享

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  3. nginx报错[error] CreateFile() "D:\Java-windows\nginx-1.16.0/logs/nginx.pid" failed (2: The system cannot find the file specified)

    无论是nginx -s stop还是nginx -s reload命令,都会出现这个错误. 解决方法:使用命令创建/logs/nginx.pid文件,命令如下所示: nginx -c conf/ngi ...

  4. 使用vue-resource请求数据的步骤

    1.需要安装 vue-resource模块 注意加上--save npm install vue-resource --save 2.main.js 引入vue-resource import Vue ...

  5. 【转】DDR3和eMMC区别

    转自:https://www.cnblogs.com/debruyne/p/9186619.html DDR3内存条和eMMC存储器区别: 1. 存储性质不同:2. 存储容量不同:3. 运行速度不同: ...

  6. [sql 注入] 注入类型

    基于整型的注入: url:http://localhost/?id=12 拼接sql:$sql = "select * from user where id = {$_GET['id']}& ...

  7. windows下laravel 快速安装

    1. 安装composer  https://getcomposer.org/ 2. 安装git windows 客户端工具 https://git-scm.com/downloads 3. 更改co ...

  8. tensorflow图像处理函数(1)

    1.tensorflow中对jpeg格式图像的编码/解码函数: import matplotlib.pyplot as plt import tensorflow as tf image_raw_da ...

  9. handy源码阅读(三):SafeQueue类

    SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...

  10. AutoLayout面试题记录-用NSLayoutConstraint写动画

    import UIKit class ViewController: UIViewController { @IBOutlet weak var topY: NSLayoutConstraint! @ ...