OSG Study Notes-Shape(3-3)

3.3 Using predefined geometry in OSG

In OSG, in order to simplify the drawing of the scene, and to facilitate developers to quickly construct a scene, it predefines some commonly used geometry.

3.3.1 osg::Shape class

The osg::Shape class directly inherits from the osg::Object base class. The osg::Shape class is the base class for various embedded geometries. It can be used not only for culling and collision detection, but also for generating predefined geometry objects.

Commonly used embedded geometries include the following

osg::Box square
osg::Capsule Space capsule
osg::Cone vertebral body
osg::Cylinder Cylinder
osg::HeightField height map
osg::InfinitePlane infinite plane
osg::Sphere sphere
osg::TriangleMesh triangle piece

3.3.2 osg::ShapeDrawable类

The osg::ShapeDrawable class is derived from the osg::Drawable class, and the method of associating osg::Shape is provided in the constructor of the osg::ShapeDrawable class:

  • ShapeDrawable(Shape* shape,TessellationHints *hints = 0);// The first parameter is shape, and the second parameter is not refined by default

3.3.3 Meshing class

The meshing class (osg::TessellationHints) directly inherits from the osg::Object base class. The main function of the osg::Tessellation class is to set the fineness of the predefined geometric objects. The higher the fineness, the more detailed the subdivision. But it works differently for different predefined geometry objects.

  • Box (square prism): The meshing class does not make sense for square prisms
  • Capsule (space capsule): The space capsule is divided into 3 parts, the upper and lower hemisphere parts and the side part of the cylinder. The default side of the cylinder is subdivided
  • Cone (cone): direct subdivision
  • Cylinder (column): direct subdivision
  • Sphere (ball): direct subdivision

At present, the osg::TessellationHints class is not complete, and some member functions of the class have not been implemented. For details, you can view the source code. In the embedded geometry object, by default, the fineness of the meshing class is 0, which means that the predefined geometry is drawn according to the original vertices by default, without any refinement.

3.3.4 Example of Predefined Geometry

#include<osgViewer/Viewer>
#include<osg/Node>
#include<osg/Geode>
#include<osg/Group>
#include<osg/ShapeDrawable>
#include<osgDB/ReadFile>
#include<osgDB/WriteFile>
#include<osgUtil/Optimizer>
//------------------------------------------------------------------------------------
// 绘制多个预定义的几何体
osg::ref_ptr<osg::Geode> createShape()
{
    // 创建一个叶节点
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    
    // 设置半径和高度
    float radius = 0.8f;
    float height = 1.0f;
    
    // 创建精细度对象,精细度越高,细分就越多
    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
    // 设置精细度为0.5f
    hints->setDetailRatio(0.5f);
    
    // 添加一个球体,第一个参数是预定义几何体对象,第二个是精细度,默认为0
    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),2 * 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));
    
    return geode.get();
}
//--------------------------------------------------------------------------------
int main()
{
    // 创建Viewer对象,场景浏览器
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    osg::ref_ptr<osg::Group>root = new osg::Group();
    
    // 添加到场景
    root->addChild(createShape());
    
    // 优化场景数据
    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());
    
    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->run();
    
    return 0;
}

Guess you like

Origin blog.csdn.net/liangfei868/article/details/123722844