<p>This section covers some of the methods that can be used to create geometric primitives. There are several ways to deal with geometry objects: at the lowest level loosly wraps OpenGL primitives; an intermediate level using open scene graph basic shapes and at a higher level, loaded from files. This tutorial covers the lowest level. This method provides the greatest flexibility and requies the most effort. Normally at the scene graph level geometry is loaded from files. Most of the effort of tracking vertices is handled by file loaders.</p>

31

</div>

32

</blockquote>

33

<div align="left">

34

<p><span class="bodystyle">Background:</span></p>

35

</div>

36

<blockquote>

37

<p><span class="bodystyle">A brief explanation of the following classes is helpful:</span></p>

38

<p><span class="bodystyle"> The 'Geode' class:</span></p>

39

<blockquote>

40

<p><span class="bodystyle"> The geode class is derived from the 'node' class. Nodes (and thus geodes) can be added to a scene graph as leaf nodes. Geode instances can have any number of 'drawables' associated with them.</span></p>

The 'geometry' class can have vertex (and vertex data) associated with it directly, or can have any number of 'primitiveSet' instances associated with it.<br>

46

Vertex and vertex attribute data (color, normals, texture coordinates) is stored in arrays. Since more than one vertex may share the same color, normal or texture coordinate, and array of indices can be used to map vertex arrays to color, normal or texture coordinate arrays.</span></p>

<p class="bodystyle">The following section of code sets up a viewer to see the scene we create, a 'group' instance to serve as the root of the scene graph, a geometry node (geode) to collect drawables, and a geometry instance to associate vertices and vertex data. (In this case the shape to render is a four-sided pyramid.)</p>

<p class="bodystyle">Declare an array of vertices. Each vertex will be represented by a triple -- an instances of the vec3 class. An instance of osg::Vec3Array can be used to store these triples. Since osg::Vec3Array is derived from the STL vector class, we can use the push_back method to add array elements. Push back adds elements to the end of the vector, thus the index of first element entered is zero, the second entries index is 1, etc.<br>

72

Using a right-handed coordinate system with 'z' up, array elements zero..four below represent the 5 points required to create a simple pyramid. </p>

<p class="bodystyle">Next, create a primitive set and add it to the pyramid geometry. Use the first four points of the pyramid to define the base using an instance of the DrawElementsUint class. Again this class is derived from the STL vector, so the push_back method will add elements in sequential order. To ensure proper backface cullling, vertices should be specified in counterclockwise order. The arguments for the constructor are the enumerated type for the primitive (same as the OpenGL primitive enumerated types), and the index in the vertex array to start from.</p>

<p>Declare the variable that will match vertex array elements to color array elements. This vector should have the same number of elements as the number of vertices. This vector serves as a link between vertex arrays and color arrays. Entries in this index array correspond to elements in the vertex array. Their values correspond to the index in the color array. This same scheme would be followed if vertex array elements were matched with normal or texture coordinate arrays.<br>

130

Note that in this case, we are assigning 5 vertices to four colors. Vertex array element zero (bottom left) and four (peak) are both assigned to color array element zero (red).</p>

<p class="bodystyle">Now that we have created a geometry node and added it to the scene we can reuse this geometry. For example, if we wanted to put a second pyramid 15 units to the right of the first one, we could add this geode as the child of a transform node in our scene graph. </p>