Search code examples
opengldrawindices

glDrawElements - how does it "consume" indices?


I am curious how glDrawElements uses it's indices. Let's say I have array of indices {1, 2, 3, 4, 5, 6}, drawn by GL_TRIANGLES. My question is, whether it forms 2 triangles by indices "123", "456" or 4 triangles "123", "234", "345", "456". I am importing .obj model, data are loaded as they should, but the rendering just gets messed up.


Solution

  • In GL_TRIANGLES mode, it needs 3 vertices for every triangle, thus it will pull 3 indices at a time (so, it will draw the two triangles with indices 1,2,3 and 4,5,6).

    Different figures apply if you have adjacency or draw a triangle strip, of course.

    Also, since you mentioned that you are importing an OBJ file, do note that there may be different indices on vertices and texture coordinates and/or normals in the OBJ file.
    This is not a bug, but a feature. This is allowable in OBJ, and many exporters do that when surfaces are smooth shaded (i.e. two faces share a normal) to conserve space.
    You must construct a list of unique vertices, each with its own normal, texcoord, etc. and use the indices for these.