10 triangles from leaf bin 1 followed by 10 triangles from leaf bin 2 to the mesh’s index buffer. Provided the triangles never have their positions within the index buffer changed, we know that after the weld we can extract the first 10 triangles back out into leaf bin 1’s array and the second 10 back out into leaf bin 2’s index array. However, if these triangles were repositioned, we would lose all knowledge of which triangles in the welded index buffer need to be copied back out into their original index array. By placing all zeros in the mesh’s attribute buffer, we trick the mesh into thinking that all the triangles belong to a single subset (subset zero) and therefore, the optimization that moves triangles for better subset batching will not be performed.
Our next task is to loop through every row in the BinIndexData map and extract the index pointer for each subset/leaf bin. We then copy the indices stored in each index array into the mesh’s index buffer. The next section of code starts the beginning of this loop.
The next section of the loop code may look a little strange, so let us quickly explain why it is needed. As the final two input parameters, the caller can pass the address of two unsigned longs that describe vertex indices into the original vertex array passed. We know that the weld function will automatically update all the indices in the index buffer so that the triangles correctly index the updated vertices in the post-weld vertex buffer (even if many of the original vertices were deleted/collapsed). However, if we call the weld function partway though adding the triangles of a single polygon to the vertex array (which we might do in the BuildRenderData method), we will need to track the position changes made to two of the polygons vertices. This way, when the weld function returns, we can continue to add another triangle using the two vertices used in the previous triangle that was added before the weld function was called. We explained where and why we need this functionality when we covered the BuildRenderData method earlier, so refer back to that discussion if you are fuzzy on the details.
If either pFirstVertex or pPreviousVertex are not NULL, it means they point to variables assigned by the caller that contain the indices of two vertices. It also means that the caller would like to know where these two vertices end up in the vertex array after the weld. Unfortunately, the weld function does not give us this information, so we will have to do a little work beforehand. Essentially, as we add each index array to the index buffer, we will loop through those indices searching for an index that references the vertex we are interested in tracking the position changes for. For example, if *pFirstVertex=10 it
*pFirstVertex = 10
*pPreviousVertex = 12
nFirstIndex = 100
nPreviousIndex = 500