本例示范了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. 从后台看python--为什么说python是慢的

    python越来越作为一种科学技术研究的语言越来越流行,可是我们经常听到一个问题,python是慢的.那么我们从后台分析一下,为什么python是慢的. python是一种动态类型,解释型语言,它的值 ...

  2. passwd - 密码文件

    描述 Passwd 是个文本文件, 它包含了一个系统帐户列表, 给出每个帐户一些有用的信息,比如用户 ID,组 ID, 家目录, shell,等. 通常它也包含了每个用户经过加密的密码. 它通常应该是 ...

  3. Spring MVC (二)

    一.使用 @RequestMapping 映射请求 Spring MVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求 在控制器的类定义以及方法定义处都可以标注 类定义处:提 ...

  4. DELPHI FMX 同时使用LONGTAP和TAP

    在应用到管理图标时,如长按显示删除标志,单击取消删除标志.在FMX的手势管理中,只有长按LONGTAP,点击TAP则是单独的事件,不能在同事件中管理.在执行LONGTAP后,TAP也会被触发​,解决方 ...

  5. 禁止input输入框输入指定内容

    链接: http://blog.csdn.net/xiaoya_syt/article/details/52746598

  6. 【转】Django 模板语法

    转自:https://www.cnblogs.com/love9527/p/9077863.html Django 模板语法 一.模板 只要是在html里面有模板语法就不是html文件了,这样的文件就 ...

  7. sqlserver连接-2

    本地连接 方法1. 方法2. 远程连接 如果无法通过IP地址远程连接你的SQL Server 2008服务器,可以参考下面的内容进行设置. 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器 ...

  8. 笨办法学Python(learn python the hard way)--练习程序1-10

    下面是当初看这本书时按照书中的代码做的练习,一行一行敲下来的,都已经试运行过,没有错误(基于python3),练习1-练习10 #ex1.py 1 #print("Hello world!& ...

  9. log4j file 路径

    默认以System.getProperty("user.dir")为准 用LOGGER.warn(System.getProperty("user.dir")) ...

  10. JavaScript .filter() 方法全解析

    .filter是一个内置的数组迭代方法,它接受一个"谓词(译者注: 指代一个过滤条件的函数)",该"谓词"针对每个值进行调用,并返回一个符合该条件(" ...