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

Geode and Drawable classes

The osg::Geode class corresponds to the leaf node of a scene graph. It has no child nodes, but always contains geometry information for rendering. Its name Geode is short for geometry node.

The geometry data to be drawn are stored in a set of osg::Drawable objects managed by osg::Geode. The non-instantiatable osg::Drawable class is defined as a pure virtual class. It has several subclasses for rendering models, images, and texts to the OpenGL pipeline. These renderable elements are collectively called drawables.

The osg::Geode class provides a few methods to attach and detach drawables, as well as collect information about them:

  1. The public method addDrawable() takes an osg::Drawable pointer as its parameter and attaches a drawable to the osg::Geode instance. All drawables added are internally managed by the osg::ref_ptr<> smart pointer.
  2. The public methods removeDrawable() and removeDrawables() will detach one or more drawables from the current osg::Geode object, and decrease their referenced counting number as well. The removeDrawable() method uses an osg::Drawable pointer as the only parameter, and removeDrawables() accepts two parameters: the zero-based index of the start element, and number of elements to be removed.
  3. The getDrawable() method returns the osg::Drawable object stored at the specified zero-based index.
  4. The getNumDrawables() method returns the total number of attached drawables. Developers are then able to traverse each drawable in a cycle with the getDrawable() method, or remove all drawables at once by using the following code:
    geode->removeDrawables( 0, geode->getNumDrawables() );