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

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

 osg::Geode* createShapes(osg::ArgumentParser& arguments)
{
osg::Geode* geode = new osg::Geode(); // ---------------------------------------
// Set up a StateSet to texture the objects
// ---------------------------------------
osg::StateSet* stateset = new osg::StateSet();
  //设置材质图片
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile( "Images/lz.rgb" );
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
stateset->setTextureAttributeAndModes(,texture, osg::StateAttribute::ON);
} stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON); geode->setStateSet( stateset ); float radius = 0.8f;
float height = 1.0f; osg::TessellationHints* hints = new osg::TessellationHints;
hints->setDetailRatio(0.5f);
  //建立各种几何实体
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),*radius),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
  //用于控制平面的高程起伏
osg::HeightField* grid = new osg::HeightField;
if (arguments.read("--large"))      //大范围
{
unsigned int numX = ;
unsigned int numY = ;
double sizeX = 10.0;
double sizeY = 10.0;
grid->allocate(numX,numY);
grid->setXInterval(sizeX/float(numX));
grid->setYInterval(sizeY/float(numY)); for(unsigned int r=;r<numY;++r)
{
for(unsigned int c=;c<numX;++c)
{
double rx = double(c)/double(numX-);
double ry = double(r)/double(numY-); grid->setHeight(c, r, 2.0*sin(rx*ry*4.0*osg::PI));
}
}
}
else              //小范围
{
grid->allocate(,);
grid->setXInterval(0.28f);
grid->setYInterval(0.28f); for(unsigned int r=;r<;++r)
{
for(unsigned int c=;c<;++c)
{
grid->setHeight(c,r,vertex[r+c*][]);
}
}
} geode->addDrawable(new osg::ShapeDrawable(grid)); return geode;
}

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

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

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

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

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

int main(int argc, char **argv)
{
osg::ArgumentParser arguments(&argc,argv); // construct the viewer.
osgViewer::Viewer viewer(arguments); // add model to viewer.
viewer.setSceneData( createShapes(arguments) ); // add the state manipulator
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); return viewer.run();
}

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

--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. 浅谈使用canvas绘制多边形

    本文主要使用坐标轴的使用来绘制多边形,点位则都是在y轴上寻找,这种方法能够更好的理解图形与修改. //id为html里canvas标签的属性id: //x,y为坐标轴的起始位置,因为canvas默认坐 ...

  2. JS对象总结

    JS对象总结   复习: 1.1 JS中对象有三种:内置对象(数组Array对象.String字符串对象.RegExp正则表达式对象.Math对象). 宿主对象(JS脚本所在的运行环境,目前我们讲的脚 ...

  3. ln创建软链接方式

    ln -s 目标文件 软链接

  4. [转]使用flask实现mock server

    什么是mock server: http://www.testclass.net/interface/mock_server 使用flask 实现  mock server : http://www. ...

  5. Installation of the latest version of netease-cloud-music on Fedora 30 linux platform

    Installation of the latest version of netease-cloud-music on Fedora 30 linux platform Abtract As we  ...

  6. 基于Redis做内存管理

    1 Redis存储机制: redis存储的数据类型包括,String,Hash,List,Set,Sorted Set,它内部使用一个redisObject对象来表示所有的key和value,这个对象 ...

  7. 使用国外 DNS 造成国内网站访问慢的解决方法

    本文原载于 wzyboy's blog,转载请注明本文地址: https://wzyboy.im/post/874.html ,谢谢合作. 为什么要用国外 DNS 由于众所周知的问题,国内 DNS 服 ...

  8. AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode

    AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...

  9. Java8 时间处理类的使用实践(LocalDate...)

    有了它,谁还在用Date?Calendar? 其实也不能这么绝对,毕竟还没到那个程度上.Java8 新增了处理时间的一组类(LocalDate.LocalDateTime.LocalTime),刚开始 ...

  10. python高级:垃圾回收机制

    ---恢复内容开始--- 垃圾回收机制 1.计数引用机制 就是一个变量.数据结构.对象当没有人引用时,python的会启用垃圾回收机制,将其从内存中删除. 怎么看引用的次数呢?sys模块提供的sys. ...