# how to parse the <triangles> <p> array to be used in OpenGL?

e.g. the array of

``````            <triangles count="12" material="Material">
<input offset="0" semantic="VERTEX" source="#Cube_006-Vertex"/>
<input offset="1" semantic="NORMAL" source="#Cube_006-Normals"/>

0 0 1 0 2 0 2 0 3 0
0 0 4 1 7 1 6 1 6 1
5 1 4 1 0 2 4 2 5 2
5 2 1 2 0 2 1 3 5 3
6 3 6 3 2 3 1 3 2 4
6 4 7 4 7 4 3 4 2 4
4 5 0 5 3 5 3 5 7 5
4 5</p>
</triangles
``````

with a

``````        <mesh>
<source id="Cube_006-Position">
<float_array count="24" id="Cube_006-Position-array">
1.00000 1.00000 -1.00000
1.00000 -1.00000 -1.00000
-1.00000 -1.00000 -1.00000
-1.00000 1.00000 -1.00000
1.00000 1.00000 1.00000
1.00000 -1.00000 1.00000
-1.00000 -1.00000 1.00000
-1.00000 1.00000 1.00000

</float_array>
``````

Assume I’ve already got the basic parsing, it’s the conversion to OpenGL indices that puzzles me.

position input offset is 0, normal is 1, so

in this case is in pairs of 2, every two are a position/normal index.

0 0 // vertex 0 is built from, positon[0], normal[0]: unique pair so far, tri indices [0]
1 0 // vertex 1 is built from, positon[1], normal[0]: unique pair so far, tri indices [1]
2 0 // vertex 2 is built from, positon[2], normal[0]: unique pair so far, tri indices [2]
2 0 // vertex 3 is built from, positon[2], normal[0]: duplicate of [2], tri indices [2]
3 0 // vertex 4 is built from, positon[3], normal[0]: unique pair so far, tri indices [3]
0 0 // vertex 5 is built from, positon[0], normal[0]: duplicate of [0], tri indices [0]

</p>

To build triangle indices from this, you would have the original pair list, loop this list, if the pair is not in your unique list, add it, record its index in the triangle indice list. If it is in your unique list, just record its index in the triangle indice list.