Time for action—drawing an octahedron

An octahedron is a polyhedron having eight triangle faces. It is really a nice example to show why primitive indexing is important. We will first sketch the octahedron structure, as shown in the following image:

Time for action—drawing an octahedron

The octahedron has six vertices, each shared by four triangles. We would have to create a vertex array with 24 elements to render all eight faces when using osg::DrawArrays. However, with the help of an index array and the osg::DrawElementsUInt class, we can allocate a vertex array with only six elements and thus improve the efficiency of drawing the geometry.

  1. Include the necessary headers:
    #include <osg/Geometry> ...

Get OpenSceneGraph 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.