OpenSceneGraph 3.0: Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Storing array data

As already mentioned in earlier chapters, OSG supports vertex arrays and VBO to speed up the rendering process. To manage the vertex data used in these two mechanisms, OSG defines a basic osg::Array class and a few derived classes for commonly used array and index array types.

The osg::Array class can't be instantiated, but it declares interfaces to exchange with OpenGL calls and buffer data modifiers. Its subclasses (osg::Vec2Array, osg::Vec3Array, osg::UIntArray, etc.) inherit the characteristics of the Standard Template Library vector class, and can thus make use of all of the std::vector members, including push_back(), pop_back(), size(), and STL algorithms and iterators.

The following code will add a three-element vector to an existing osg::Vec3Array object named vertices:

vertices->push_back( osg::Vec3(1.0f, 0.0f, 0.0f) );

The OSG built-in array classes should be allocated from heap and managed by smart pointers. However, it is not necessary for the array elements such as osg::Vec2 and osg::Vec3 to follow this rule, as they are very basic data types.

The osg::Geometry class acts as the high-level wrapper of the OpenGL vertex array functionality. It records different types of arrays and manages a geometry primitive set to render these vertex data in an orderly manner. It is derived from osg::Drawable class and can be added to an osg::Geode object at any time. This class accepts arrays as basic data carriers and uses them to produce simple or complex geometry models.