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

Rendering basic shapes

OSG provides an osg::ShapeDrawable class, which inherits from the osg::Drawable base class, to render basic geometry shapes quickly with plain parameters. An osg::ShapeDrawable instance always includes an osg::Shape object to indicate the specified geometry's type and properties.

The setShape() method is usually used to allocate and set a shape. For example:

shapeDrawable->setShape( new osg::Box(osg::Vec3(1.0f, 0.0f, 0.0f),
                         10.0f, 10.0f, 5.0f) );

It will assign a box with a center point at (1.0, 0.0, 0.0) in its local coordinate space, width and height of 10, and depth of 5. Here, the class osg::Vec3 represents a three-element vector in OSG. Other predefined classes such as osg::Vec2 and osg::Vec4 will also help when defining vertices, colors, normals, and texture coordinates.

Note that osg::Vec3 means a float type vector, and osg::Vec3d means a double type one, as do osg::Vec2 and osg::Vec2d, osg::Vec4 and osg::Vec4d, and so on.

The most frequently used basic shapes defined in OSG are: osg::Box, osg::Capsule, osg::Cone, osg::Cylinder, and osg::Sphere. Their appearances can be well defined by passing parameters directly to constructors.