本例示范了osg中Shape ---- 基本几何元素的绘制过程。参照osg官方文档,Shape 类包含以下子类:

在示例程序中,函数createShapes函数用于生成需要绘制的几何形状。

  1. osg::Geode* createShapes(osg::ArgumentParser& arguments)
  2. {
  3. osg::Geode* geode = new osg::Geode();
  4.  
  5. // ---------------------------------------
  6. // Set up a StateSet to texture the objects
  7. // ---------------------------------------
  8. osg::StateSet* stateset = new osg::StateSet();
  9.   //设置材质图片
  10. osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile( "Images/lz.rgb" );
  11. if (image)
  12. {
  13. osg::Texture2D* texture = new osg::Texture2D;
  14. texture->setImage(image);
  15. texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
  16. stateset->setTextureAttributeAndModes(,texture, osg::StateAttribute::ON);
  17. }
  18.  
  19. stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
  20.  
  21. geode->setStateSet( stateset );
  22.  
  23. float radius = 0.8f;
  24. float height = 1.0f;
  25.  
  26. osg::TessellationHints* hints = new osg::TessellationHints;
  27. hints->setDetailRatio(0.5f);
  28.   //建立各种几何实体
  29. geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
  30. geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),*radius),hints));
  31. geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
  32. geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
  33. geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
  34.   //用于控制平面的高程起伏
  35. osg::HeightField* grid = new osg::HeightField;
  36. if (arguments.read("--large"))      //大范围
  37. {
  38. unsigned int numX = ;
  39. unsigned int numY = ;
  40. double sizeX = 10.0;
  41. double sizeY = 10.0;
  42. grid->allocate(numX,numY);
  43. grid->setXInterval(sizeX/float(numX));
  44. grid->setYInterval(sizeY/float(numY));
  45.  
  46. for(unsigned int r=;r<numY;++r)
  47. {
  48. for(unsigned int c=;c<numX;++c)
  49. {
  50. double rx = double(c)/double(numX-);
  51. double ry = double(r)/double(numY-);
  52.  
  53. grid->setHeight(c, r, 2.0*sin(rx*ry*4.0*osg::PI));
  54. }
  55. }
  56. }
  57. else              //小范围
  58. {
  59. grid->allocate(,);
  60. grid->setXInterval(0.28f);
  61. grid->setYInterval(0.28f);
  62.  
  63. for(unsigned int r=;r<;++r)
  64. {
  65. for(unsigned int c=;c<;++c)
  66. {
  67. grid->setHeight(c,r,vertex[r+c*][]);
  68. }
  69. }
  70. }
  71.  
  72. geode->addDrawable(new osg::ShapeDrawable(grid));
  73.  
  74. return geode;
  75. }

在以上代码中,首先建立了几何节点Geode,加载纹理图像,并将其设置为节点的材质。

之后向节点中加入各种Shape模型,设置它们的集合参数。之后建立了高程域模型osg::HeightField* grid,根据运行程序时提供的命令行参数,设置其为不同的点密度。若运行命令中提供有--large选项,则建立高密度高程集,两层for循环中嵌套的语句,即为计算高程网格中各点的高程值方程。对于large情形,其在点(x,y)处的高程值为2*sin(x*y*4π)。而在普通情况下,点的高程值取决与vertex向量的第三个分量,因为在文件的开始,包含了地形坐标数据:

  1. #include "../osghangglide/terrain_coords.h"

最后,将定义的地形平面添加到节点中去。

在主函数中,调用上面定义的createShapes函数,在进行一些其他准备工作即可:

  1. int main(int argc, char **argv)
  2. {
  3. osg::ArgumentParser arguments(&argc,argv);
  4.  
  5. // construct the viewer.
  6. osgViewer::Viewer viewer(arguments);
  7.  
  8. // add model to viewer.
  9. viewer.setSceneData( createShapes(arguments) );
  10.  
  11. // add the state manipulator
  12. viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
  13.  
  14. return viewer.run();
  15. }

按不同模式运行程序,得到不同的效果:

--large模式:

正常模式:

由此程序受到启发,可利用osg高程域,建立DEM(数字高程模型)浏览或仿真、编辑工具,达到学以致用的目的。

Enjoy!

Learning OSG programing---osgShape的更多相关文章

  1. Learning OSG programing---osgScribe

    Learning OSG programing---osgScribe Scribe可以翻译为素描,抄写等.本例通过在模型表面添加一层素描,来显示模型的骨架. 关键代码: osg::ref_ptr&l ...

  2. Learning OSG programing---Multi Camera in Multi window 在多窗口中创建多相机

    这个例子演示了在多个窗口中创建多个相机,函数的代码如下: void multiWindowMultipleCameras(osgViewer::Viewer& viewer,bool mult ...

  3. Learning OSG programing---Multi Camera in one window 在单窗口中创建多相机

    在学习OSG提供的例子osgCamera中,由于例子很长,涉及很多细节,考虑将其分解为几个小例子.本文介绍实现在一个窗口中添加多个相机的功能. 此函数接受一个Viewer引用类型参数,设置图形上下文的 ...

  4. Learning OSG programing---osgAnimation(3)

    接下来是用createModel函数创建模型: osg::ref_ptr<osg::Group> createModel(bool overlay, osgSim::OverlayNode ...

  5. Learning OSG programing---osgAnimation(2)

    osg::Node* createBase(const osg::Vec3& center,float radius) { ; ; *radius; *radius; osg::Vec3 v0 ...

  6. Learning OSG programing---osgAnimation(1)

    osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) { ...

  7. Learning OSG programing---osgClip

    OSG Clip例程剖析 首先是创建剪切节点的函数代码: osg::ref_ptr<osg::Node> decorate_with_clip_node(const osg::ref_pt ...

  8. Learning OSG programing---osgwindows

    /* OpenSceneGraph example, osgwindows. * * Permission is hereby granted, free of charge, to any pers ...

  9. Coursera Deep Learning 2 Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization - week3, Hyperparameter tuning, Batch Normalization and Programming Frameworks

    Tuning process 下图中的需要tune的parameter的先后顺序, 红色>黄色>紫色,其他基本不会tune. 先讲到怎么选hyperparameter, 需要随机选取(sa ...

随机推荐

  1. Robot Framework 源码阅读 day1 __main__.py

    robot文件夹下的__main__.py函数 是使用module运行时的入口函数: import sys # Allows running as a script. __name__ check n ...

  2. 异步json发送put或者delete

    第一种 put请求或者delete请求 直接写发送的情况 //批量删除 function batchDel() { var ids = []; $("#list-table").f ...

  3. python if-else替代三元表达式

    python中判断一个数是否是偶数的常规代码: def _compare(data): if data % 2 == 0: return True else: return False # 调用偶数判 ...

  4. Google Capture The Flag 2018 (Quals) - Reverse - Beginner's Quest - Gatekeeper

    参考链接:https://ctftime.org/task/6264 题目 It's a media PC! All fully purchased through the online subscr ...

  5. springboot操作rabbitmq

    ////DirectExchange directExchange = new DirectExchange("test.direct");////amqpAdmin.declar ...

  6. maven 配置发布仓库

    ·首先,在工程的pom.xml中添加仓库信息 <distributionManagement> <repository> <id>releases</id&g ...

  7. Js中window.location.href和window.location.replace的区别

    href相当于打开一个新页面,replace相当于替换当前页面:这里打开页面都是针对历史记录来说,在页面上看完全相同,只是浏览器的history表现不同如果在1.html中点击链接到2.html,然后 ...

  8. LYXF-PE-tools

    先随便说一下这个PE-tools有什么用? 我开发这款PE-tools是为了学习而开发的,且是开源的,这里我会提供源码链接.它可以解析windows 32/64位程序中比较常用的一些属性. 里面有个稍 ...

  9. js执行上下文与执行上下文栈

    一.什么是执行上下文 简单说就是代码运行时的执行环境,必须是在函数调用的时候才会产生,如果不调用就不会产生这个执行上下文.在这个环境中,所有变量会被事先提出来(变量提升),有的直接赋值,有的为默认值 ...

  10. [hadoop](1) MapReduce:ChainMapper

    前言 本章主要讲述的是对于hadoop生态系统中,MapReduce写的ChainMapper的学习.MapReduce是hadoop集群数据处理的默认框架.而对于数据集中所有的数据必然有一些不友好的 ...