algorithmlookupvertex-bufferindex-buffer

Fastest algorithm to lookup which triangles share a vertex


There are index and vertex buffers like the ones described here to draw triangles.

Frequently in my code, I need to lookup which triangles share a specific vertex. The most basic approach is:

Now I wonder if I'm missing more efficient or standard algorithms. Thanks.


Solution

  • Create a second lookup table which maps index of a vertex to the list of triangles containing it.

    It can be created by looping over the index buffer and adding the triangle to all three of it's vertices.

    For instance, if the index buffer is:

    0, 1, 4
    0, 2, 3
    

    The lookup table will be

    0 -> [t0, t1]
    1 -> [t0]
    2 -> [t1]
    3 -> [t1]
    4 -> [t0]
    

    If the data is being updated, this lookup table would have to updated accordingly. Depending on how large the number of triangles sharing a single vertex can be, and how often you are adding/deleting them, you might need to use a hash table instead of an ordinary list to store these triangles.